Skip to content

Conversation

@joboet
Copy link
Member

@joboet joboet commented Oct 23, 2025

The comment about double-free is wrong – we can safely drop both the thread attributes and the thread closure. Here, I've used DropGuard for the attributes and moved the Box::into_raw to just before the pthread_create.

@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 Oct 23, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 23, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Comment on lines +59 to +61
let mut attr = DropGuard::new(&mut attr, |attr| {
assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0)
});
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps you could add a comment here to explain the relevant considerations of using DropGuard here?

Copy link
Member

Choose a reason for hiding this comment

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

One thing I notice is that before, libc::pthread_attr_destroy(attr.as_mut_ptr()) is not called for #[cfg(any(target_os = "espidf", target_os = "nuttx"))], but with this change it will/could be. Is that intended?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that is very much intended. The leakage of the current version is a bug. And do you really think that this needs documentation? The DropGuard just makes sure that the attribute structure is destroyed, which is pretty self-explanatory to me.

Copy link
Member

Choose a reason for hiding this comment

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

The what is clear enough, but the why could use some expanding. If it was completely straightforward, then this bug wouldn't have needed fixing, so I do think explaining why the DropGuard is needed would be good.

Copy link
Member

Choose a reason for hiding this comment

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

IMO, this is fine as-is. The bug existed precisely because the resources (Box and attr) weren't guarded appropriately with DropGuard or equivalent. That's fixed now.

// Round up to the nearest page and try again.
let page_size = os::page_size();
let stack_size =
(stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1);
Copy link
Member

Choose a reason for hiding this comment

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

nit: I wonder if we can make this be stack_size.next_multiple_of(page_size)? I'm not sure why we need to handwrite the code... I guess maybe it's a bit slower since LLVM doesn't know page_size is a multiple of 2, but that seems unimportant in this context?

(Obviously pre-existing...)

@Mark-Simulacrum
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Nov 2, 2025

📌 Commit a7f08de has been approved by Mark-Simulacrum

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 Nov 2, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 2, 2025
…=Mark-Simulacrum

std: don't leak the thread closure if destroying the thread attributes fails

The comment about double-free is wrong – we can safely drop both the thread attributes and the thread closure. Here, I've used `DropGuard` for the attributes and moved the `Box::into_raw` to just before the `pthread_create`.
bors added a commit that referenced this pull request Nov 2, 2025
Rollup of 7 pull requests

Successful merges:

 - #146573 (Constify Range functions)
 - #146699 (Add `is_ascii` function optimized for LoongArch64 for [u8])
 - #148026 (std: don't leak the thread closure if destroying the thread attributes fails)
 - #148135 (Ignore unix socket related tests for VxWorks)
 - #148211 (clippy fixes and code simplification)
 - #148395 (Generalize branch references)
 - #148405 (Fix suggestion when there were a colon already in generics)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 2afb64e into rust-lang:master Nov 3, 2025
11 checks passed
@rustbot rustbot added this to the 1.93.0 milestone Nov 3, 2025
rust-timer added a commit that referenced this pull request Nov 3, 2025
Rollup merge of #148026 - joboet:dont-leak-thread-closure, r=Mark-Simulacrum

std: don't leak the thread closure if destroying the thread attributes fails

The comment about double-free is wrong – we can safely drop both the thread attributes and the thread closure. Here, I've used `DropGuard` for the attributes and moved the `Box::into_raw` to just before the `pthread_create`.
github-actions bot pushed a commit to rust-lang/rust-analyzer that referenced this pull request Nov 3, 2025
Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#146573 (Constify Range functions)
 - rust-lang/rust#146699 (Add `is_ascii` function optimized for LoongArch64 for [u8])
 - rust-lang/rust#148026 (std: don't leak the thread closure if destroying the thread attributes fails)
 - rust-lang/rust#148135 (Ignore unix socket related tests for VxWorks)
 - rust-lang/rust#148211 (clippy fixes and code simplification)
 - rust-lang/rust#148395 (Generalize branch references)
 - rust-lang/rust#148405 (Fix suggestion when there were a colon already in generics)

r? `@ghost`
`@rustbot` modify labels: rollup
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Nov 3, 2025
Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#146573 (Constify Range functions)
 - rust-lang/rust#146699 (Add `is_ascii` function optimized for LoongArch64 for [u8])
 - rust-lang/rust#148026 (std: don't leak the thread closure if destroying the thread attributes fails)
 - rust-lang/rust#148135 (Ignore unix socket related tests for VxWorks)
 - rust-lang/rust#148211 (clippy fixes and code simplification)
 - rust-lang/rust#148395 (Generalize branch references)
 - rust-lang/rust#148405 (Fix suggestion when there were a colon already in generics)

r? `@ghost`
`@rustbot` modify labels: rollup
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Nov 3, 2025
Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#146573 (Constify Range functions)
 - rust-lang/rust#146699 (Add `is_ascii` function optimized for LoongArch64 for [u8])
 - rust-lang/rust#148026 (std: don't leak the thread closure if destroying the thread attributes fails)
 - rust-lang/rust#148135 (Ignore unix socket related tests for VxWorks)
 - rust-lang/rust#148211 (clippy fixes and code simplification)
 - rust-lang/rust#148395 (Generalize branch references)
 - rust-lang/rust#148405 (Fix suggestion when there were a colon already in generics)

r? `@ghost`
`@rustbot` modify labels: rollup
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.

5 participants