Skip to content

Windows: detached child inherits listening socket and keeps port open after parent exits #36936

Closed

Description

@DamoyY

What version of Bun is running?

1.4.0-canary.1+81dac417b

What platform is your computer?

Microsoft Windows NT 10.0.29639.0 x64

What steps can reproduce the bug?

On Windows, a detached child spawned after an HTTP server starts appears to inherit the server's listening socket. If the child outlives the parent, the port remains listening even after server.close() completes and the parent exits normally.

Save this as repro.mjs:

import { spawn } from "node:child_process";
import { createServer } from "node:http";

const port = 43123;
const server = createServer((_request, response) => response.end("ok"));

await new Promise((resolve, reject) => {
  server.once("error", reject);
  server.listen(port, "127.0.0.1", resolve);
});

const child = spawn(
  process.execPath,
  ["-e", "setTimeout(() => {}, 300000)"],
  {
    detached: true,
    stdio: "ignore",
    windowsHide: true,
  },
);
child.unref();

console.log(JSON.stringify({ childPid: child.pid, parentPid: process.pid, port }));

await new Promise((resolve) => setTimeout(resolve, 250));
const closed = new Promise((resolve, reject) => {
  server.close((error) => (error ? reject(error) : resolve()));
});
server.closeAllConnections();
await closed;

console.log("server.close() completed; parent exiting normally");

Run:

bun .\repro.mjs
Get-NetTCPConnection -LocalPort 43123 -State Listen |
  Select-Object LocalAddress, LocalPort, State, OwningProcess

Example output from the script:

{"childPid":32292,"parentPid":23068,"port":43123}
server.close() completed; parent exiting normally

After the parent has exited, PowerShell still reports:

LocalAddress LocalPort State  OwningProcess
------------ --------- -----  -------------
127.0.0.1        43123 Listen         23068

PID 23068 no longer exists, while child PID 32292 is still alive. Terminating child PID 32292 immediately releases the port:

Stop-Process -Id 32292 -Force
Get-NetTCPConnection -LocalPort 43123 -State Listen -ErrorAction SilentlyContinue

The second command produces no output.

What is the expected behavior?

server.close() should close the listening socket completely. The detached child should remain alive without holding the parent's unrelated server socket, and the port should be immediately reusable after the parent exits.

This is how the same script behaves with Node.js 25.8.1 on the same machine: the child remains alive, but no listener remains on the port.

What do you see instead?

The port remains in LISTENING state. Windows attributes it to the already-exited parent PID, so killing the reported owning PID is impossible. The port is released only when the surviving child is terminated.

This also happens when the parent exits normally; Ctrl+C, a forced exit, active HTTP connections, and multiple process generations are not required.

Additional information

This appears distinct from #20319 and #36291. Those concern bun --filter / bun --parallel descendant cleanup and Job Objects. This reproduction is a regular Bun script using node:child_process.spawn, and the surviving child does not open or use the HTTP port itself.

The behavior suggests that a Windows listening socket/handle is marked inheritable and is included when the child is created. A long-lived child or daemon can therefore unknowingly pin any server port opened earlier by the Bun parent.

Activity

  1. robobun commented on Aug 5, 2026

    @robobun
    Collaborator

    ✅ Reproduced on Windows: uSockets creates sockets with plain socket(), which returns inheritable handles, so the detached child duplicated the listen handle and pinned the port.
    Fix in progress → #36938 (create sockets with WSA_FLAG_NO_HANDLE_INHERIT, like libuv). Verified the repro now matches Node behavior.

  2. added a commit that references this issue on Aug 5, 2026
    1c4cfba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions