-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
child_process: watch child_process stdin pipe peer close event #62353
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 all commits
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 |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| #include "handle_wrap.h" | ||
| #include "node.h" | ||
| #include "node_buffer.h" | ||
| #include "node_errors.h" | ||
| #include "node_external_reference.h" | ||
| #include "stream_base-inl.h" | ||
| #include "stream_wrap.h" | ||
|
|
@@ -80,6 +81,7 @@ void PipeWrap::Initialize(Local<Object> target, | |
| SetProtoMethod(isolate, t, "listen", Listen); | ||
| SetProtoMethod(isolate, t, "connect", Connect); | ||
| SetProtoMethod(isolate, t, "open", Open); | ||
| SetProtoMethod(isolate, t, "watchPeerClose", WatchPeerClose); | ||
|
|
||
| #ifdef _WIN32 | ||
| SetProtoMethod(isolate, t, "setPendingInstances", SetPendingInstances); | ||
|
|
@@ -110,6 +112,7 @@ void PipeWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |
| registry->Register(Listen); | ||
| registry->Register(Connect); | ||
| registry->Register(Open); | ||
| registry->Register(WatchPeerClose); | ||
| #ifdef _WIN32 | ||
| registry->Register(SetPendingInstances); | ||
| #endif | ||
|
|
@@ -159,6 +162,11 @@ PipeWrap::PipeWrap(Environment* env, | |
| // Suggestion: uv_pipe_init() returns void. | ||
| } | ||
|
|
||
| PipeWrap::~PipeWrap() { | ||
| peer_close_watching_ = false; | ||
| peer_close_cb_.Reset(); | ||
| } | ||
|
|
||
| void PipeWrap::Bind(const FunctionCallbackInfo<Value>& args) { | ||
| PipeWrap* wrap; | ||
| ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This()); | ||
|
|
@@ -213,6 +221,97 @@ void PipeWrap::Open(const FunctionCallbackInfo<Value>& args) { | |
| args.GetReturnValue().Set(err); | ||
| } | ||
|
|
||
| void PipeWrap::WatchPeerClose(const FunctionCallbackInfo<Value>& args) { | ||
| PipeWrap* wrap; | ||
| ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This()); | ||
|
|
||
| CHECK_GT(args.Length(), 0); | ||
| CHECK(args[0]->IsBoolean()); | ||
| const bool enable = args[0].As<v8::Boolean>()->Value(); | ||
|
|
||
| // UnwatchPeerClose | ||
| if (!enable) { | ||
| if (!wrap->peer_close_watching_) { | ||
| wrap->peer_close_cb_.Reset(); | ||
| return args.GetReturnValue().Set(0); | ||
| } | ||
|
|
||
| wrap->peer_close_watching_ = false; | ||
| wrap->peer_close_cb_.Reset(); | ||
| return args.GetReturnValue().Set(uv_read_stop(wrap->stream())); | ||
| } | ||
|
|
||
| if (!wrap->IsAlive()) { | ||
| return args.GetReturnValue().Set(UV_EBADF); | ||
| } | ||
|
|
||
| if (wrap->peer_close_watching_) { | ||
| return args.GetReturnValue().Set(0); | ||
| } | ||
|
|
||
| CHECK_GT(args.Length(), 1); | ||
| CHECK(args[1]->IsFunction()); | ||
|
|
||
| Environment* env = wrap->env(); | ||
| Isolate* isolate = env->isolate(); | ||
|
|
||
| // Store the JS callback securely so it isn't garbage collected. | ||
| wrap->peer_close_cb_.Reset(isolate, args[1].As<Function>()); | ||
| wrap->peer_close_watching_ = true; | ||
|
|
||
| // Start reading to detect EOF/ECONNRESET from the peer. | ||
| // We use our custom allocator and reader, ignoring actual data. | ||
| int err = uv_read_start(wrap->stream(), PeerCloseAlloc, PeerCloseRead); | ||
| if (err != 0) { | ||
| wrap->peer_close_watching_ = false; | ||
| wrap->peer_close_cb_.Reset(); | ||
| } | ||
| args.GetReturnValue().Set(err); | ||
|
Member
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. Is the
Contributor
Author
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. Indeed, it's not currently used in the JS side. Do you mean that if it's not used in the JS side, we shouldn't return a value? I saw that Bind and Connect both return values, and I assumed this was a convention. |
||
| } | ||
|
|
||
| void PipeWrap::PeerCloseAlloc(uv_handle_t* handle, | ||
| size_t suggested_size, | ||
| uv_buf_t* buf) { | ||
| // We only care about EOF, not the actual data. | ||
| // Using a static 1-byte buffer avoids dynamic memory allocation overhead. | ||
| static char scratch; | ||
| *buf = uv_buf_init(&scratch, 1); | ||
| } | ||
|
|
||
| void PipeWrap::PeerCloseRead(uv_stream_t* stream, | ||
| ssize_t nread, | ||
| const uv_buf_t* buf) { | ||
| PipeWrap* wrap = static_cast<PipeWrap*>(stream->data); | ||
| if (wrap == nullptr || !wrap->peer_close_watching_) return; | ||
|
|
||
| // Ignore actual data reads or EAGAIN (0). We only watch for disconnects. | ||
| if (nread > 0 || nread == 0) return; | ||
|
|
||
| // Wait specifically for EOF or connection reset (peer closed). | ||
| if (nread != UV_EOF && nread != UV_ECONNRESET) return; | ||
|
|
||
| // Peer has closed the connection. Stop reading immediately. | ||
| wrap->peer_close_watching_ = false; | ||
| uv_read_stop(stream); | ||
|
|
||
| if (wrap->peer_close_cb_.IsEmpty()) return; | ||
| Environment* env = wrap->env(); | ||
| Isolate* isolate = env->isolate(); | ||
|
|
||
| // Set up V8 context and handles to safely execute the JS callback. | ||
| v8::HandleScope handle_scope(isolate); | ||
| v8::Context::Scope context_scope(env->context()); | ||
| Local<Function> cb = wrap->peer_close_cb_.Get(isolate); | ||
| // Reset before calling to prevent re-entrancy issues | ||
| wrap->peer_close_cb_.Reset(); | ||
|
|
||
| errors::TryCatchScope try_catch(env); | ||
| try_catch.SetVerbose(true); | ||
|
|
||
| // MakeCallback properly tracks AsyncHooks context and flushes microtasks. | ||
| wrap->MakeCallback(cb, 0, nullptr); | ||
| } | ||
|
|
||
| void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) { | ||
| Environment* env = Environment::GetCurrent(args); | ||
|
|
||
|
|
@@ -252,7 +351,6 @@ void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) { | |
|
|
||
| args.GetReturnValue().Set(err); | ||
| } | ||
|
|
||
| } // namespace node | ||
|
|
||
| NODE_BINDING_CONTEXT_AWARE_INTERNAL(pipe_wrap, node::PipeWrap::Initialize) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const { spawn } = require('child_process'); | ||
|
|
||
| if (common.isWindows) { | ||
| common.skip('Not applicable on Windows'); | ||
| } | ||
|
|
||
| const child = spawn(process.execPath, [ | ||
| '-e', | ||
| 'require("fs").closeSync(0); setTimeout(() => {}, 2000)', | ||
| ], { stdio: ['pipe', 'ignore', 'ignore'] }); | ||
|
|
||
| const timeout = setTimeout(() => { | ||
| assert.fail('stdin close event was not emitted'); | ||
| }, 1000); | ||
|
|
||
| child.stdin.on('close', common.mustCall(() => { | ||
| clearTimeout(timeout); | ||
| child.kill(); | ||
| })); | ||
|
|
||
| child.on('exit', common.mustCall(() => { | ||
| clearTimeout(timeout); | ||
| })); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than exposing two functions, can this expose a single function with a single bool argument? e.g.
watchPeerClose(true, callback)/watchPeerClose(false)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, Got it. I'll commit it soon.