Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,11 @@ class SerializerDelegate : public ValueSerializer::Delegate {

private:
Maybe<bool> WriteHostObject(BaseObjectPtr<BaseObject> host_object) {
BaseObject::TransferMode mode = host_object->GetTransferMode();
// A null host_object means the underlying BaseObject has already been
// destroyed (e.g. a MessagePort whose close has completed).
BaseObject::TransferMode mode =
host_object ? host_object->GetTransferMode()
: TransferMode::kDisallowCloneAndTransfer;
if (mode == TransferMode::kDisallowCloneAndTransfer) {
ThrowDataCloneError(env_->clone_unsupported_type_str());
return Nothing<bool>();
Expand Down Expand Up @@ -1160,8 +1164,8 @@ void MessagePort::ReceiveMessage(const FunctionCallbackInfo<Value>& args) {
"The \"port\" argument must be a MessagePort instance");
}
MessagePort* port = Unwrap<MessagePort>(args[0].As<Object>());
if (port == nullptr) {
// Return 'no messages' for a closed port.
if (port == nullptr || !port->data_) {
// Return 'no messages' for a closed or detached (e.g. transferred) port.
args.GetReturnValue().Set(env->no_message_symbol());
return;
}
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-worker-message-port-detached-no-crash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const { MessageChannel, receiveMessageOnPort } = require('worker_threads');

// Regression test for https://github.com/nodejs/node/issues/67766

{
// receiveMessageOnPort() on a port that was just transferred must not crash.
const { port1 } = new MessageChannel();
const other = new MessageChannel();
other.port1.postMessage(null, [port1]);
assert.strictEqual(receiveMessageOnPort(port1), undefined);
other.port1.close();
}

{
// structuredClone() of a port whose close has completed must throw a
// DataCloneError instead of crashing.
const { port1 } = new MessageChannel();
port1.close(common.mustCall(() => {
setImmediate(common.mustCall(() => {
assert.throws(() => structuredClone(port1), {
name: 'DataCloneError',
message: 'Cannot clone object of unsupported type.',
});
}));
}));
}
Loading