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

fix(ext/node): MessagePort works #22999

Merged
merged 15 commits into from
Apr 2, 2024
Merged

Conversation

satyarohith
Copy link
Member

@satyarohith satyarohith commented Mar 20, 2024

Work in progress.

TODO:

  • fix the order of events generated
  • add a test

Closes #22951
Closes #23001

@bartlomieju
Copy link
Member

@satyarohith do you need any help with landing this PR? Would be great if we could land it before v1.42 this week.

@satyarohith
Copy link
Member Author

@satyarohith do you need any help with landing this PR? Would be great if we could land it before v1.42 this week.

Yes. I'm trying to figure out how close event would be emitted when either of the ports get closed.

@bartlomieju
Copy link
Member

@satyarohith do you need any help with landing this PR? Would be great if we could land it before v1.42 this week.

Yes. I'm trying to figure out how close event would be emitted when either of the ports get closed.

I think you can override the MessagePort::close to emit the proper event?

@bartlomieju
Copy link
Member

Additionally you can emit the event in MessagePort::start method when the Interrupted error is thrown (that tells that the other side has been closed).

To do that you probably want to add a private API that allows to call a callback in either of these situations - you'd install the callback if the MessagePort is created from the Node polyfills.

This patch allows the example code to work but theoritically close() should
emit "close" event on both the ports of a channel
(see https://nodejs.org/api/worker_threads.html#portclose) but that's not being
done here. We need to address that.
@satyarohith satyarohith marked this pull request as ready for review March 25, 2024 07:16
@satyarohith satyarohith changed the title fix(ext/node): MessagePort works [WIP] fix(ext/node): MessagePort works Mar 25, 2024
@satyarohith
Copy link
Member Author

This PR is working but the order of events fired is not similar to node:

➜  tst git:(fix_message_port_node_compat) ✗ node a.mjs        
worker: Hello from worker on parentPort!
mainPort: Hello from worker on workerPort!
worker port closed
mainPort closed
➜  tst git:(fix_message_port_node_compat) ✗ denod run -A a.mjs
worker: Hello from worker on parentPort!
worker port closed
mainPort: Hello from worker on workerPort!
mainPort closed

And according to https://nodejs.org/api/worker_threads.html#portclose, we need to emit "close" event on both the ports of a channel when either of the port is closed which is not happening right now.

@satyarohith satyarohith changed the title [WIP] fix(ext/node): MessagePort works fix(ext/node): MessagePort works Mar 25, 2024
ext/web/13_message_port.js Outdated Show resolved Hide resolved
ext/web/13_message_port.js Outdated Show resolved Hide resolved
ext/web/13_message_port.js Outdated Show resolved Hide resolved
ext/web/13_message_port.js Outdated Show resolved Hide resolved
ext/web/13_message_port.js Outdated Show resolved Hide resolved
ext/web/13_message_port.js Outdated Show resolved Hide resolved
ext/web/13_message_port.js Outdated Show resolved Hide resolved
Comment on lines +130 to +134
itest_flaky!(node_worker_message_port {
args: "run --quiet --allow-read workers/node_worker_message_port.mjs",
output: "workers/node_worker_message_port.mjs.out",
exit_code: 0,
});
Copy link
Member

Choose a reason for hiding this comment

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

So it passes sometimes?

Copy link
Member Author

@satyarohith satyarohith Apr 2, 2024

Choose a reason for hiding this comment

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

I marked it as flaky because the order is not predictable. But it doesn't match the output of Node even in a single instance.

runtime/js/99_main.js Outdated Show resolved Hide resolved
ext/web/internal.d.ts Outdated Show resolved Hide resolved
@@ -16,7 +16,7 @@ Deno.test("[node/worker_threads] BroadcastChannel is exported", () => {
});

Deno.test("[node/worker_threads] MessageChannel are MessagePort are exported", () => {
assertEquals<unknown>(workerThreads.MessageChannel, MessageChannel);
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because the MessageChannel exported from worker_threads is now NodeMessageChannel

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM, let's address the ordering problem in a separate PR.

@satyarohith satyarohith merged commit 4d66ec9 into main Apr 2, 2024
17 checks passed
@satyarohith satyarohith deleted the fix_message_port_node_compat branch April 2, 2024 11:36
satyarohith added a commit that referenced this pull request Apr 11, 2024
Closes #22951
Closes #23001

Co-authored-by: Divy Srivastava <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants