Skip to content

Run TLS destructors for wasm32-wasip1-threads - #133472

Merged
bors merged 4 commits into
rust-lang:masterfrom
rust-wasi-web:master
Dec 10, 2024
Merged

Run TLS destructors for wasm32-wasip1-threads#133472
bors merged 4 commits into
rust-lang:masterfrom
rust-wasi-web:master

Conversation

@surban

@surban surban commented Nov 25, 2024

Copy link
Copy Markdown
Contributor

The target wasm32-wasip1-threads has support for pthreads and allows registration of TLS destructors.

For spawned threads, this registers Rust TLS destructors by creating a pthreads key with an attached destructor function.
For the main thread, this registers an atexit handler to run the TLS destructors.

try-job: test-various

The target has support for pthreads and allows
registration of TLS destructors.
@rustbot

ghost commented Nov 25, 2024

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @jhpratt (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 25, 2024
@surban
surban marked this pull request as ready for review November 25, 2024 20:55
Comment thread library/std/src/sys/thread_local/guard/wasi.rs Outdated
Comment thread library/std/src/sys/thread_local/guard/wasi.rs Outdated
@surban
surban requested a review from joboet December 3, 2024 15:17
@surban

ghost commented Dec 3, 2024

Copy link
Copy Markdown
Contributor Author

As suggested I have switched to using the UNIX thread_local implementation for WASI.

For now I have added the missing libc declarations locally.
What is the way forward here? Shall I open a PR with the missing functions against the libc crate and then update the libc version of std?

@surban

ghost commented Dec 3, 2024

Copy link
Copy Markdown
Contributor Author

Note that the new implementation shows the behavior described in #28129.

However, this is consistent with the behavior of other UNIX platforms. I tested on FreeBSD and there TLS destructors are also not run for the main thread.

@surban

ghost commented Dec 3, 2024

Copy link
Copy Markdown
Contributor Author

I have created follow-up PR #133819 that deals with calling TLS destructors when the main thread exits on UNIX platforms other than Linux.

@jhpratt

ghost commented Dec 4, 2024

Copy link
Copy Markdown
Member

r? @joboet

You seem to have more knowledge of how this works, so feel free to take it over. I don't see anything wrong with the PR, though.

@rustbot rustbot assigned joboet and unassigned jhpratt Dec 4, 2024
@joboet

ghost commented Dec 4, 2024

Copy link
Copy Markdown
Member

As suggested I have switched to using the UNIX thread_local implementation for WASI.

Great, thanks!

For now I have added the missing libc declarations locally.
What is the way forward here? Shall I open a PR with the missing functions against the libc crate and then update the libc version of std?

I'm totally OK with temporarily adding these here. A PR on libc would be appreciated though!

Note that the new implementation shows the behavior described in #28129.

However, this is consistent with the behavior of other UNIX platforms. I tested on FreeBSD and there TLS destructors are also not run for the main thread.

Considering that this is documented as a caveat, I think this is fine for now, especially since this PR is already a big improvement over the status quo.

I have created follow-up PR #133819 that deals with calling TLS destructors when the main thread exits on UNIX platforms other than Linux.

Cool, I'll have a look soon!

@bors r+

@bors

ghost commented Dec 4, 2024

Copy link
Copy Markdown
Collaborator

📌 Commit 4fe15b0 has been approved by joboet

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 4, 2024
@jieyouxu

ghost commented Dec 5, 2024

Copy link
Copy Markdown
Member

I believe this failed in rollup:

  error[E0432]: unresolved import `crate::sys::thread_local::destructors`
    --> library/std/src/sys/thread_local/guard/key.rs:10:9
     |
  10 |     use crate::sys::thread_local::destructors;
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `destructors` in `sys::thread_local`
     |
  note: found an item that was configured out
    --> library/std/src/sys/thread_local/mod.rs:54:16
     |
  54 | pub(crate) mod destructors {
     |                ^^^^^^^^^^^
  note: the item is gated here
    --> library/std/src/sys/thread_local/mod.rs:53:1
     |
  53 | #[cfg(all(target_thread_local, not(all(target_family = "wasm", not(target_feature = "atomics")))))]
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Included the test-various try job in case anyone wants to run it, which would exercise this test.

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 5, 2024
@surban

ghost commented Dec 5, 2024

Copy link
Copy Markdown
Contributor Author

This was caused by not distinguishing between wasm32-wasip1 and wasm32-wasip1-threads.

Should be fixed now.

@surban

ghost commented Dec 5, 2024

Copy link
Copy Markdown
Contributor Author

@rustbot review

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Dec 5, 2024
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 5, 2024
@joboet

ghost commented Dec 9, 2024

Copy link
Copy Markdown
Member

@bors r+

@bors

ghost commented Dec 9, 2024

Copy link
Copy Markdown
Collaborator

📌 Commit 4f16640 has been approved by joboet

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 9, 2024
@bors
bors merged commit ce8d241 into rust-lang:master Dec 10, 2024
@rustbot rustbot added this to the 1.85.0 milestone Dec 10, 2024
target_family = "unix",
),
target_os = "teeos",
all(target_os = "wasi", target_env = "p1", target_feature = "atomics"),

ghost Feb 24, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When users compile to wasm32-wasip1-threads, they don't necessarily enable target_feature = "atomics", but the rust-std of wasm32-wasip1-threads always enables atomics, which causes some issues: #137510.

ghost Feb 24, 2025

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.

Move this to #137510 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants