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

Move and better-document waiting for thread exit #2839

Merged
merged 1 commit into from
Apr 5, 2023

Conversation

sporksmith
Copy link
Contributor

@sporksmith sporksmith commented Apr 4, 2023

This moves waiting for thread-exit from Process to ManagedThread, where it more logically belongs. It also adds a check in ManagedThread's Drop implementation to validate that we never drop it while the native thread is still running, and adds documentation about why this is important - to prevent memory corruption due to shared memory regions being deallocated and reused.

This also fixes a bug - Before we weren't waiting for the thread to exit if the clear_child_tid attribute wasn't set. I belive this was a race condition, but in practice I think most higher level thread APIs (e.g. libc's pthread and Rust's std::thread) use this attribute, so wouldn't have wouldn't have been affected.

@sporksmith sporksmith added this to the Code health and maintenance milestone Apr 4, 2023
@sporksmith sporksmith self-assigned this Apr 4, 2023
@github-actions github-actions bot added Component: Documentation In-repository documentation, under docs/ Component: Libraries Support functions like LD_PRELOAD and logging Component: Main Composing the core Shadow executable labels Apr 4, 2023
@sporksmith sporksmith force-pushed the cleanup-thread-exit branch 2 times, most recently from ba13e36 to 79b344b Compare April 4, 2023 20:58
This moves waiting for thread-exit from Process to ManagedThread, where
it more logically belongs. It also adds a check in ManagedThread's Drop
implementation to validate that we never drop it while the native thread
is still running, and adds documentation about why this is important -
to prevent memory corruption due to shared memory regions being
deallocated and reused.

This also fixes a bug - Before we *weren't* waiting for the thread to
exit if the `clear_child_tid` attribute wasn't set. I belive this was a
race condition, but in practice I think most higher level thread APIs
(e.g. libc's `pthread` and Rust's `std::thread`) use this attribute, so
wouldn't have wouldn't have been affected.
@sporksmith
Copy link
Contributor Author

On one last look I realized that while iterating on this I'd somehow dropped the documentation I'd added about why it's important not to drop ManagedThread while the native thread is still running 🤦 . Added this back in and on further reflection changed the drop implementation to always panic if the native thread is still running at drop time.

@sporksmith sporksmith enabled auto-merge (squash) April 5, 2023 14:49
@sporksmith sporksmith merged commit 82d4a79 into shadow:main Apr 5, 2023
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Documentation In-repository documentation, under docs/ Component: Libraries Support functions like LD_PRELOAD and logging Component: Main Composing the core Shadow executable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants