-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
feat: optional async ops 2 #3721
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
71fc6e7
feat: optional async ops
kt3k d1933d5
docs: add a todo comment
kt3k 35bbdee
refactor: use AsyncOptional enum variant
kt3k 985bc40
refactor: restore previous version of pending_ops
kt3k ee6cd48
refactor: address review feedbacks
kt3k dacba75
fix comment style
ry 71f28b8
comment
ry 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 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 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 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 |
---|---|---|
|
@@ -16,6 +16,7 @@ use crate::shared_queue::SharedQueue; | |
use crate::shared_queue::RECOMMENDED_SIZE; | ||
use futures::future::FutureExt; | ||
use futures::future::TryFutureExt; | ||
use futures::stream::select; | ||
use futures::stream::FuturesUnordered; | ||
use futures::stream::StreamExt; | ||
use futures::task::AtomicWaker; | ||
|
@@ -178,6 +179,7 @@ pub struct Isolate { | |
needs_init: bool, | ||
pub(crate) shared: SharedQueue, | ||
pending_ops: FuturesUnordered<PendingOpFuture>, | ||
pending_unref_ops: FuturesUnordered<PendingOpFuture>, | ||
have_unpolled_ops: bool, | ||
startup_script: Option<OwnedScript>, | ||
pub op_registry: Arc<OpRegistry>, | ||
|
@@ -340,6 +342,7 @@ impl Isolate { | |
shared, | ||
needs_init, | ||
pending_ops: FuturesUnordered::new(), | ||
pending_unref_ops: FuturesUnordered::new(), | ||
have_unpolled_ops: false, | ||
startup_script, | ||
op_registry: Arc::new(OpRegistry::new()), | ||
|
@@ -517,6 +520,12 @@ impl Isolate { | |
self.have_unpolled_ops = true; | ||
None | ||
} | ||
Op::AsyncUnref(fut) => { | ||
let fut2 = fut.map_ok(move |buf| (op_id, buf)); | ||
self.pending_unref_ops.push(fut2.boxed()); | ||
self.have_unpolled_ops = true; | ||
None | ||
} | ||
} | ||
} | ||
|
||
|
@@ -711,7 +720,9 @@ impl Future for Isolate { | |
// Now handle actual ops. | ||
inner.have_unpolled_ops = false; | ||
#[allow(clippy::match_wild_err_arm)] | ||
match inner.pending_ops.poll_next_unpin(cx) { | ||
match select(&mut inner.pending_ops, &mut inner.pending_unref_ops) | ||
.poll_next_unpin(cx) | ||
{ | ||
Comment on lines
+723
to
+725
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. Very elegant |
||
Poll::Ready(Some(Err(_))) => panic!("unexpected op error"), | ||
Poll::Ready(None) => break, | ||
Poll::Pending => break, | ||
|
@@ -814,6 +825,7 @@ pub mod tests { | |
|
||
pub enum Mode { | ||
Async, | ||
AsyncUnref, | ||
OverflowReqSync, | ||
OverflowResSync, | ||
OverflowReqAsync, | ||
|
@@ -836,6 +848,17 @@ pub mod tests { | |
let buf = vec![43u8, 0, 0, 0].into_boxed_slice(); | ||
Op::Async(futures::future::ok(buf).boxed()) | ||
} | ||
Mode::AsyncUnref => { | ||
assert_eq!(control.len(), 1); | ||
assert_eq!(control[0], 42); | ||
let fut = async { | ||
// This future never finish. | ||
futures::future::pending::<()>().await; | ||
let buf = vec![43u8, 0, 0, 0].into_boxed_slice(); | ||
Ok(buf) | ||
}; | ||
Op::AsyncUnref(fut.boxed()) | ||
} | ||
Mode::OverflowReqSync => { | ||
assert_eq!(control.len(), 100 * 1024 * 1024); | ||
let buf = vec![43u8, 0, 0, 0].into_boxed_slice(); | ||
|
@@ -953,6 +976,31 @@ pub mod tests { | |
}); | ||
} | ||
|
||
#[test] | ||
fn test_poll_async_optional_ops() { | ||
run_in_task(|cx| { | ||
let (mut isolate, dispatch_count) = setup(Mode::AsyncUnref); | ||
js_check(isolate.execute( | ||
"check1.js", | ||
r#" | ||
Deno.core.setAsyncHandler(1, (buf) => { | ||
// This handler will never be called | ||
assert(false); | ||
}); | ||
let control = new Uint8Array([42]); | ||
Deno.core.send(1, control); | ||
"#, | ||
)); | ||
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1); | ||
// The above op never finish, but isolate can finish | ||
// because the op is an unreffed async op. | ||
assert!(match isolate.poll_unpin(cx) { | ||
Poll::Ready(Ok(_)) => true, | ||
_ => false, | ||
}); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn terminate_execution() { | ||
let (tx, rx) = std::sync::mpsc::channel::<bool>(); | ||
|
This file contains 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
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.
Sorry. I misread your comment!