Skip to content

Free thread data in native code. NFC #15532

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

Closed
wants to merge 1 commit into from

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Nov 16, 2021

We free the thread data either when the thread exits (in the case it is
detached) or when it is joined (in the case it is not detached).

In either case we can re-cycle the worker (return it to the worker pool)
right away since the thread is no longer running.

Since freeThreadData is no longer called during returnWorkerToPool
it means that pthread data accocated with threads that were terminated
with worker.terminate will not be memory leaks. However terminating a
thread in this was otherwise racey and dangerous and we should try to
avoid worker.terminate if at all possible. Also, pthread data is
already excluded from lsan so this won't show up in leak reports.

Thread data is now free'd in native code and no longer requires a
postMessage back to the main thread.  We free the thread data either
when the thread exits (in the case it is detached) or when it is joined
(in the case is not detached).

In either case we can re-cycle the worker (return it to the worker pool)
right away since the thread is no longer running.

Since `freeThreadData` is no longer called during `returnWorkerToPool`
it means that pthreads that are terminated asyncronously (with
prejudice) will not leak their thread data.  However terminating a
thread in this way (using worker.terminate()) is already racey and
dangerous since we don't know what state the diing thread is in, or
exactly when it is safe to free its data.
@sbc100 sbc100 requested a review from kleisauke November 16, 2021 00:19
Copy link
Collaborator

@kleisauke kleisauke left a comment

Choose a reason for hiding this comment

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

Looks good to me, but there are a couple of test failures that need to be investigated further.

Added a small comment regarding that exit event.

@@ -311,7 +304,7 @@ var LibraryPThread = {
worker.on('error', function(e) {
worker.onerror(e);
});
worker.on('detachedExit', function() {
worker.on('threadDone', function() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
worker.on('threadDone', function() {
worker.on('exit', function() {

See: #12861 (comment).

@sbc100
Copy link
Collaborator Author

sbc100 commented Nov 17, 2021

I think i'm going to try to make an simpler NFC version of this change.

@sbc100 sbc100 closed this Nov 17, 2021
@sbc100 sbc100 deleted the free_thread_data_in_native_code branch November 30, 2021 04:43
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

Successfully merging this pull request may close these issues.

2 participants