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

Atomics.wait prevents postMessage being queued #21417

Closed
bmeck opened this issue Jun 19, 2018 · 16 comments
Closed

Atomics.wait prevents postMessage being queued #21417

bmeck opened this issue Jun 19, 2018 · 16 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.

Comments

@bmeck
Copy link
Member

bmeck commented Jun 19, 2018

  • Version: v11.0.0-pre
  • Platform: OSX
  • Subsystem: worker_threads

It appears that workers don't get sent messages if the sending thread uses Atomics.wait to block the thread sending the message.

// main.mjs
import worker from 'worker_threads';
const child = new worker.Worker(new URL('./worker.js', import.meta.url).pathname);
const shared = new Int32Array(new SharedArrayBuffer(4));
child.postMessage({shared});

// removing the following line allows {shared} to be passed to the worker
Atomics.wait(shared, 0, 0);
// worker.js
const {parentPort} = require('worker_threads');
parentPort.on('message', ({shared}) => {
  // never gets here
  console.log('worker got message', shared);
  Atomics.wake(shared, 0);
});
@bmeck bmeck added the worker Issues and PRs related to Worker support. label Jun 19, 2018
@benjamingr
Copy link
Member

I'm not entirely sure this isn't the behaviour I'd expect.

To be clear - you're doing this to prevent access to the ShardArrayBuffer while it is being moved around?

@bmeck
Copy link
Member Author

bmeck commented Jun 20, 2018

@benjamingr in the reduced example above, the main thread should wait on the SharedArrayBuffer being notified that it should wake anyone waiting on it. The shared is posted to the worker prior to wait being called on the main thread. The worker never receives shared.

@addaleax addaleax added esm Issues and PRs related to the ECMAScript Modules implementation. and removed worker Issues and PRs related to Worker support. labels Jun 20, 2018
@addaleax
Copy link
Member

addaleax commented Jun 20, 2018

Okay, looked into it a bit, and the workers thing is a red herring here. Minimal reproduction:

$ echo "console.log('hi')" > foo.js && node --experimental-modules foo.js
(node:14413) ExperimentalWarning: The ESM module loader is experimental.
    at startup (internal/bootstrap/node.js:147:17)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:600:3)

i.e. the main script just doesn’t get executed if it’s file extension is .js.

(In your original example, Atomics.wait() blocks forever because the worker thread doesn’t even get the chance to attach a message handler in the first place.)

@bmeck
Copy link
Member Author

bmeck commented Jun 20, 2018

@addaleax my main script is using .mjs I'm not sure what you mean here. Are you stating that posting to workers is unsafe due to dropped events before they finish loading?

@addaleax
Copy link
Member

@bmeck Try the example in the last comment, I think that it explains the issue better than words can

Are you stating that posting to workers is unsafe due to dropped events before they finish loading?

No – I’m saying it has nothing to do with workers

@bmeck
Copy link
Member Author

bmeck commented Jun 20, 2018

@addaleax I don't understand .

@addaleax
Copy link
Member

addaleax commented Jun 20, 2018

@bmeck If you create a script called foo.js, and its only contents are console.log('hi'), then:

  • node foo.js prints hi
  • node --experimental-modules foo.js does not print hi, it doesn’t actually run the main script if its extension is .js

@bmeck
Copy link
Member Author

bmeck commented Jun 20, 2018

Are you saying that is related to new Worker('/path/to/foo.js')?

I am running node --experimental-modules --experimental-worker ./main.mjs which has main.mjs certainly logging out that it posted the message.

@bmeck
Copy link
Member Author

bmeck commented Jun 20, 2018

I can get the worker to log as well if I remove the line in question above.

@addaleax
Copy link
Member

I think we’re seeing different things because the recently landed 8ee604c caused a regression in ESM that may be masking other issues :/ Will open a revert PR, then dig into it again

@devsnek
Copy link
Member

devsnek commented Jun 20, 2018

@addaleax there's already a pr to fix it: #21350

@addaleax
Copy link
Member

@devsnek Can you land that? It seems like we should still revert the original patch asap if we can’t do that now?

@addaleax
Copy link
Member

Okay, but with that patch applied I can’t reproduce the original problem here anymore … @bmeck Can you share the SHA you’ve built Node for?

@bmeck
Copy link
Member Author

bmeck commented Jun 20, 2018

I'm running a fork of 31d5bde on https://github.com/bmeck/node/tree/service-loader/ I haven't written anything to change the Worker code. let me try and rebase to latest today.

@bmeck
Copy link
Member Author

bmeck commented Jun 21, 2018

I can no longer reproduce this on new builds, closing . https://gist.github.com/bmeck/fb90b23234aee41f392d4a19b61ba6eb is my more complete reduced test for this working properly

@jimmywarting

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

No branches or pull requests

5 participants