Skip to content

Move and better-document waiting for thread exit#2839

Merged
sporksmith merged 1 commit intoshadow:mainfrom
sporksmith:cleanup-thread-exit
Apr 5, 2023
Merged

Move and better-document waiting for thread exit#2839
sporksmith merged 1 commit intoshadow:mainfrom
sporksmith:cleanup-thread-exit

Conversation

@sporksmith
Copy link
Copy Markdown
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
@sporksmith sporksmith requested a review from stevenengler April 4, 2023 21:16
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 force-pushed the cleanup-thread-exit branch from 79b344b to 44ada8c Compare April 5, 2023 14:45
@sporksmith
Copy link
Copy Markdown
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
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.

2 participants