Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit f0f7c38

Browse files
committed
draft task.cancel and subtask.cancel implementations
Tests to follow in a subsequent commit. This includes some refactoring to reuse the code for aborting spawned tasks in the case of guest->host calls. Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 0016cd2 commit f0f7c38

14 files changed

Lines changed: 343 additions & 208 deletions

File tree

Cargo.lock

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,18 +595,18 @@ lto = true
595595

596596
# TODO: remove this once we've switched to a wasm-tools/wit-bindgen release:
597597
[patch.crates-io]
598-
wasmparser = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
599-
wat = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
600-
wast = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
601-
wasmprinter = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
602-
wasm-encoder = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
603-
wasm-smith = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
604-
wasm-mutate = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
605-
wit-parser = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
606-
wit-component = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
607-
wasm-wave = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
608-
wasm-compose = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
609-
wasm-metadata = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
598+
wasmparser = { git = "https://github.com/bytecodealliance/wasm-tools" }
599+
wat = { git = "https://github.com/bytecodealliance/wasm-tools" }
600+
wast = { git = "https://github.com/bytecodealliance/wasm-tools" }
601+
wasmprinter = { git = "https://github.com/bytecodealliance/wasm-tools" }
602+
wasm-encoder = { git = "https://github.com/bytecodealliance/wasm-tools" }
603+
wasm-smith = { git = "https://github.com/bytecodealliance/wasm-tools" }
604+
wasm-mutate = { git = "https://github.com/bytecodealliance/wasm-tools" }
605+
wit-parser = { git = "https://github.com/bytecodealliance/wasm-tools" }
606+
wit-component = { git = "https://github.com/bytecodealliance/wasm-tools" }
607+
wasm-wave = { git = "https://github.com/bytecodealliance/wasm-tools" }
608+
wasm-compose = { git = "https://github.com/bytecodealliance/wasm-tools" }
609+
wasm-metadata = { git = "https://github.com/bytecodealliance/wasm-tools" }
610610
wit-bindgen = { git = "https://github.com/bytecodealliance/witx-bindgen" }
611611
wit-bindgen-rt = { git = "https://github.com/bytecodealliance/witx-bindgen" }
612612
wit-bindgen-rust-macro = { git = "https://github.com/bytecodealliance/witx-bindgen" }

crates/environ/src/component.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ macro_rules! foreach_builtin_component_function {
106106
#[cfg(feature = "component-model-async")]
107107
subtask_cancel(vmctx: vmctx, caller_instance: u32, async_: u8, task_id: u32) -> u64;
108108
#[cfg(feature = "component-model-async")]
109-
sync_enter(vmctx: vmctx, memory: ptr_u8, start: ptr_u8, return_: ptr_u8, caller_instance: u32, task_return_type: u32, string_encoding: u32, result_count: u32, storage: ptr_u8, storage_len: size) -> bool;
109+
sync_enter(vmctx: vmctx, memory: ptr_u8, start: ptr_u8, return_: ptr_u8, caller_instance: u32, callee_instance: u32, task_return_type: u32, string_encoding: u32, result_count: u32, storage: ptr_u8, storage_len: size) -> bool;
110110
#[cfg(feature = "component-model-async")]
111-
sync_exit(vmctx: vmctx, callback: ptr_u8, caller_instance: u32, callee: ptr_u8, callee_instance: u32, param_count: u32, storage: ptr_u8, storage_len: size) -> bool;
111+
sync_exit(vmctx: vmctx, callback: ptr_u8, callee: ptr_u8, param_count: u32, storage: ptr_u8, storage_len: size) -> bool;
112112
#[cfg(feature = "component-model-async")]
113-
async_enter(vmctx: vmctx, memory: ptr_u8, start: ptr_u8, return_: ptr_u8, caller_instance: u32, task_return_type: u32, string_encoding: u32, params: u32, results: u32) -> bool;
113+
async_enter(vmctx: vmctx, memory: ptr_u8, start: ptr_u8, return_: ptr_u8, caller_instance: u32, callee_instance: u32, task_return_type: u32, string_encoding: u32, params: u32, results: u32) -> bool;
114114
#[cfg(feature = "component-model-async")]
115-
async_exit(vmctx: vmctx, callback: ptr_u8, post_return: ptr_u8, caller_instance: u32, callee: ptr_u8, callee_instance: u32, param_count: u32, result_count: u32, flags: u32) -> u64;
115+
async_exit(vmctx: vmctx, callback: ptr_u8, post_return: ptr_u8, callee: ptr_u8, param_count: u32, result_count: u32, flags: u32) -> u64;
116116
#[cfg(feature = "component-model-async")]
117117
future_new(vmctx: vmctx, ty: u32) -> u64;
118118
#[cfg(feature = "component-model-async")]

crates/environ/src/fact.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub static SYNC_ENTER_FIXED_PARAMS: &[ValType] = &[
5555
ValType::I32,
5656
ValType::I32,
5757
ValType::I32,
58+
ValType::I32,
5859
];
5960

6061
/// Representation of an adapter module.
@@ -527,10 +528,9 @@ impl<'a> Module<'a> {
527528
callback: Option<FuncIndex>,
528529
results: &[ValType],
529530
) -> FuncIndex {
530-
let ty = self.core_types.function(
531-
&[ValType::I32, ValType::FUNCREF, ValType::I32, ValType::I32],
532-
results,
533-
);
531+
let ty = self
532+
.core_types
533+
.function(&[ValType::FUNCREF, ValType::I32], results);
534534
self.core_imports.import(
535535
"sync",
536536
&format!("[exit-call]{suffix}"),
@@ -558,6 +558,7 @@ impl<'a> Module<'a> {
558558
ValType::I32,
559559
ValType::I32,
560560
ValType::I32,
561+
ValType::I32,
561562
],
562563
&[],
563564
Import::AsyncEnterCall {
@@ -585,14 +586,7 @@ impl<'a> Module<'a> {
585586
self.import_simple_get_and_set(
586587
"async",
587588
&format!("[exit-call]{suffix}"),
588-
&[
589-
ValType::I32,
590-
ValType::FUNCREF,
591-
ValType::I32,
592-
ValType::I32,
593-
ValType::I32,
594-
ValType::I32,
595-
],
589+
&[ValType::FUNCREF, ValType::I32, ValType::I32, ValType::I32],
596590
&[ValType::I32],
597591
Import::AsyncExitCall {
598592
callback: callback

crates/environ/src/fact/trampoline.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ impl<'a, 'b> Compiler<'a, 'b> {
436436
self.instruction(I32Const(
437437
i32::try_from(adapter.lower.instance.as_u32()).unwrap(),
438438
));
439+
self.instruction(I32Const(
440+
i32::try_from(adapter.lift.instance.as_u32()).unwrap(),
441+
));
439442
self.instruction(I32Const(
440443
i32::try_from(self.types[adapter.lift.ty].results.as_u32()).unwrap(),
441444
));
@@ -463,13 +466,7 @@ impl<'a, 'b> Compiler<'a, 'b> {
463466
format!("[adapter-callee]{}", adapter.name),
464467
));
465468

466-
self.instruction(I32Const(
467-
i32::try_from(adapter.lower.instance.as_u32()).unwrap(),
468-
));
469469
self.instruction(RefFunc(adapter.callee.as_u32()));
470-
self.instruction(I32Const(
471-
i32::try_from(adapter.lift.instance.as_u32()).unwrap(),
472-
));
473470
self.instruction(I32Const(param_count));
474471
// The result count for an async callee is either one (if there's a
475472
// callback) or zero (if there's no callback). We conservatively use
@@ -519,6 +516,9 @@ impl<'a, 'b> Compiler<'a, 'b> {
519516
self.instruction(I32Const(
520517
i32::try_from(adapter.lower.instance.as_u32()).unwrap(),
521518
));
519+
self.instruction(I32Const(
520+
i32::try_from(adapter.lift.instance.as_u32()).unwrap(),
521+
));
522522
self.instruction(I32Const(
523523
i32::try_from(self.types[adapter.lift.ty].results.as_u32()).unwrap(),
524524
));
@@ -561,13 +561,7 @@ impl<'a, 'b> Compiler<'a, 'b> {
561561
format!("[adapter-callee]{}", adapter.name),
562562
));
563563

564-
self.instruction(I32Const(
565-
i32::try_from(adapter.lower.instance.as_u32()).unwrap(),
566-
));
567564
self.instruction(RefFunc(adapter.callee.as_u32()));
568-
self.instruction(I32Const(
569-
i32::try_from(adapter.lift.instance.as_u32()).unwrap(),
570-
));
571565
self.instruction(I32Const(lift_param_count));
572566
self.instruction(Call(exit.as_u32()));
573567

@@ -608,6 +602,9 @@ impl<'a, 'b> Compiler<'a, 'b> {
608602
self.instruction(I32Const(
609603
i32::try_from(adapter.lower.instance.as_u32()).unwrap(),
610604
));
605+
self.instruction(I32Const(
606+
i32::try_from(adapter.lift.instance.as_u32()).unwrap(),
607+
));
611608
self.instruction(I32Const(
612609
i32::try_from(self.types[adapter.lift.ty].results.as_u32()).unwrap(),
613610
));
@@ -626,13 +623,7 @@ impl<'a, 'b> Compiler<'a, 'b> {
626623
format!("[adapter-callee]{}", adapter.name),
627624
));
628625

629-
self.instruction(I32Const(
630-
i32::try_from(adapter.lower.instance.as_u32()).unwrap(),
631-
));
632626
self.instruction(RefFunc(adapter.callee.as_u32()));
633-
self.instruction(I32Const(
634-
i32::try_from(adapter.lift.instance.as_u32()).unwrap(),
635-
));
636627
self.instruction(I32Const(param_count));
637628
self.instruction(I32Const(result_count));
638629
self.instruction(I32Const(0));

crates/wasi-http/src/p3/host/handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ where
224224
});
225225
let body = empty_body().with_trailers(OutgoingRequestTrailers {
226226
trailers: Some(trailers_rx),
227-
trailer_task: task.abort_handle(),
227+
trailer_task: task.abort_on_drop_handle(),
228228
});
229229
let request = http::Request::from_parts(request, body);
230230
match client.send_request(request, options).await? {
@@ -264,7 +264,7 @@ where
264264
let body = OutgoingRequestBody::new(contents, buffer, content_length)
265265
.with_trailers(OutgoingRequestTrailers {
266266
trailers: Some(trailers_rx),
267-
trailer_task: task.abort_handle(),
267+
trailer_task: task.abort_on_drop_handle(),
268268
});
269269
let request = http::Request::from_parts(request, body);
270270
match client.send_request(request, options).await? {

crates/wasi-http/src/p3/host/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ where
927927
trailers_tx,
928928
});
929929
let req = get_request_mut(view.table(), &req)?;
930-
req.task = Some(task.abort_handle());
930+
req.task = Some(task.abort_on_drop_handle());
931931
Ok(Ok((contents_rx.into(), trailers_rx.into())))
932932
})
933933
}
@@ -1150,7 +1150,7 @@ where
11501150
trailers_tx,
11511151
});
11521152
let res = get_response_mut(view.table(), &res)?;
1153-
res.body_task = Some(task.abort_handle());
1153+
res.body_task = Some(task.abort_on_drop_handle());
11541154
Ok(Ok((contents_rx.into(), trailers_rx.into())))
11551155
})
11561156
}

crates/wasi/src/p3/filesystem/host.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ where
112112
let id = {
113113
let mut tasks = tasks.lock().map_err(|_| anyhow!("lock poisoned"))?;
114114
tasks
115-
.push(task.abort_handle())
115+
.push(task.abort_on_drop_handle())
116116
.context("failed to push task to table")?
117117
};
118118
view.spawn(ReadTask {
@@ -399,7 +399,7 @@ where
399399
let id = {
400400
let mut tasks = tasks.lock().map_err(|_| anyhow!("lock poisoned"))?;
401401
tasks
402-
.push(task.abort_handle())
402+
.push(task.abort_on_drop_handle())
403403
.context("failed to push task to table")?
404404
};
405405
view.spawn(ReadTask {

crates/wasi/src/p3/sockets/host/types/tcp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ where
320320
} = get_socket_mut(view.table(), &socket)?;
321321
*tcp_state = TcpState::Listening {
322322
listener,
323-
task: task.abort_handle(),
323+
task: task.abort_on_drop_handle(),
324324
};
325325
Ok(Ok((
326326
rx,
@@ -487,7 +487,7 @@ where
487487
else {
488488
bail!("corrupted socket state");
489489
};
490-
*rx_task = Some(task.abort_handle());
490+
*rx_task = Some(task.abort_on_drop_handle());
491491
}
492492
_ => {
493493
let fut = res_tx.write(Err(ErrorCode::InvalidState));

0 commit comments

Comments
 (0)