Skip to content
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

Run TLS destructors for wasm32-wasip1-threads #133472

Merged
merged 4 commits into from
Dec 10, 2024
Merged

Conversation

surban
Copy link
Contributor

@surban surban commented Nov 25, 2024

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
Copy link
Collaborator

rustbot commented Nov 25, 2024

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
@surban surban requested a review from joboet December 3, 2024 15:17
@surban
Copy link
Contributor Author

surban commented Dec 3, 2024

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
Copy link
Contributor Author

surban commented Dec 3, 2024

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
Copy link
Contributor Author

surban commented Dec 3, 2024

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
Copy link
Member

jhpratt commented Dec 4, 2024

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
Copy link
Member

joboet commented Dec 4, 2024

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
Copy link
Contributor

bors commented Dec 4, 2024

📌 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
fmease added a commit to fmease/rust that referenced this pull request Dec 4, 2024
Run TLS destructors for wasm32-wasip1-threads

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.
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 5, 2024
Rollup of 6 pull requests

Successful merges:

 - rust-lang#127565 (Teach rustc about the Xtensa VaListImpl)
 - rust-lang#128004 (codegen `#[naked]` functions using global asm)
 - rust-lang#133256 (CI: use free runners for i686-gnu jobs)
 - rust-lang#133472 (Run TLS destructors for wasm32-wasip1-threads)
 - rust-lang#133632 (CI: split x86_64-msvc job)
 - rust-lang#133827 (CI: rfl: move job forward to Linux v6.13-rc1)

r? `@ghost`
`@rustbot` modify labels: rollup
@jieyouxu
Copy link
Member

jieyouxu commented Dec 5, 2024

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
Copy link
Contributor Author

surban commented Dec 5, 2024

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

Should be fixed now.

@surban
Copy link
Contributor Author

surban commented Dec 5, 2024

@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
Copy link
Member

joboet commented Dec 9, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Dec 9, 2024

📌 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
fmease added a commit to fmease/rust that referenced this pull request Dec 9, 2024
Run TLS destructors for wasm32-wasip1-threads

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
fmease added a commit to fmease/rust that referenced this pull request Dec 9, 2024
Run TLS destructors for wasm32-wasip1-threads

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
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 10, 2024
Rollup of 10 pull requests

Successful merges:

 - rust-lang#131558 (Lint on combining `#[no_mangle]` and `#[export_name]`)
 - rust-lang#133122 (Add unpolished, experimental support for AFIDT (async fn in dyn trait))
 - rust-lang#133184 (wasi/fs: Improve stopping condition for <ReadDir as Iterator>::next)
 - rust-lang#133456 (Add licenses + Run `cargo update`)
 - rust-lang#133472 (Run TLS destructors for wasm32-wasip1-threads)
 - rust-lang#133853 (use vendor sources by default on dist tarballs)
 - rust-lang#133946 (coverage: Prefer to visit nodes whose predecessors have been visited)
 - rust-lang#134010 (fix ICE on type error in promoted)
 - rust-lang#134029 (coverage: Use a query to find counters/expressions that must be zero)
 - rust-lang#134071 (Configure renovatebot)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 10, 2024
Rollup of 10 pull requests

Successful merges:

 - rust-lang#131558 (Lint on combining `#[no_mangle]` and `#[export_name]`)
 - rust-lang#133184 (wasi/fs: Improve stopping condition for <ReadDir as Iterator>::next)
 - rust-lang#133456 (Add licenses + Run `cargo update`)
 - rust-lang#133472 (Run TLS destructors for wasm32-wasip1-threads)
 - rust-lang#133853 (use vendor sources by default on dist tarballs)
 - rust-lang#133946 (coverage: Prefer to visit nodes whose predecessors have been visited)
 - rust-lang#134010 (fix ICE on type error in promoted)
 - rust-lang#134029 (coverage: Use a query to find counters/expressions that must be zero)
 - rust-lang#134071 (Configure renovatebot)
 - rust-lang#134102 (Miscellaneous fixes for nix-dev-shell)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ce8d241 into rust-lang:master Dec 10, 2024
6 checks passed
@rustbot rustbot added this to the 1.85.0 milestone Dec 10, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Dec 10, 2024
Rollup merge of rust-lang#133472 - rust-wasi-web:master, r=joboet

Run TLS destructors for wasm32-wasip1-threads

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

6 participants