Skip to content

fix(ext/node): unref stdin on pause to allow process exit#32086

Merged
fraidev merged 1 commit intodenoland:mainfrom
fraidev:fix/node-stdin-pause-unref
Feb 9, 2026
Merged

fix(ext/node): unref stdin on pause to allow process exit#32086
fraidev merged 1 commit intodenoland:mainfrom
fraidev:fix/node-stdin-pause-unref

Conversation

@fraidev
Copy link
Copy Markdown
Contributor

@fraidev fraidev commented Feb 6, 2026

When process.stdin is a pipe without a _handle (PIPE/TCP type), calling pause() did not unref the underlying Deno stdin resource. This caused pending reads to keep the event loop alive, preventing the process from exiting even after stdin was paused.

This fix adds io.stdin[UNREF]() in the onpause handler for handleless stdin, and io.stdin[REF]() in _read() to re-ref when reading resumes. This matches Node.js behavior where paused stdin does not keep the process alive.

Fixes node_compat test-stdin-child-proc.js timeout.

@bartlomieju
Copy link
Copy Markdown
Member

Can you check if this fixes #31871 by any chance?

@fraidev
Copy link
Copy Markdown
Contributor Author

fraidev commented Feb 9, 2026

Can you check if this fixes #31871 by any chance?

Unfortunately no, I think is related to unref the worker, maybe this test cover it test-worker-unref-from-message-during-exit.js

@fraidev fraidev merged commit 29e64f0 into denoland:main Feb 9, 2026
24 checks passed
@sigmaSd
Copy link
Copy Markdown
Contributor

sigmaSd commented Feb 9, 2026

What about #30747 it feels related (though I tested and it still doesn't work)

fraidev added a commit that referenced this pull request Feb 20, 2026
- Fix `child_process` shell command translation to handle shell
redirections (`1>&-`, `< file`), piped commands (`cmd1 | cmd2`), and
shell variable prefixes (`"$NODE"`, `${NODE}`). Previously shell
operators were passed to the CLI arg translator, causing parse failures
or incorrect output.
- Remove duplicate `transformDenoShellCommand` call from
`normalizeSpawnArguments` — it was already called in `buildCommand`, and
the double transformation corrupted POSIX `'\''` quoting.
- Add `wrap_eval` parameter to `op_node_translate_cli_args` so shell
command contexts skip eval code wrapping (which introduces
metacharacters unsafe for shell embedding).
- Map `BrokenPipe` to `EPIPE` (was incorrectly mapped to `EBADF`) in
`stream_wrap`, and catch `BrokenPipe` in `process.stdout`/
`process.stderr` `writeSync` to emit it as an async error event.
- Add minimal `_handle` with `ref()`/`unref()`/`close()` to PIPE stdin.
Intentionally omits `readStart`/`readStop`/`reading` — adding those
resets `_readableState.reading`, which triggers duplicate `_read()`
calls with orphaned reffed promises that prevent process exit.
- Ref `io.stdin` in `_read()` and unref on pause when no `readStop`
exists, matching Node.js event loop ref-counting behavior.
- Add platform-specific quoting in `transformDenoShellCommand`: POSIX
uses single quotes with `'\''` escaping, Windows uses double quotes with
`\\` doubling before `"`.
- Add Windows `cmd.exe` detection (`/d /s /c` pattern) in `buildCommand`
for shell command transformation.
- Deduplicate `--unstable-*` flags in `fork()` between CLI translator
and bootstrap args.

Enables 3 new node_compat tests:
  - `test-stdio-closed.js`
  - `test-stdout-close-catch.js`
  - `test-stdout-close-unref.js`


Needs: 
- [x] #32086
- [x] #32087
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants