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

What happens if thread_id gets reused? #956

Open
malkia opened this issue Oct 31, 2024 · 2 comments
Open

What happens if thread_id gets reused? #956

malkia opened this issue Oct 31, 2024 · 2 comments

Comments

@malkia
Copy link

malkia commented Oct 31, 2024

Scenario:

  • Segment stores the thread_id it was created in.
  • The thread is no longer available, but the segment stays still keeping it's number.
  • After really long time, the thread_id gets reused, and as it happens the current thread has this number and wants to free object from the segment above (with the same thread_id)?

Does mimalloc handle this case gracefully?

@daanx
Copy link
Collaborator

daanx commented Nov 2, 2024

Hi @malkia -- ah, no worries -- this is handled well. In particular, when a thread terminates the _mi_thread_done is called which will "abandon" all segments and set their thread-id to 0 (and preventing reused thread-id issues). Btw. we need to keep the segments alive since they contain still live data resulting from pointers that are shared with other threads). Later, other threads can "reclaim" such abandoned segments (see _mi_segment_try_reclaim) and reuse the memory that was abandoned.

@malkia
Copy link
Author

malkia commented Nov 3, 2024

Awesome - thank you so much! This makes it for another re-reading of the source code! Much appreciated!

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

No branches or pull requests

2 participants