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

Clarify the guarantees that ThreadId does and doesn't make. #84083

Merged
merged 2 commits into from
Jan 2, 2022
Merged

Clarify the guarantees that ThreadId does and doesn't make. #84083

merged 2 commits into from
Jan 2, 2022

Conversation

ltratt
Copy link
Contributor

@ltratt ltratt commented Apr 11, 2021

The existing documentation does not spell out whether ThreadIds are unique during the lifetime of a thread or of a process. I had to examine the source code to realise (pleasingly!) that they're unique for the lifetime of a process. That seems worth documenting clearly, as it's a strong guarantee.

Examining the way ThreadIds are created also made me realise that the as_u64 method on ThreadId could be a trap for the unwary on those platforms where the platform's notion of a thread identifier is also a 64 bit integer (particularly if they happen to use a similar identifier scheme to ThreadId). I therefore think it's worth being even clearer that there's no relationship between the two.

The existing documentation does not spell out whether `ThreadId`s are unique
during the lifetime of a thread or of a process. I had to examine the source
code to realise (pleasingly!) that they're unique for the lifetime of a process.
That seems worth documenting clearly, as it's a strong guarantee.

Examining the way `ThreadId`s are created also made me realise that the `as_u64`
method on `ThreadId` could be a trap for the unwary on those platforms where the
platform's notion of a thread identifier is also a 64 bit integer (particularly
if they happen to use a similar identifier scheme to `ThreadId`). I therefore
think it's worth being even clearer that there's no relationship between the
two.
@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 11, 2021
@Mark-Simulacrum
Copy link
Member

r? @joshtriplett as we'll want a T-libs FCP on the new guarantees, but there's also relevant discussion in #67939 which may make us want to avoid making additional guarantees.

@camelid camelid added A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. labels Apr 30, 2021
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 22, 2021
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 11, 2021
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 9, 2021
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 31, 2021
@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 15, 2021
@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 8, 2021
@joshtriplett
Copy link
Member

I thought, based on the previous discussion, that we'd landed on not making these guarantees, so that we could just use the OS thread ID?

@ltratt
Copy link
Contributor Author

ltratt commented Oct 11, 2021

I think I'm still of the same mind on this issue as when I wrote #67939 (comment): it doesn't really matter what we choose to do, IMHO, so long as we change (at least) one of the code and documentation (on the basis that the status quo is going to lead people astray).

@joshtriplett
Copy link
Member

I've found myself increasingly enamored of more robust techniques to make Rust's standard library not care what environment it's called from. As a result, I like the idea of using the OS thread ID where possible, rather than inventing our own.

@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 5, 2021
@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 10, 2021
@cuviper
Copy link
Member

cuviper commented Dec 15, 2021

Put another way, if we allow the possibility of using OS thread IDs, then I don't think there's anything much that one can safely do with thread IDs in Rust

I see that not necessarily for users' sake, but for implementations to avoid their own atomic thread counter.

@danielhenrymantilla
Copy link
Contributor

Worthy of consideration:

are two crates which rely on ThreadId unicity to be sound. I know I've personally written code that makes that assumption in the past, for experimental crates or whatnot which I didn't publish to crates.io.


All this to say that:

  • ThreadId not being unique during the lifetime of a process is a footgun for those that may not be aware of this; a soundness-endangering one!
  • it could lead to "reuse" race conditions such as those for pids on Unix;
  • the solution is simple to implement / circumvent, see dtolnay/syn@964cd08 for an example of this.

This means that if the std library does not guarantee this, the ecosystem will be split between people unaware of the issue doing The Wrong Thing™, and people reimplementing it themselves, inefficiently for OSes that actually gave stronger guarantees, and redundantly in between implementations, and even potentially incorrectly w.r.t. overflows.

The standard library, on the other hand, to guarantee this, would just need a fallback module for this implementation, written once and correctly, that would only be pulled for the platforms unable to feature something better.

So, while I respect the idea of reducing the maintenance burden for the standard library by not requiring cfg-gated modules to meet these criteria, efficiently for the OSes that support it, and semantically correctly for the others, @joshtriplett, I do think the situation, for this very issue, is worthy of exceptional treatment.

  • (Or, as a fallback, a nursery crate to provide a canonical "lib" implementation)

@joshtriplett
Copy link
Member

We discussed this again in today's @rust-lang/libs-api meeting. I decided to withdraw my objection, since you can only get the ID for a random non-Rust thread via thread::current, and we already have things like thread names that require thread-local storage (at least for lazy initialization).

@rfcbot merge

@rfcbot
Copy link

rfcbot commented Dec 22, 2021

Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Dec 22, 2021
@rfcbot
Copy link

rfcbot commented Dec 22, 2021

🔔 This is now entering its final comment period, as per the review above. 🔔

/// method on a [`Thread`].
/// A `ThreadId` is an opaque object that uniquely identifies each thread
/// created during the lifetime of a process. `ThreadId`s are guaranteed not to
/// be reused, even if a thread dies. `ThreadId`s are under the control of
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// be reused, even if a thread dies. `ThreadId`s are under the control of
/// be reused, even after thread terminates. `ThreadId`s are under the control of

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've used a minor variant of this in d66a9e1.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Jan 1, 2022
@rfcbot
Copy link

rfcbot commented Jan 1, 2022

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Jan 1, 2022
@dtolnay
Copy link
Member

dtolnay commented Jan 1, 2022

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jan 1, 2022

📌 Commit d66a9e1 has been approved by dtolnay

@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 Jan 1, 2022
@dtolnay dtolnay assigned dtolnay and unassigned joshtriplett Jan 1, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 2, 2022
…askrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#84083 (Clarify the guarantees that ThreadId does and doesn't make.)
 - rust-lang#91593 (Remove unnecessary bounds for some Hash{Map,Set} methods)
 - rust-lang#92297 (Reduce compile time of rustbuild)
 - rust-lang#92332 (Add test for where clause order)
 - rust-lang#92438 (Enforce formatting for rustc_codegen_cranelift)
 - rust-lang#92463 (Remove pronunciation guide from Vec<T>)
 - rust-lang#92468 (Emit an error for `--cfg=)`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 30ec1f0 into rust-lang:master Jan 2, 2022
@rustbot rustbot added this to the 1.59.0 milestone Jan 2, 2022
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Jan 6, 2022
@ltratt ltratt deleted the threadid_doc_tweak branch February 9, 2022 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. I-needs-decision Issue: In need of a decision. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.