MessagePort can send messages after closedΒ #42296
Copy link
Copy link
Closed
Labels
workerIssues and PRs related to the worker_threads module and Worker API.Issues and PRs related to the worker_threads module and Worker API.
Description
Version
v17.7.1
Platform
Linux 5.13.0-30-generic #33~20.04.1-Ubuntu SMP Mon Feb 7 14:25:10 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
worker_threads
What steps will reproduce the bug?
const { MessageChannel } = require('worker_threads');
const ch = new MessageChannel()
ch.port1.onmessage = () => console.log('hey');
ch.port2.close();
ch.port2.postMessage('asdf');How often does it reproduce? Is there a required condition?
Consistently in v17.7.1, v16.14.0 and v14.19.0.
What is the expected behavior?
The program should not log anything, since port2 is closed right before the call to postMessage.
What do you see instead?
The program does log hey.
Additional information
For whatever is worth, both in Chrome (Chromium 98.0.4758.102, V8 9.8.177.11) and Firefox (97.0.2), the message never arrives.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
workerIssues and PRs related to the worker_threads module and Worker API.Issues and PRs related to the worker_threads module and Worker API.
Activity
WillianAgostini commentedon Mar 14, 2022
Hi @jrvidal
In this case the method
ch.port2.close()has not been completely closed when the code callsch.port2.postMessage('asdf').If you execute the next code you can see the log 'hey' has shown before than log 'close'.
You can see this docs on https://nodejs.org/api/worker_threads.html#portclose.
jrvidal commentedon Mar 14, 2022
Thanks, the docs are somewhat ambiguous about this point, that's why I reported.
I think it'd be desirable to more closely align with browsers here, or at least be more explicit about this behavior in the documentation.
Trott commentedon Mar 16, 2022
#42357 aligns the behavior with the expectations described here and the behavior described for Chrome and Firefox.
jrvidal commentedon Mar 16, 2022
@Trott Thanks for that PR! Any intent to backport to 16.x?
aduh95 commentedon Mar 16, 2022
Node.js v16.x is on Active LTS mode; unless a git conflict arises, it should be backported "automatically" after the adequate maturation delay, see
node/doc/contributing/backporting-to-release-lines.md
Lines 22 to 25 in a199387
worker: do not send message if port is closing
worker: do not send message if port is closing
worker: do not send message if port is closing
worker: do not send message if port is closing
worker: do not send message if port is closing
worker: do not send message if port is closing
worker: do not send message if port is closing