Skip to content
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
f0031ab
ipc: deliver a peer-initiated channel close from the read dispatch, b…
robobun Aug 18, 2026
115e9dc
ipc: pass the libuv read-error context as a raw pointer; block the fo…
robobun Aug 18, 2026
820de1d
ipc: shorten the comments on the peer-close path
robobun Aug 18, 2026
b80014d
ipc: on Windows, report a peer close that lands while a write is in f…
robobun Aug 18, 2026
6313483
ipc: let an in-flight Windows write complete before a peer close is r…
robobun Aug 18, 2026
22d2255
ipc: report the channel close from the exit path, before the exit itself
robobun Aug 18, 2026
519958f
ipc: pin the disconnect-before-exit order with a node:test file that …
robobun Aug 18, 2026
904dac0
ipc: at exit, close the channel on Windows without waiting for a writ…
robobun Aug 18, 2026
63d7b8d
Merge branch 'main' into farm/f99e6bad/ipc-peer-close-before-exit
robobun Oct 25, 2026
9cc897a
test: a child and a cluster worker that exit on their own while the p…
robobun Oct 25, 2026
289e7b6
ipc: leave a channel with unread input open when the child exits
robobun Oct 25, 2026
3a2a29d
types: say when onDisconnect follows onExit
robobun Oct 25, 2026
080c4fb
ipc: say what happens to a channel with unread input at exit
robobun Oct 25, 2026
408ae0a
ipc: run the queued close work from the exit path through run_pending
robobun Oct 26, 2026
a089ea7
ipc: at exit, close only a channel whose other end is gone
robobun Oct 26, 2026
93747ea
sys: add recv_peek_non_block and use it for the IPC end-of-channel check
robobun Oct 26, 2026
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: 4 additions & 5 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7918,11 +7918,10 @@ declare module "bun" {
* - This callback indicates that the pipe is closed; it is not an error
* by itself. Use {@link onExit} or {@link Subprocess.exited} to
* determine why the process ended.
* - It may occur before or after {@link onExit} depending on timing; do
* not rely on ordering. Typically, if you or the child call
* `disconnect()` first, this fires before {@link onExit}; if the
* process exits without an explicit disconnect, either may happen
* first.
* - When the process exits, this fires before {@link onExit}, the order
* in which `node:child_process` emits `'disconnect'` and `'exit'`.
* The exception is a channel with input that is still unread at that
* moment: it is closed, and reported, after {@link onExit}.
* - Only runs when {@link ipc} is enabled and runs at most once per
* subprocess.
* - If the child becomes a zombie (exited but not yet reaped), the IPC is
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/api/bun/subprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,11 @@ impl Subprocess<'_> {
}
}

// The channel's close is reported before the exit below, as in node.
if let Some(ipc) = self.ipc() {
ipc.close_after_peer_exit();
Comment thread
robobun marked this conversation as resolved.
Resolved
}

let mut did_update_has_pending_activity = false;

// Kept as raw `*mut` so the enter guard and the body can both call
Expand Down
95 changes: 79 additions & 16 deletions src/runtime/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,22 +1200,7 @@ impl SendQueue {
log!("SendQueue#closeSocketTask");
sq.close_socket(CloseReason::Normal, CloseFrom::User);
}
if sq.pending_after_close.replace(false) {
log!("SendQueue#_onAfterIPCClosed");
if !sq.close_event_sent.replace(true) {
let global = sq.get_global_this();
if let Some(item) = sq.waiting_for_ack.with_mut(|w| w.take()) {
item.complete(&global);
}
// on_write_complete already dequeued everything fully written; the rest was never delivered.
for item in sq.queue.with_mut(std::mem::take) {
item.abort_unsent(&global);
}
if let Some(owner) = sq.owner.get() {
owner.handle_ipc_close();
}
}
}
sq.run_after_close();
}
// Release the task's ref; the SendQueue may be freed here.
// SAFETY: `this` is live and owns the ref taken at schedule.
Expand All @@ -1232,6 +1217,84 @@ impl SendQueue {
unsafe { <SendQueue as bun_ptr::CellRefCounted>::deref(this) };
}

fn run_after_close(&self) {
if !self.pending_after_close.replace(false) {
return;
}
log!("SendQueue#_onAfterIPCClosed");
if self.close_event_sent.replace(true) {
return;
}
let global = self.get_global_this();
if let Some(item) = self.waiting_for_ack.with_mut(|w| w.take()) {
item.complete(&global);
}
// on_write_complete already dequeued everything fully written; the rest was never delivered.
for item in self.queue.with_mut(std::mem::take) {
item.abort_unsent(&global);
Comment thread
robobun marked this conversation as resolved.
Resolved
}
if let Some(owner) = self.owner.get() {
owner.handle_ipc_close();
}
}

/// The peer process has exited: closes what is left of the channel and reports the close now,
/// for the owner to report before the exit (node's order). A channel with input the loop has
/// yet to read stays open for that read; its close follows the exit.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub(crate) fn close_after_peer_exit(&self) {
log!("SendQueue#closeAfterPeerExit");
if self.socket_is_open() {
if self.is_connected() && self.has_unread_input() {
return;
}
Comment thread
robobun marked this conversation as resolved.
Resolved
// A disconnect() still postponed (behind a handle's ack, or on its task) is overtaken.
self.close_after_flush.set(false);
self.pending_close.set(false);
// As is a write still in flight: nothing is left to complete it.
#[cfg(windows)]
self.windows_close(true);
#[cfg(not(windows))]
self.close_socket(CloseReason::Normal, CloseFrom::User);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if !self.pending_after_close.get() {
return;
}
let _scope = self.get_global_this().bun_vm().enter_event_loop_scope();
self.run_after_close();
}

/// Whether the kernel holds input for the channel that the loop has not read yet.
fn has_unread_input(&self) -> bool {
let SocketUnion::Open(socket) = *self.socket.get() else {
return false;
};
#[cfg(not(windows))]
{
let mut queued: c_int = 0;
// SAFETY: FIONREAD writes one int.
let rc =
unsafe { libc::ioctl(socket.fd().native(), libc::FIONREAD as _, &raw mut queued) };
rc == 0 && queued > 0
}
#[cfg(windows)]
{
use bun_sys::windows::kernel32::PeekNamedPipe;
let mut queued: u32 = 0;
// SAFETY: `socket` is the live pipe (`socket == Open`); only `queued` is written.
let peeked = unsafe {
PeekNamedPipe(
(*socket).handle,
core::ptr::null_mut(),
0,
core::ptr::null_mut(),
&raw mut queued,
core::ptr::null_mut(),
)
};
peeked != 0 && queued > 0
}
}

/// `uv::open_handles` closes the channel's pipe through here at a thread
/// teardown: close now (pending writes finish ECANCELED) and let the owner
/// observe the disconnect, rather than waiting for writes as a user close does.
Expand Down
9 changes: 9 additions & 0 deletions src/windows_sys/externs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,15 @@ pub mod kernel32 {
nDefaultTimeOut: DWORD,
lpSecurityAttributes: *mut c_void,
) -> HANDLE;
/// `PeekNamedPipe` (`namedpipeapi.h`). Each out-pointer may be null.
pub fn PeekNamedPipe(
hNamedPipe: HANDLE,
lpBuffer: *mut c_void,
nBufferSize: DWORD,
lpBytesRead: *mut DWORD,
lpTotalBytesAvail: *mut DWORD,
lpBytesLeftThisMessage: *mut DWORD,
) -> BOOL;
/// `AddVectoredExceptionHandler` (`errhandlingapi.h`).
pub fn AddVectoredExceptionHandler(
First: u32,
Expand Down
Loading
Loading