Skip to content

oxnode watch restart leaves processes the app started running, so a port stays busy #875

Closed

Description

@fengmk2

On a restart, oxnode watch sends the kill signal (SIGTERM) to the app process only. Once the app exits, the watcher clears the kill timer and starts the next run. A process the app started, such as a server in a normal (not detached) subprocess, keeps running, so the next run cannot bind the same port. killTree runs only on the kill timeout, Return, or a second Ctrl+C.

Reproduction

$ cat package.json main.ts
{ "type": "module" }
import { spawn } from 'node:child_process';

console.log('APP', process.pid);
spawn(
  process.execPath,
  [
    '-e',
    "require('node:net').createServer().listen(43219, () => console.log('GRANDCHILD', process.pid)).on('error', (e) => { console.log('SERVER_ERROR', e.code); process.exit(1); })",
  ],
  { stdio: 'inherit' },
);

$ oxnode watch ./main.ts
[oxnode] watching for changes
APP 82158
GRANDCHILD 82159
[oxnode] change in ./main.ts, restarting     <- after saving main.ts
APP 82161
SERVER_ERROR EADDRINUSE
[oxnode] completed — waiting for changes

The first run's server (82159) still holds the port after the restart (timestamps removed).

Reproduced on main (4759ced, v1.0.0) with Node.js 22.18 on macOS arm64. Found while embedding the watcher in Vite+ for vpx watch (voidzero-dev/vite-plus#3164). tsx watch 4.23.15 and node --watch behave the same way.

Cause

  • The kill effect signals the child alone, and its exit handler dispatches childExit right away (command.ts L213-L233).
  • On childExit while stopping for a restart, the machine clears the kill timer and spawns the next run (machine.ts L178-L183).

Once the child exits, its descendants are re-parented, so they cannot be found from its PID afterwards either.

Possible fix

List the child's descendants (descendants(processTable(), pid)) when a restart tells it to stop. When the child exits, send the same signal to the ones still running, and start the next run only after they are gone, force killing them on the kill timeout. On Linux, a zombie should count as gone: as PID 1 in a container without an init, oxnode adopts the orphans and never reaps them.

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

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions