Skip to content

Synchronize with destroying spawned tasks more in wasip2 - #14266

Merged
alexcrichton merged 1 commit into
bytecodealliance:mainfrom
alexcrichton:synchronize-more-tasks
Sep 2, 2026
Merged

Synchronize with destroying spawned tasks more in wasip2#14266
alexcrichton merged 1 commit into
bytecodealliance:mainfrom
alexcrichton:synchronize-more-tasks

Conversation

@alexcrichton

Copy link
Copy Markdown
Member

This commit fixes a test failure I'm running into in wasi-libc development where spawned tasks for wasip2 are keeping objects alive in a race condition where sometimes the task is torn down and sometimes it's not. Specifically wasip2 is built on wasip3-style primitives for UDP/TCP which means that futures are used, and if futures aren't immediately ready they're resolved in a spawned Tokio task. This spawned task can interact with UDP, for example, where exclusivity of a UDP socket is tested via Arc::get_mut which will nondeterministically return true or false depending if a previously spawned task has exited or not. In wasi-libc this means that reconnecting a UDP socket sometimes fails and sometimes passes because the background task may or may not have exited.

Here this is resolved by making the drop methods async and then hooking into the preexisting cancel method which aborts the task and then waits on the result. This synchronizes with the task to ensure that the state of the socket is guaranteed to be exclusive after a disconnect and ready for another connect. This similar fix is then applied to ip-name-lookup as well.

@alexcrichton
alexcrichton requested review from a team as code owners September 1, 2026 23:59
@alexcrichton
alexcrichton requested review from pchickey and rvolosatovs and removed request for a team September 1, 2026 23:59
@github-actions github-actions Bot added the wasi Issues pertaining to WASI label Sep 2, 2026

@rvolosatovs rvolosatovs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, however it looks like the ip-name-lookup will need a sync module added for p2

Comment thread crates/wasi/src/p2/host/udp.rs Outdated
Comment thread crates/wasi/src/p2/ip_name_lookup.rs
@alexcrichton
alexcrichton added this pull request to the merge queue Sep 2, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Sep 2, 2026
This commit fixes a test failure I'm running into in wasi-libc
development where spawned tasks for wasip2 are keeping objects alive in
a race condition where sometimes the task is torn down and sometimes
it's not. Specifically wasip2 is built on wasip3-style primitives for
UDP/TCP which means that futures are used, and if futures aren't
immediately ready they're resolved in a spawned Tokio task. This spawned
task can interact with UDP, for example, where exclusivity of a UDP
socket is tested via `Arc::get_mut` which will nondeterministically
return true or false depending if a previously spawned task has exited
or not. In wasi-libc this means that reconnecting a UDP socket sometimes
fails and sometimes passes because the background task may or may not
have exited.

Here this is resolved by making the `drop` methods async and then
hooking into the preexisting `cancel` method which aborts the task and
then waits on the result. This synchronizes with the task to ensure that
the state of the socket is guaranteed to be exclusive after a disconnect
and ready for another connect. This similar fix is then applied to
ip-name-lookup as well.
@alexcrichton
alexcrichton force-pushed the synchronize-more-tasks branch from 667f5c7 to cfbaf82 Compare September 2, 2026 18:02
@alexcrichton
alexcrichton added this pull request to the merge queue Sep 2, 2026
Merged via the queue into bytecodealliance:main with commit 3df636a Sep 2, 2026
54 checks passed
@alexcrichton
alexcrichton deleted the synchronize-more-tasks branch September 2, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasi Issues pertaining to WASI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants