Repository navigation
node:child_process: keep an IPC message that arrives before there is a 'message' listener #45678
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
base: main
Are you sure you want to change the base?
Changes from 11 commits
8141b01
dd3283d
31474d6
07f946e
2a7ae47
647f0c7
d1fa042
3b17139
8000854
3c32b31
ff0ac4c
5889eb8
a01f1c4
3654a5a
14e38d2
e1edd65
1158d52
804af16
4e0ae84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1586,6 +1586,78 @@ extern "C" void Bun__installWatchModeSignalHandler(int signalNumber) | |
| extern "C" void Bun__MemoryPressure__install(JSC::JSGlobalObject* global); | ||
| extern "C" void Bun__MemoryPressure__uninstall(JSC::JSGlobalObject* global); | ||
|
|
||
| // Node's `target.listenerCount('message')`. The native list is first: the listenerCount() of node:events does not see it. | ||
| static bool hasMessageListener(Zig::GlobalObject* global) | ||
| { | ||
| auto& vm = JSC::getVM(global); | ||
| auto scope = DECLARE_THROW_SCOPE(vm); | ||
| auto* process = global->processObject(); | ||
| if (process->wrapped().hasEventListeners(vm.propertyNames->message)) | ||
| return true; | ||
| JSValue listenerCount = process->get(global, Identifier::fromString(vm, "listenerCount"_s)); | ||
| RETURN_IF_EXCEPTION(scope, false); | ||
| JSC::MarkedArgumentBuffer args; | ||
| args.append(jsString(vm, vm.propertyNames->message.string())); | ||
| JSValue count = JSC::call(global, listenerCount, process, args, "process.listenerCount is not a function"_s); | ||
| RETURN_IF_EXCEPTION(scope, false); | ||
| return count.toBoolean(global); | ||
| } | ||
|
|
||
| // https://github.com/nodejs/node/blob/v26.3.0/lib/internal/child_process.js#L726-L742 | ||
| JSC_DEFINE_HOST_FUNCTION(Process_functionEmitPendingMessages, (JSGlobalObject * lexicalGlobalObject, CallFrame*)) | ||
| { | ||
| auto* global = defaultGlobalObject(lexicalGlobalObject); | ||
| auto& vm = JSC::getVM(global); | ||
| auto scope = DECLARE_THROW_SCOPE(vm); | ||
| auto* process = global->processObject(); | ||
| if (!Bun__GlobalObject__connectedIPC(global)) | ||
| return JSValue::encode(jsUndefined()); | ||
| bool hasListener = hasMessageListener(global); | ||
| RETURN_IF_EXCEPTION(scope, {}); | ||
| auto* pending = process->m_pendingMessages.get(); | ||
| if (!hasListener || !pending) | ||
| return JSValue::encode(jsUndefined()); | ||
|
|
||
| for (unsigned i = 0, length = pending->length(); i < length; i += 2) { | ||
| JSC::MarkedArgumentBuffer args; | ||
| args.append(pending->getIndex(global, i)); | ||
| RETURN_IF_EXCEPTION(scope, {}); | ||
| args.append(pending->getIndex(global, i + 1)); | ||
| RETURN_IF_EXCEPTION(scope, {}); | ||
| bool listenerThrew = false; | ||
| process->wrapped().emit(vm.propertyNames->message, args, &listenerThrew); | ||
| // In Node the exception leaves the loop, and the next listener gets all of them again. | ||
| if (listenerThrew) | ||
| return JSValue::encode(jsUndefined()); | ||
| } | ||
| process->m_pendingMessages.clear(); | ||
| return JSValue::encode(jsUndefined()); | ||
| } | ||
|
|
||
| static void queueEmitPendingMessages(Zig::GlobalObject* global) | ||
| { | ||
| auto* process = global->processObject(); | ||
| if (process->m_pendingMessages) | ||
| process->queueNextTick(global, process->m_emitPendingMessagesFunction.getInitializedOnMainThread(process)); | ||
| } | ||
|
|
||
| JSC_DEFINE_HOST_FUNCTION(Process_functionOnNewListener, (JSGlobalObject * lexicalGlobalObject, CallFrame*)) | ||
| { | ||
| queueEmitPendingMessages(defaultGlobalObject(lexicalGlobalObject)); | ||
| return JSValue::encode(jsUndefined()); | ||
|
Comment on lines
+1646
to
+1649
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 nit (optional): maintainers get a host function that can leave a pending JS exception while returning a value, which the repo's exception-check rule forbids. Process_functionOnNewListener at src/jsc/bindings/BunProcess.cpp:1645 calls queueNextTick, which runs process.nextTick (user JS) and can throw, then returns jsUndefined() at :1646 with no throw scope or exception check. Fix: declare a throw scope at the top, and after queueNextTick use RETURN_IF_EXCEPTION(scope, {}) (or RELEASE_AND_RETURN) as the sibling host functions in this file do, so a throwing nextTick propagates as an empty return. Why this was flaggedThe 'newListener' listener installed by installPendingMessagesListener (src/jsc/bindings/BunProcess.cpp:1650-1659) fires Process_functionOnNewListener on every process.on()/once() in an IPC child. It calls global->processObject()->queueNextTick(...) at BunProcess.cpp:1645; queueNextTick (BunProcess.cpp:4523-4547) does this->get(globalObject, "nextTick") and JSC::call on the function, both of which can run user JS and throw, and it propagates the exception via RELEASE_AND_RETURN. Process_functionOnNewListener has no DECLARE_THROW_SCOPE and unconditionally returns JSValue::encode(jsUndefined()) at :1646, so a non-empty result is returned while vm has a pending exception. The sibling host functions added in the same change (Process_functionEmitPendingMessages at :1612-1640) check and propagate exceptions. The caller innerInvokeEventListeners catches the pending exception via its NakedPtr call, so this does not crash today; it is a convention slip against the repository rule 'Exception checks after every call that can enter JS'. The base branch has no such function. Verification: nit. Any process.on()/once() in an IPC child fires the 'newListener' listener, and its queueNextTick call can throw. src/jsc/bindings/BunProcess.cpp:1642-1647 Process_functionOnNewListener calls queueNextTick at :1645 and then returns jsUndefined() at :1646, with no DECLARE_THROW_SCOPE and no RETURN_IF_EXCEPTION. Nothing observable breaks: src/jsc/bindings/webcore/EventEmitter.cpp:259-261 captures the pending exception. |
||
| } | ||
|
|
||
| // The 'newListener' listener of Node's setupChannel. Native, so that process.on() does not enter the VM for it. | ||
| void Process::installPendingMessagesListener(JSC::VM& vm) | ||
| { | ||
| auto* globalObject = defaultGlobalObject(this->globalObject()); | ||
| if (!Bun__GlobalObject__hasIPC(globalObject)) | ||
| return; | ||
| auto* onNewListener = JSFunction::create(vm, globalObject, 0, ""_s, Process_functionOnNewListener, ImplementationVisibility::Public); | ||
| wrapped().addListener(Identifier::fromString(vm, "newListener"_s), WebCore::NativeEventListener::create(*onNewListener, [globalObject] { queueEmitPendingMessages(globalObject); }), false, false); | ||
| vm.writeBarrier(this, onNewListener); | ||
| } | ||
|
|
||
| static void onDidChangeListeners(EventEmitter& eventEmitter, const Identifier& eventName, bool isAdded) | ||
| { | ||
| if (Bun__isMainThreadVM()) { | ||
|
|
@@ -3782,13 +3854,15 @@ void Process::visitChildrenImpl(JSCell* cell, Visitor& visitor) | |
| visitor.append(thisObject->m_argv); | ||
| visitor.append(thisObject->m_execArgv); | ||
| visitor.append(thisObject->m_onWarning); | ||
| visitor.append(thisObject->m_pendingMessages); | ||
|
|
||
| thisObject->m_cpuUsageStructure.visit(visitor); | ||
| thisObject->m_resourceUsageStructure.visit(visitor); | ||
| thisObject->m_memoryUsageStructure.visit(visitor); | ||
| thisObject->m_bindingUV.visit(visitor); | ||
| thisObject->m_bindingNatives.visit(visitor); | ||
| thisObject->m_emitHelperFunction.visit(visitor); | ||
| thisObject->m_emitPendingMessagesFunction.visit(visitor); | ||
| } | ||
|
|
||
| DEFINE_VISIT_CHILDREN(Process); | ||
|
|
@@ -4953,10 +5027,39 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionEmitHelper, (JSGlobalObject * globalObj | |
|
|
||
| static constexpr auto kInternalIpcPrefix = "NODE_"_s; | ||
|
|
||
| // https://github.com/nodejs/node/blob/v26.3.0/lib/internal/child_process.js#L954-L964 | ||
| static void emitOrHoldMessage(Zig::GlobalObject* global, JSValue message, JSValue handle) | ||
| { | ||
| auto& vm = JSC::getVM(global); | ||
| auto scope = DECLARE_THROW_SCOPE(vm); | ||
| auto* process = global->processObject(); | ||
| bool hasListener = hasMessageListener(global); | ||
| RETURN_IF_EXCEPTION(scope, ); | ||
| if (hasListener) { | ||
| JSC::MarkedArgumentBuffer args; | ||
| args.append(message); | ||
| args.append(handle); | ||
| process->wrapped().emit(vm.propertyNames->message, args); | ||
| return; | ||
| } | ||
| if (!Bun__GlobalObject__connectedIPC(global)) | ||
| return; | ||
| auto* pending = process->m_pendingMessages.get(); | ||
| if (!pending) { | ||
| pending = JSC::constructEmptyArray(global, nullptr); | ||
| RETURN_IF_EXCEPTION(scope, ); | ||
| process->m_pendingMessages.set(vm, process, pending); | ||
| } | ||
| pending->push(global, message); | ||
| RETURN_IF_EXCEPTION(scope, ); | ||
| RELEASE_AND_RETURN(scope, pending->push(global, handle)); | ||
| } | ||
|
|
||
| extern "C" void Process__emitMessageEvent(Zig::GlobalObject* global, EncodedJSValue value, EncodedJSValue handle) | ||
| { | ||
| auto* process = global->processObject(); | ||
| auto& vm = JSC::getVM(global); | ||
| auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm); | ||
|
|
||
| auto& names = WebCore::builtinNames(vm); | ||
| auto ident = vm.propertyNames->message; | ||
|
|
@@ -4971,7 +5074,13 @@ extern "C" void Process__emitMessageEvent(Zig::GlobalObject* global, EncodedJSVa | |
| } | ||
| } | ||
|
|
||
| if (process->wrapped().hasEventListeners(ident)) { | ||
| if (ident == vm.propertyNames->message) { | ||
| emitOrHoldMessage(global, message, JSValue::decode(handle)); | ||
| if (auto* exception = scope.exception()) { | ||
| CLEAR_IF_EXCEPTION(scope); | ||
| Bun__reportUnhandledError(global, JSValue::encode(exception)); | ||
| } | ||
| } else if (process->wrapped().hasEventListeners(ident)) { | ||
| JSC::MarkedArgumentBuffer args; | ||
| args.append(message); | ||
| args.append(JSValue::decode(handle)); | ||
|
|
@@ -4983,6 +5092,7 @@ extern "C" void Process__emitDisconnectEvent(Zig::GlobalObject* global) | |
| { | ||
| auto* process = global->processObject(); | ||
| auto& vm = JSC::getVM(global); | ||
| process->m_pendingMessages.clear(); | ||
| auto ident = Identifier::fromString(vm, "disconnect"_s); | ||
| if (process->wrapped().hasEventListeners(ident)) { | ||
| JSC::MarkedArgumentBuffer args; | ||
|
|
@@ -5116,6 +5226,7 @@ void Process::finishCreation(JSC::VM& vm) | |
|
|
||
| // Before the hook below: onDidChangeListeners loads the signal tables on any add. | ||
| installDefaultWarningListener(vm); | ||
| installPendingMessagesListener(vm); | ||
| wrapped().onDidChangeListener = &onDidChangeListeners; | ||
|
|
||
| m_cpuUsageStructure.initLater([](const JSC::LazyProperty<Process, JSC::Structure>::Initializer& init) { | ||
|
|
@@ -5136,6 +5247,9 @@ void Process::finishCreation(JSC::VM& vm) | |
| m_bindingNatives.initLater([](const JSC::LazyProperty<Process, JSC::JSObject>::Initializer& init) { | ||
| init.set(Bun::ProcessBindingNatives::create(init.vm, ProcessBindingNatives::createStructure(init.vm, init.owner->globalObject()))); | ||
| }); | ||
| m_emitPendingMessagesFunction.initLater([](const JSC::LazyProperty<Process, JSFunction>::Initializer& init) { | ||
| init.set(JSFunction::create(init.vm, init.owner->globalObject(), 0, ""_s, Process_functionEmitPendingMessages, ImplementationVisibility::Private)); | ||
| }); | ||
| m_emitHelperFunction.initLater([](const JSC::LazyProperty<Process, JSFunction>::Initializer& init) { | ||
| init.set(JSFunction::create(init.vm, init.owner->globalObject(), 2, "emit"_s, Process_functionEmitHelper, ImplementationVisibility::Private)); | ||
| }); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.