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

Remove __pthread_detached_exit in favor of __emscripten_thread_cleanup. NFC. #15605

Merged
merged 3 commits into from
Jan 11, 2022

Conversation

kleisauke
Copy link
Collaborator

Split from PR #13007.

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

Choose a reason for hiding this comment

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

Why this change rather than just removing this handler completely like above? What is the 'exit' message here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The exit -> detachedExit change was mistakenly introduced in PR #12861 (see comment #12861 (comment)), but perhaps it is indeed better to remove this listener completely (since issue #9763 may have already been resolved, test_pthread_create.cpp runs fine on Node).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh I see. Can you split that out into its own fix and mention that is address an accidental change from #12861? Thanks for doing all this work BTW, its much appreciated cleanup.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No problem! I've reverted this change with commit d69ff5b, and will make a new PR with that (or one that removes this listener completely).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just opened PR #19178 for this.

@@ -236,7 +236,7 @@ void _emscripten_thread_exit(void* result) {
// object and we are done.
if (state == DT_DETACHED) {
self->detach_state = DT_EXITED;
__pthread_detached_exit();
__emscripten_thread_cleanup(self);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was thinking about doing this .. but I was slightly worried about free'ing the stack being used by the current thread.

Seems a little dangerous. I don't come up with better solution yet but I was thinking maybe we could instead call this from worker.js? (i.e. once the stack is unwound all the way)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

IIUC, __emscripten_thread_free_data was also called by the detachedExit/__pthread_detached_exit, so this PR should be completely non-functional (except that it now sends the cleanupThread command, which will also eventually call returnWorkerToPool/__emscripten_thread_free_data).

Copy link
Collaborator

@sbc100 sbc100 Nov 23, 2021

Choose a reason for hiding this comment

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

IIUC, In the current code, when __pthread_detached_exit is called the detachedExit command is sent back to the main thread which ends up freeing the data, so the data is not freed on the exit'ing thread at all.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh wait, I see that __emscripten_thread_cleanup also posts a message back to the main thread.. so I agree this should be and NFC. Thanks for clarifying!

@kleisauke
Copy link
Collaborator Author

Is there anything needed to land this? I could do this myself, but I'm a little uncomfortable with merging PRs as an external contributor.

@sbc100 sbc100 merged commit 97d4676 into emscripten-core:main Jan 11, 2022
@kleisauke kleisauke deleted the remove-detached_exit branch January 11, 2022 13: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