Skip to content

Keep the current task per thread on WASIp3 - #1792

Merged
alexcrichton merged 1 commit into
bytecodealliance:mainfrom
tschneidereit:task-fix
Nov 16, 2026
Merged

alexcrichton merged 1 commit into
bytecodealliance:mainfrom
tschneidereit:task-fix

Conversation

@tschneidereit

Copy link
Copy Markdown
Member

wasip3_task_set stored the current task in a plain global. With cooperative threads, a thread that runs while another thread is suspended inside a poll reads and overwrites that thread's current task.

On WASIp3, wasip3_task_set is now defined in the WASIp3 TLS helper, with WASIP3_TASK as _Thread_local, so each thread with its own TLS block has its own current task.

Other targets keep the MVP build with a plain global, since compiling _Thread_local as MVP marks the object as incompatible with shared memory.

wasip3_task_set is a weak symbol, so a component that also links an older version of wit-bindgen can still end up with that version's definition.

`wasip3_task_set` stored the current task in a plain global. With
cooperative threads, a thread that runs while another thread is suspended
inside a poll reads and overwrites that thread's current task.

On WASIp3, `wasip3_task_set` is now defined in the WASIp3 TLS helper, with
`WASIP3_TASK` as `_Thread_local`, so each thread with its own TLS block has
its own current task.

Other targets keep the MVP build with a plain global, since compiling
`_Thread_local` as MVP marks the object as incompatible with shared memory.

`wasip3_task_set` is a weak symbol, so a component that also links an older
version of `wit-bindgen` can still end up with that version's definition.

@alexcrichton alexcrichton 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.

I think this might work if the previous definition got the _Thread_local added, avoiding the need for the duplicate function here. Would that work for your use case?

@tschneidereit

Copy link
Copy Markdown
Member Author

Unfortunately that doesn't work as such, no: wit_bindgen_cabi_wasip3.c is compiled with -mcpu=mvp, _Thread_local would just turn into a plain global anyway. But at the same time it marks the object file as incompatible with shared memory, breaking the wasm32-wasip1-threads target.

I can instead add #ifdef __wasip3__ _Thread_local #endif before void *WASIP3_TASK = NULL; in wit_bindgen_cabi_wasip3.c and compile the file a second time for wasm32-wasip3, linking that object on p3 instead of the MVP one. I'm not sure that's cleaner than this approach though.

@tschneidereit

Copy link
Copy Markdown
Member Author

I should also note that this particular issue isn't blocking my current work. I encountered it while working on a separate fix, and it seemed like something we'll run into soon enough.

@alexcrichton alexcrichton 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.

Good points! And makes sense yeah

@alexcrichton
alexcrichton added this pull request to the merge queue Nov 16, 2026
Merged via the queue into bytecodealliance:main with commit dabfa55 Nov 16, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants