Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion src/bun_core/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ impl RawModeGuard {
impl Drop for RawModeGuard {
#[inline]
fn drop(&mut self) {
let _ = self.state.set_mode(self.fd, Mode::Normal, SetAttrWhen::Drain);
let _ = self
.state
.set_mode(self.fd, Mode::Normal, SetAttrWhen::Drain);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/md/ansi_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2535,7 +2535,11 @@ fn probe_kitty_graphics() -> bool {
Err(_) => return false,
};
let mut tty_state = bun_core::tty::State::new();
let _ = tty_state.set_mode(0, bun_core::tty::Mode::Raw, bun_core::tty::SetAttrWhen::Drain);
let _ = tty_state.set_mode(
0,
bun_core::tty::Mode::Raw,
bun_core::tty::SetAttrWhen::Drain,
);
let _restore = scopeguard::guard((saved_termios, tty_state), |(saved, mut state)| {
if bun_sys::posix::tcsetattr(0, bun_sys::posix::TCSA::Now, &saved).is_err() {
let _ = state.set_mode(
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/bake/bake_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use core::ptr::NonNull;
use bun_alloc::Arena; // = bumpalo::Bump
use bun_collections::ArrayHashMap;
use bun_core::Output;
use bun_jsc::{JSGlobalObject, JSValue, JsError, JsResult, ZigStringSlice};
use bun_core::{ZStr, strings};
use bun_jsc::{JSGlobalObject, JSValue, JsError, JsResult, ZigStringSlice};
use bun_options_types::schema as bun_schema;
use bun_paths::{self as paths, PathBuffer};

Expand Down
5 changes: 4 additions & 1 deletion src/runtime/server/server_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,10 @@ pub mod BunInfo {
// `JSON.toAST(allocator, BunInfo, info)` — hand-expanded:
let platform_props = bun_alloc::AstAlloc::vec_from_iter([
prop(b"os", str_expr(os_tag_name(info.platform.os))),
prop(b"arch", str_expr(arch_tag_name(bun_core::Environment::ARCH))),
prop(
b"arch",
str_expr(arch_tag_name(bun_core::Environment::ARCH)),
),
prop(b"version", str_expr(info.platform.version)),
]);
let platform_expr = Expr::init(
Expand Down
9 changes: 9 additions & 0 deletions src/runtime/socket/Listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,15 @@ impl WindowsNamedPipeListeningContext {
// return error.FailedChmodPipe;
//}

// `uv_listen` made the pipe an active+ref'd uv handle. Strip libuv's
// loop ref so the owning `Listener`'s `poll_ref` is the only thing
// keeping the process alive (the contract usockets' libuv backend
// applies to its handles); otherwise `server.unref()` drops the
// `poll_ref` but the uv handle still pins `uv_loop_alive` and the
// process never exits.
// SAFETY: `this` is live; `&mut uv_pipe` is scoped to this call.
unsafe { (*this).uv_pipe.unref() };

let (this, _) = scopeguard::ScopeGuard::into_inner(cleanup);
Ok(this)
}
Expand Down
35 changes: 34 additions & 1 deletion test/js/node/net/node-net-server.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { realpathSync } from "fs";
import { bunEnv, bunExe } from "harness";
import { bunEnv, bunExe, tempDir } from "harness";
import { AddressInfo, createServer, Server, Socket } from "net";
import { createTest } from "node-harness";
import { once } from "node:events";
Expand Down Expand Up @@ -629,3 +629,36 @@ describe("accepted socket event-loop hold matches Node (per-connection KeepAlive
).toEqual({ stdout: "fast", exitCode: 0, failureDetail: "" });
});
});

// The Windows named-pipe listener never stripped libuv's own loop ref from its
// uv_pipe_t (uv_listen marks the handle active+ref'd), so server.unref()
// dropped the Listener's KeepAlive but the uv handle still pinned
// uv_loop_alive and the process never exited. TCP and unix-socket listeners go
// through usockets, which unrefs its uv handles up front.
it("server.unref() on a pipe/unix-socket listener lets the process exit", async () => {
// The child exits without close() (natural exit is the observable), so the
// unix socket file must live in a tempDir the parent disposes.
using dir = tempDir("server-unref", {});
const listenPath =
process.platform === "win32"
? "\\\\.\\pipe\\bun-server-unref-" + process.pid + "-" + Math.random().toString(36).slice(2)
: join(String(dir), "server-unref.sock");
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`
const server = require("net").createServer();
server.listen(process.env.SERVER_UNREF_LISTEN_PATH, () => server.unref());
setTimeout(() => { process.stdout.write("HUNG"); process.exit(1); }, 4000).unref();
`,
],
env: { ...bunEnv, SERVER_UNREF_LISTEN_PATH: listenPath },
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stdout).toBe("");
expect(exitCode === 0 ? "" : stderr).toBe("");
expect(exitCode).toBe(0);
});
Loading