-
Notifications
You must be signed in to change notification settings - Fork 27
Remove interrupt tasks entirely #1222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DavisVaughan
wants to merge
13
commits into
main
Choose a base branch
from
feature/task-timing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b4a1105
Let R be fully in charge of `R_RunPendingFinalizers()`
DavisVaughan 6cdcae8
Note why it is preferable to never supply `NULL` to `R_runHandlers()`
DavisVaughan 877b6ec
Remove usage of `R_PolledEvents()` entirely
DavisVaughan 707ecd2
Implement `report_crash()` via `ShowMessageRequest`
DavisVaughan 7b22393
Move `r_task()` to `IDLE_ANY_TASKS` and drop `INTERRUPT_TASKS`
DavisVaughan c4548a4
Tweak comment
DavisVaughan 9ffbc42
Use `idle_any_prompt()` for `process_console_notifications()`
DavisVaughan 835b75c
`report_crash()` now has a 5 second timeout
DavisVaughan 7ee58e7
Directly test that `r_task()` runs at `browser()` prompts
DavisVaughan 5bc20f6
Use slightly more standard "inner" suffix
DavisVaughan 5aa75f9
Tweak message
DavisVaughan 73798ce
Fix clippy lint
DavisVaughan 36b248f
`check_timeout()` is now used on all platforms
DavisVaughan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,13 +24,11 @@ use crate::console::Console; | |
| use crate::console::ConsoleOutputCapture; | ||
| use crate::fixtures::r_test_init; | ||
|
|
||
| /// Task channels for interrupt-time tasks | ||
| static INTERRUPT_TASKS: LazyLock<TaskChannels> = LazyLock::new(TaskChannels::new); | ||
|
|
||
| /// Task channels for idle-time tasks | ||
| /// Task channels for idle-time tasks (top-level only) | ||
| static IDLE_TASKS: LazyLock<TaskChannels> = LazyLock::new(TaskChannels::new); | ||
|
|
||
| /// Task channels for idle tasks that run at any idle prompt (top-level or browser) | ||
| /// Task channels for idle tasks that run at any idle prompt (top-level or browser, but | ||
| /// not input) | ||
| static IDLE_ANY_TASKS: LazyLock<TaskChannels> = LazyLock::new(TaskChannels::new); | ||
|
|
||
| // Compared to `futures::BoxFuture`, this doesn't require the future to be Send. | ||
|
|
@@ -64,19 +62,11 @@ impl TaskChannels { | |
| } | ||
| } | ||
|
|
||
| /// Returns receivers for interrupt, idle, and debug-idle tasks. | ||
| /// Returns receivers for idle and idle-any tasks. | ||
| /// Initializes the task channels if they haven't been initialized yet. | ||
| /// Can only be called once (intended for `Console` during init). | ||
| pub(crate) fn take_receivers() -> ( | ||
| Receiver<QueuedRTask>, | ||
| Receiver<QueuedRTask>, | ||
| Receiver<QueuedRTask>, | ||
| ) { | ||
| ( | ||
| INTERRUPT_TASKS.take_rx(), | ||
| IDLE_TASKS.take_rx(), | ||
| IDLE_ANY_TASKS.take_rx(), | ||
| ) | ||
| pub(crate) fn take_receivers() -> (Receiver<QueuedRTask>, Receiver<QueuedRTask>) { | ||
| (IDLE_TASKS.take_rx(), IDLE_ANY_TASKS.take_rx()) | ||
| } | ||
|
|
||
| pub enum QueuedRTask { | ||
|
|
@@ -148,7 +138,7 @@ impl std::task::Wake for RTaskWaker { | |
| } | ||
|
|
||
| impl RTaskStartInfo { | ||
| pub(crate) fn new(idle: bool) -> Self { | ||
| pub(crate) fn new() -> Self { | ||
| let thread = std::thread::current(); | ||
| let thread_id = thread.id(); | ||
| let thread_name = thread | ||
|
|
@@ -158,7 +148,7 @@ impl RTaskStartInfo { | |
| .to_owned(); | ||
|
|
||
| let start_time = std::time::Instant::now(); | ||
| let span = tracing::trace_span!("R task", thread = thread_name, interrupt = !idle,); | ||
| let span = tracing::trace_span!("R task", thread = thread_name); | ||
|
|
||
| Self { | ||
| thread_id, | ||
|
|
@@ -191,6 +181,9 @@ impl RTaskStartInfo { | |
| // running, so borrowing is allowed even though we send it to another | ||
| // thread. See also `Crossbeam::thread::ScopedThreadBuilder` (from which | ||
| // `r_task()` is adapted) for a similar approach. | ||
| // | ||
| // `r_task()`s run via `IDLE_ANY_TASKS`, i.e. they run at top level, and at a debugger | ||
| // prompt (but notably not the input prompt as a way to reduce risk of reentrancy). | ||
|
|
||
| pub fn r_task<'env, F, T>(f: F) -> T | ||
| where | ||
|
|
@@ -242,9 +235,9 @@ where | |
| let task = QueuedRTask::Sync(RTaskSync { | ||
| fun: closure, | ||
| status_tx: Some(status_tx), | ||
| start_info: RTaskStartInfo::new(false), | ||
| start_info: RTaskStartInfo::new(), | ||
| }); | ||
| INTERRUPT_TASKS.tx().send(task).unwrap(); | ||
| IDLE_ANY_TASKS.tx().send(task).unwrap(); | ||
|
|
||
| // Block until we get the signal that the task has started | ||
| let status = status_rx.recv().unwrap(); | ||
|
|
@@ -289,23 +282,18 @@ where | |
|
|
||
| /// An async task to be run on the R thread. | ||
| /// | ||
| /// Construct via `RTask::interrupt`, `RTask::idle`, or `RTask::idle_any_prompt` | ||
| /// when spawning from the R thread. Use the `Send` variants | ||
| /// (`RTask::send_interrupt`, etc.) when spawning from other threads. | ||
| /// Construct via [RTask::idle] or [RTask::idle_any_prompt] when spawning from the R | ||
| /// thread. Use the `Send` variants ([RTask::send_idle], etc.) when spawning from other | ||
| /// threads. | ||
| /// | ||
| /// For idle modes, console output is automatically captured during the task's | ||
| /// execution via a `ConsoleOutputCapture` passed to the closure. | ||
| /// Console output is automatically captured during the task's execution via a | ||
| /// `ConsoleOutputCapture` passed to the closure. | ||
| pub(crate) enum RTask { | ||
| /// Run at the next interrupt check. Must be spawned from the R thread. | ||
| Interrupt(BoxFuture<'static, ()>), | ||
| /// Run when R is at a top-level idle prompt. Must be spawned from the R thread. | ||
| Idle(BoxFuture<'static, ()>), | ||
| /// Run when R is at any idle prompt (top-level or browser). Must be spawned | ||
| /// from the R thread. | ||
| IdleAnyPrompt(BoxFuture<'static, ()>), | ||
| /// Like `Interrupt`, but can be spawned from any thread. The constructor | ||
| /// enforces `Send` on the closure. | ||
| SendInterrupt(BoxFuture<'static, ()>), | ||
| /// Like `Idle`, but can be spawned from any thread. The constructor | ||
| /// enforces `Send` on the closure. | ||
| SendIdle(BoxFuture<'static, ()>), | ||
|
|
@@ -315,14 +303,6 @@ pub(crate) enum RTask { | |
| } | ||
|
|
||
| impl RTask { | ||
| pub(crate) fn interrupt<F, Fut>(fun: F) -> Self | ||
| where | ||
| F: FnOnce() -> Fut + 'static, | ||
| Fut: Future<Output = ()> + 'static, | ||
| { | ||
| RTask::Interrupt(Box::pin(fun())) | ||
| } | ||
|
|
||
| pub(crate) fn idle<F, Fut>(fun: F) -> Self | ||
| where | ||
| F: FnOnce(ConsoleOutputCapture) -> Fut + 'static, | ||
|
|
@@ -340,90 +320,85 @@ impl RTask { | |
| RTask::IdleAnyPrompt(Self::pin_with_capture(fun)) | ||
| } | ||
|
|
||
| fn pin_with_capture<F, Fut>(fun: F) -> BoxFuture<'static, ()> | ||
| pub(crate) fn send_idle<F, Fut>(fun: F) -> Self | ||
| where | ||
| F: FnOnce(ConsoleOutputCapture) -> Fut + 'static, | ||
| F: FnOnce(ConsoleOutputCapture) -> Fut + 'static + Send, | ||
| Fut: Future<Output = ()> + 'static, | ||
| { | ||
| Box::pin(async move { | ||
| let capture = if Console::is_initialized() { | ||
| Console::get_mut().start_capture() | ||
| } else { | ||
| // Unit tests run without a Console. The dummy capture is | ||
| // inert and doesn't interact with Console state. | ||
| debug_assert!(stdext::IS_TESTING); | ||
| ConsoleOutputCapture::dummy() | ||
| }; | ||
| fun(capture).await | ||
| }) | ||
| RTask::SendIdle(Self::pin_with_capture(fun)) | ||
| } | ||
|
|
||
| pub(crate) fn send_interrupt<F, Fut>(fun: F) -> Self | ||
| /// For rare performance sensitive cases where you'd like to avoid the cost of | ||
| /// poking R options via [Self::pin_with_capture()] and you know you don't need | ||
| /// the safety of capturing output because you aren't running R code | ||
| pub(crate) fn send_idle_without_capture<F, Fut>(fun: F) -> Self | ||
|
Comment on lines
+331
to
+334
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did end up wanting this for |
||
| where | ||
| F: FnOnce() -> Fut + 'static + Send, | ||
| Fut: Future<Output = ()> + 'static, | ||
| { | ||
| RTask::SendInterrupt(Box::pin(fun())) | ||
| RTask::SendIdle(Box::pin(fun())) | ||
| } | ||
|
|
||
| pub(crate) fn send_idle<F, Fut>(fun: F) -> Self | ||
| pub(crate) fn send_idle_any_prompt<F, Fut>(fun: F) -> Self | ||
| where | ||
| F: FnOnce(ConsoleOutputCapture) -> Fut + 'static + Send, | ||
| Fut: Future<Output = ()> + 'static, | ||
| { | ||
| RTask::SendIdle(Self::pin_with_capture(fun)) | ||
| RTask::SendIdleAnyPrompt(Self::pin_with_capture(fun)) | ||
| } | ||
|
|
||
| pub(crate) fn send_idle_any_prompt<F, Fut>(fun: F) -> Self | ||
| fn pin_with_capture<F, Fut>(fun: F) -> BoxFuture<'static, ()> | ||
| where | ||
| F: FnOnce(ConsoleOutputCapture) -> Fut + 'static + Send, | ||
| F: FnOnce(ConsoleOutputCapture) -> Fut + 'static, | ||
| Fut: Future<Output = ()> + 'static, | ||
| { | ||
| RTask::SendIdleAnyPrompt(Self::pin_with_capture(fun)) | ||
| Box::pin(async move { | ||
| let capture = if Console::is_initialized() { | ||
| Console::get_mut().start_capture() | ||
| } else { | ||
| // Unit tests run without a Console. The dummy capture is | ||
| // inert and doesn't interact with Console state. | ||
| debug_assert!(stdext::IS_TESTING); | ||
| ConsoleOutputCapture::dummy() | ||
| }; | ||
| fun(capture).await | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// Spawn an async task on the R thread. | ||
| /// | ||
| /// For `Send` variants (`RTask::send_interrupt`, etc.) this can be called from | ||
| /// For `Send` variants ([RTask::send_idle], etc.) this can be called from | ||
| /// any thread. Non-`Send` variants must be called from the R thread. | ||
| pub(crate) fn spawn(task: RTask) { | ||
| if stdext::IS_TESTING && !Console::is_initialized() { | ||
| let _lock = harp::fixtures::R_TEST_LOCK.lock(); | ||
| let fut = match task { | ||
| RTask::Interrupt(fut) | | ||
| RTask::Idle(fut) | | ||
| RTask::IdleAnyPrompt(fut) | | ||
| RTask::SendInterrupt(fut) | | ||
| RTask::SendIdle(fut) | | ||
| RTask::SendIdleAnyPrompt(fut) => fut, | ||
| }; | ||
| futures::executor::block_on(fut); | ||
| return; | ||
| } | ||
|
|
||
| let needs_r_thread = matches!( | ||
| task, | ||
| RTask::Interrupt(_) | RTask::Idle(_) | RTask::IdleAnyPrompt(_) | ||
| ); | ||
| let needs_r_thread = matches!(task, RTask::Idle(_) | RTask::IdleAnyPrompt(_)); | ||
| if needs_r_thread && !Console::on_main_thread() { | ||
| let thread = std::thread::current(); | ||
| let name = thread.name().unwrap_or("<unnamed>"); | ||
| panic!("`spawn()` must be called from the R thread, not thread '{name}'"); | ||
| } | ||
|
|
||
| let (fut, tasks_tx, only_idle) = match task { | ||
| RTask::Interrupt(fut) | RTask::SendInterrupt(fut) => (fut, INTERRUPT_TASKS.tx(), false), | ||
| RTask::Idle(fut) | RTask::SendIdle(fut) => (fut, IDLE_TASKS.tx(), true), | ||
| RTask::IdleAnyPrompt(fut) | RTask::SendIdleAnyPrompt(fut) => { | ||
| (fut, IDLE_ANY_TASKS.tx(), true) | ||
| }, | ||
| let (fut, tasks_tx) = match task { | ||
| RTask::Idle(fut) | RTask::SendIdle(fut) => (fut, IDLE_TASKS.tx()), | ||
| RTask::IdleAnyPrompt(fut) | RTask::SendIdleAnyPrompt(fut) => (fut, IDLE_ANY_TASKS.tx()), | ||
| }; | ||
|
|
||
| let task = QueuedRTask::Async(RTaskAsync { | ||
| fut, | ||
| tasks_tx: tasks_tx.clone(), | ||
| start_info: RTaskStartInfo::new(only_idle), | ||
| start_info: RTaskStartInfo::new(), | ||
| }); | ||
|
|
||
| tasks_tx.send(task).unwrap(); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
report_crash()moves off needing to use the ui comm!