GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — worker: handle detached `MessagePort` from a different context · nodejs/node@b4d149b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_messaging.cc
  • test/parallel
    • test-worker-workerdata-messageport.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_messaging.cc‎

Copy file name to clipboardExpand all lines: src/node_messaging.cc
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,13 @@ MaybeLocal<Value> MessagePort::ReceiveMessage(Local<Context> context,
779779

780780
void MessagePort::OnMessage(MessageProcessingMode mode) {
781781
Debug(this, "Running MessagePort::OnMessage()");
782+
// Maybe the async handle was triggered empty or more than needed.
783+
// The data_ could be freed or, the handle has been/is being closed.
784+
// A possible case for this, is transfer the MessagePort to another
785+
// context, it will call the constructor and trigger the async handle empty.
786+
// Because all data was sent from the preivous context.
787+
if (IsDetached()) return;
788+
782789
HandleScope handle_scope(env()->isolate());
783790
Local<Context> context =
784791
object(env()->isolate())->GetCreationContext().ToLocalChecked();
Collapse file

‎test/parallel/test-worker-workerdata-messageport.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-worker-workerdata-messageport.js
+28-2Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22

33
require('../common');
4-
const assert = require('assert');
4+
const assert = require('node:assert');
55

66
const {
77
Worker, MessageChannel
8-
} = require('worker_threads');
8+
} = require('node:worker_threads');
99

1010
const channel = new MessageChannel();
1111
const workerData = { mesage: channel.port1 };
@@ -59,3 +59,29 @@ const meowScript = () => 'meow';
5959
'listed in transferList'
6060
});
6161
}
62+
63+
{
64+
// Should not crash when MessagePort is transferred to another context.
65+
// https://github.com/nodejs/node/issues/49075
66+
const channel = new MessageChannel();
67+
new Worker(`
68+
const { runInContext, createContext } = require('node:vm')
69+
const { workerData } = require('worker_threads');
70+
const context = createContext(Object.create(null));
71+
context.messagePort = workerData.messagePort;
72+
runInContext(
73+
\`messagePort.postMessage("Meow")\`,
74+
context,
75+
{ displayErrors: true }
76+
);
77+
`, {
78+
eval: true,
79+
workerData: { messagePort: channel.port2 },
80+
transferList: [channel.port2]
81+
});
82+
channel.port1.on(
83+
'message',
84+
(message) =>
85+
assert.strictEqual(message, 'Meow')
86+
);
87+
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.