Skip to content
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

fix(ext/node): cancel pending ipc writes on channel close #26504

Merged
merged 2 commits into from
Oct 24, 2024
Merged
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
9 changes: 8 additions & 1 deletion ext/node/ops/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
})
}
Expand Down
19 changes: 15 additions & 4 deletions ext/node/polyfills/internal/child_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ export function setupChannel(target: any, ipc: number) {
}
}

process.nextTick(handleMessage, msg);
nextTick(handleMessage, msg);
}
} catch (err) {
if (
Expand Down Expand Up @@ -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));
}
Expand All @@ -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];
Expand All @@ -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");
Expand Down