Skip to content

Don't proxy entire abort function back to the main thread #14911

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

Merged
merged 1 commit into from
Aug 19, 2021

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Aug 19, 2021

It turns out proxying abort has several issues:

  1. If we do stuff like proxing during the call to abort
    it can allocate stack and heap space which might not
    be possible by the time abort is called.
  2. All the other calls to abort that don't go via the library
    function (calls to abort directly from JS) are not proxied
    meaning we get inconsistent proxying.

Instead I use postMessage to send just the onAbort and do
the rest of the abort in the local thread.

The exception will end up triggering the worker.onerror so
go entire application will still get torn down.

@sbc100 sbc100 force-pushed the dont_proxy_abort branch 2 times, most recently from 4d6ed8b to f9b13cb Compare August 19, 2021 05:14
@sbc100 sbc100 requested a review from kripken August 19, 2021 05:14
@sbc100 sbc100 changed the title Don't proxy abort back to the main thread Don't proxy entire abort function back to the main thread Aug 19, 2021
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

Is this needed? As we proxy exceptions to the main thread, wouldn't just throwing an exception as abort does do all we need?

@sbc100
Copy link
Collaborator Author

sbc100 commented Aug 19, 2021

The use case in #11345 specifically requires that onAbort is called.

In many cases the Module object that that includes the onAbort function only exists on the main thread.

@@ -445,9 +445,6 @@ LibraryManager.library = {
// TODO: There are currently two abort() functions that get imported to asm module scope: the built-in runtime function abort(),
// and this function _abort(). Remove one of these, importing two functions for the same purpose is wasteful.
abort__sig: 'v',
// Proxy synchronously, which will have the effect of halting the program
// and killing all threads, including this one.
abort__proxy: 'sync',
abort: function() {
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment here, maybe

// abort() must be proxied, but we avoid the normal method since
// the runtime may not be in a good enough state to support that.
// Instead, it is proxied using postMessage, see library_pthread.js.

Copy link
Collaborator Author

@sbc100 sbc100 Aug 19, 2021

Choose a reason for hiding this comment

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

Its not that abort needs to be proxied its just the onAbort callback. I will add a comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(Is there an existing test for this?)

Yes, the test I added back in #14669 tests exactly this. It injects the onAbort handler during preInit which means that handler will only exist on the main thread and therefore must be invoked there:

emscripten/tests/test_core.py

Lines 2356 to 2360 in 8cbca51

@node_pthreads
def test_pthread_abort(self):
self.set_setting('PROXY_TO_PTHREAD')
self.add_pre_run("Module.onAbort = function() { console.log('onAbort called'); }")
self.do_run_in_out_file_test('pthread/test_pthread_abort.c', assert_returncode=NON_ZERO)

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good.

@kripken
Copy link
Member

kripken commented Aug 19, 2021

(Is there an existing test for this?)

It turns out proxying abort has several issues:

1. If we do stuff like proxing during the call to abort
   it can allocate stack and heap space which might not
   be possible by the time abort is called.
2. All the other calls to abort that don't go via the library
   function (calls to abort directly from JS) are not proxied
   meaning we get inconsisten proxying.

Instead I use postMessage to send just the onAbort and do
the rest of the abort in the local thread.

The exception will end up triggering the worker.onerror so
go entire application will still get torn down.
@sbc100 sbc100 enabled auto-merge (squash) August 19, 2021 20:14
@sbc100 sbc100 merged commit d23597a into main Aug 19, 2021
@sbc100 sbc100 deleted the dont_proxy_abort branch August 19, 2021 21:29
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