summaryrefslogtreecommitdiff
path: root/ext/node
diff options
context:
space:
mode:
authorNathan Whitaker <17734409+nathanwhit@users.noreply.github.com>2024-10-23 21:50:35 -0700
committerGitHub <noreply@github.com>2024-10-24 04:50:35 +0000
commit27df42f659ae7b77968d31363ade89addb516ea1 (patch)
tree4668ce4fe1b1bb32a4397b511dff46a818eab7a1 /ext/node
parent7c57105cc41cf16c5f48f85d85c7fd9bd3bb4d1f (diff)
fix(ext/node): cancel pending ipc writes on channel close (#26504)
Fixes the issue described in https://github.com/denoland/deno/issues/23882#issuecomment-2423316362. The parent was starting to send a message right before the process would exit, and the channel closed in the middle of the write. Unlike with reads, we weren't cancelling the pending writes, which resulted in a `Broken pipe` error surfacing to the user.
Diffstat (limited to 'ext/node')
-rw-r--r--ext/node/ops/ipc.rs9
-rw-r--r--ext/node/polyfills/internal/child_process.ts19
2 files changed, 23 insertions, 5 deletions
diff --git a/ext/node/ops/ipc.rs b/ext/node/ops/ipc.rs
index 59b6fece1..c5e0f875d 100644
--- a/ext/node/ops/ipc.rs
+++ b/ext/node/ops/ipc.rs
@@ -216,10 +216,17 @@ mod impl_ {
queue_ok.set_index(scope, 0, v);
}
Ok(async move {
- stream.clone().write_msg_bytes(&serialized).await?;
+ let cancel = stream.cancel.clone();
+ let result = stream
+ .clone()
+ .write_msg_bytes(&serialized)
+ .or_cancel(cancel)
+ .await;
+ // adjust count even on error
stream
.queued_bytes
.fetch_sub(serialized.len(), std::sync::atomic::Ordering::Relaxed);
+ result??;
Ok(())
})
}
diff --git a/ext/node/polyfills/internal/child_process.ts b/ext/node/polyfills/internal/child_process.ts
index 65d825fd2..cfff1079f 100644
--- a/ext/node/polyfills/internal/child_process.ts
+++ b/ext/node/polyfills/internal/child_process.ts
@@ -1339,7 +1339,7 @@ export function setupChannel(target: any, ipc: number) {
}
}
- process.nextTick(handleMessage, msg);
+ nextTick(handleMessage, msg);
}
} catch (err) {
if (
@@ -1400,7 +1400,7 @@ export function setupChannel(target: any, ipc: number) {
if (!target.connected) {
const err = new ERR_IPC_CHANNEL_CLOSED();
if (typeof callback === "function") {
- process.nextTick(callback, err);
+ nextTick(callback, err);
} else {
nextTick(() => target.emit("error", err));
}
@@ -1416,7 +1416,18 @@ export function setupChannel(target: any, ipc: number) {
.then(() => {
control.unrefCounted();
if (callback) {
- process.nextTick(callback, null);
+ nextTick(callback, null);
+ }
+ }, (err: Error) => {
+ control.unrefCounted();
+ if (err instanceof Deno.errors.Interrupted) {
+ // Channel closed on us mid-write.
+ } else {
+ if (typeof callback === "function") {
+ nextTick(callback, err);
+ } else {
+ nextTick(() => target.emit("error", err));
+ }
}
});
return queueOk[0];
@@ -1433,7 +1444,7 @@ export function setupChannel(target: any, ipc: number) {
target.connected = false;
target[kCanDisconnect] = false;
control[kControlDisconnect]();
- process.nextTick(() => {
+ nextTick(() => {
target.channel = null;
core.close(ipc);
target.emit("disconnect");