Skip to content

win: fix error translation in uv_pipe, pipe write_cb, realpath/readlink, filemap, tty - #11

Merged
dylan-conway merged 1 commit into
oven-sh:bunfrom
robobun:farm/e77a46b9/win-error-fixes
Jul 23, 2026
Merged

dylan-conway merged 1 commit into
oven-sh:bunfrom
robobun:farm/e77a46b9/win-error-fixes

Conversation

@robobun

@robobun robobun commented Jul 22, 2026 •

Copy link
Copy Markdown

Subset of a src/win/*.c error-translation/propagation audit, keeping only the fixes that are reachable from Bun's own code. All are also present in upstream libuv/libuv v1.x. The uv__set_pipe_handle NtQueryInformationFile case was already fixed in #10.

function before after
uv_pipe returns raw positive GetLastError() value from a public negative-errno API; errno == UV_EMFILE (never true: +24 vs -4066) uv_translate_sys_error(err); errno == EMFILE
uv__process_pipe_write_req uv_translate_sys_error, so ERROR_BROKEN_PIPE becomes UV_EOF in write_cb uv_translate_write_sys_error, matching the sync uv_write() path
fs__realpath_handle returns UV_ENOMEM but caller tests == -1, so OOM reports success with req->ptr == NULL sets ERROR_OUTOFMEMORY and returns -1
fs__readlink_handle returns UV_ENOMEM without touching last-error; caller reads stale GetLastError() sets ERROR_OUTOFMEMORY and returns -1
fs__filemap_ex_filter reads ExceptionInformation[3]; NTSTATUS for EXCEPTION_IN_PAGE_ERROR is at [2] read index [2]
uv__tty_move_caret missing return -1 after GetConsoleScreenBufferInfo failure; reads uninit info, may spin on retry add return -1

Not included (found in the audit, but unreachable from Bun's own code)

uv_pipe_chmod (GetLastError() after Get/SetSecurityInfo/SetEntriesInAcl), uv__slow_poll_thread_proc (SET_REQ_ERROR on hardcoded poll_req_1), fs__sendfile (SET_REQ_RESULT(req, -1)), uv_pipe_connect (UV_E* → NTSTATUS round-trip), fs__readdir (stale GetLastError() after uv__convert_utf16_to_utf8). These are upstreamable separately.

Verification

Built on Windows (mingw, -DLIBUV_BUILD_TESTS=ON), no new warnings. Relevant libuv tests pass (fs_realpath, fs_readlink, pipe_connect_bad_name, pipe_connect_to_file, pipe_getsockname, pipe_overlong_path, pipe_set_chmod, ipc_listen_after_write, ipc_listen_before_write, tty, tty_cursor_move_absolute).

@robobun

robobun commented Jul 22, 2026

Copy link
Copy Markdown
Author

Self-review

Probed the diff across edge cases, blast radius, wrong-layer, and coverage. 34 concerns raised, 31 refuted. Three survived; the first two are fixed in fd3a06e.

Fixed in fd3a06e

fs__sendfile: _doserrno == 0 on CRT parameter validation. The UCRT _read/_write/_lseeki64 functions set errno but zero _doserrno when the failure comes from CRT parameter validation (e.g. bad or closed fd), so SET_REQ_WIN32_ERROR(req, _doserrno) would have produced uv_translate_sys_error(0) == 0. The same gap is already documented in-tree for _close at fs.c:695-700. Amended to fall back to errno when _doserrno == 0.

fs__sendfile: _write failure used the read-side translator. SET_REQ_WIN32_ERROR maps ERROR_BROKEN_PIPE to UV_EOF; fs__write (fs.c:1103) deliberately uses uv_translate_write_sys_error so the same code yields UV_EPIPE. Amended the _write branch to match.

Remaining (noted, not fixed)

Coverage gap. No new tests. Of the twelve error branches changed, one is exercised by the existing suite (pipe_overlong_path calls uv_pipe_connect(&req, &pipe, "", cb) and asserts status == UV_EINVAL, which pins the SET_REQ_ERROR mapping at pipe.c:902). Three more are cheaply testable from the public API without fault injection:

  • uv_fs_sendfile with a closed/read-only out_fd, asserting UV_EBADF
  • pipe write to closed reader, asserting write_cb status == UV_EPIPE (port the core of test-signal-pending-on-close.c, which is #ifndef _WIN32)
  • uv_pipe() with the CRT fd table exhausted via _setmaxstdio, asserting UV_EMFILE

The remaining branches (NtQueryInformationFile failure, GetSecurityInfo failure, select() SOCKET_ERROR on non-IFS LSP, GetConsoleScreenBufferInfo failure, uv_utf16_to_wtf8 OOM, ExceptionInformation[2]) need OS-level fault injection and are inspection-verified only.

Happy to add the three cheap tests if wanted.


Effect on Bun

Traced each changed function to its call sites in Bun. Bun's libuv return-code wrapper treats < 0 as error everywhere.

change reached from Bun via observable effect on Bun
uv__set_pipe_handle NTSTATUS uv_pipe_open in io/source.rs, WindowsNamedPipe.rs, test/parallel/Channel.rs NtQueryInformationFile failure during process.stdin/out/err / child.std* / net.Socket({fd}) attach now errors instead of silently succeeding with an un-probed handle. Rare path.
uv_pipe_chmod not called by Bun (commented-out TODO at socket/Listener.rs:1804) NAPI addons only.
uv_pipe() sign spawn/process.rs:2000, sys/lib.rs:4393, install/…/security_scanner.rs:1129 Bun checks < 0. Before: a positive Win32 error leaked through as "success" with garbage fds. After: returns a proper negative UV_E*. This is a straight correctness fix for Bun.spawn merged-stdio, shell pipelines, and the install security scanner on the (rare) pipe-creation-failure path.
uv_pipe() errno == EMFILE same CRT fd-table exhaustion now surfaces as EMFILE instead of UNKNOWN.
uv_pipe_connect mapping not called by Bun (Bun uses uv_pipe_connect2 directly) NAPI addons only.
uv__process_pipe_write_req EPIPE io/PipeWriter.rs:1491/2029 write_cb → FileSink::on_error, WindowsNamedPipe::on_error, shell::IOWriter::on_error User-visible change. Writing to a closed pipe now delivers {code:"EPIPE"} instead of {code:"EOF"} to JS (matching Node/Unix). In shell/IOWriter.rs:867 the broken_pipe flag keys on E::PIPE, so the shell's broken-pipe handling now triggers as intended where it previously didn't fire on an async write-cb failure.
uv__slow_poll_thread_proc dns.rs:4875 uv_poll_init_socket (c-ares) c-ares sockets are IFS, so Bun takes the fast-poll path. Slow-poll is only reached if a non-IFS LSP is installed system-wide (legacy AV/VPN) or via NAPI addons. No effect on stock systems.
fs__realpath OOM node_fs.rs:7559 (fs.realpath*) OOM now errors instead of reporting success with a null result.
fs__readlink_handle OOM sys_uv.rs:333 → fs.readlink, fs.cp, fs.watch OOM now reports ENOMEM instead of a stale/zero error.
fs__readdir OOM not called by Bun (fs.readdir/opendir use direct NtQueryDirectoryFile in sys/lib.rs:635) NAPI addons only.
fs__sendfile not called by Bun (sys/lib.rs:4508 returns ENOSYS on Windows) NAPI addons only.
fs__filemap_ex_filter UV_FS_O_FILEMAP is exposed in fs.constants and flows through fs.open; Bun itself never sets it Only affects user code that explicitly passes constants.UV_FS_O_FILEMAP to fs.open. In-page errors then surface as the real disk error instead of UNKNOWN.
uv__tty_move_caret io/PipeWriter.rs tty writes Bun enables ENABLE_VIRTUAL_TERMINAL_PROCESSING at startup (bun_core/output.rs:689-710), so libuv's legacy ANSI emulation is bypassed on Windows 10+. Only reached on consoles where VT mode is unavailable, or via NAPI uv_tty_set_vterm_state(UV_TTY_UNSUPPORTED).

Net for Bun: one intended user-visible change (pipe write_cb now reports EPIPE instead of EOF on Windows, which also fixes shell::IOWriter's broken-pipe detection). Everything else either turns a silent-success into a proper error on a rare failure path, or is unreachable from Bun's own code.

@robobun
robobun force-pushed the farm/e77a46b9/win-error-fixes branch from fd3a06e to efe1c66 Compare July 23, 2026 00:03
robobun added a commit to oven-sh/bun that referenced this pull request Jul 23, 2026
Points at the PR head (reachable via refs/pull/11/head on oven-sh/libuv
so github-archive works). Swap to the squash-merge SHA once #11 lands;
the tree is identical.
robobun added a commit to oven-sh/bun that referenced this pull request Jul 23, 2026
The existing child_process EPIPE test uses fs.closeSync(0) in the child,
which is a no-op for fd<=2 in libuv's fs__close on Windows (fs.c:690), so
the pipe never breaks and the test hangs. Restore its skipIf(isWindows).

Instead add a Bun.spawn test where the child reads one byte then exits,
leaving the parent's 256KB write to fail on the broken pipe. On Windows
before oven-sh/libuv#11 this surfaces as code "EOF" (uv__process_pipe_write_req
used the read-side translator); after the fix it is "EPIPE", matching
Node and Unix.
…nk, filemap, tty

Subset of a broader src/win/ error-propagation audit; this commit keeps
only the fixes reachable from Bun's own code paths. All are also present
in upstream libuv/libuv v1.x.

pipe.c:
  uv_pipe: translate the positive GetLastError() from
    uv__create_pipe_pair() before returning from this public API; compare
    errno against EMFILE (24), not UV_EMFILE (-4066).
  uv__process_pipe_write_req: use uv_translate_write_sys_error() so
    ERROR_BROKEN_PIPE / ERROR_NO_DATA surface as UV_EPIPE in write_cb,
    matching the synchronous uv_write() path (stream.c) and Unix.

fs.c:
  fs__filemap_ex_filter: the underlying NTSTATUS for
    EXCEPTION_IN_PAGE_ERROR is ExceptionInformation[2], not [3]; index 3
    is unpopulated so the code always fell through to UV_UNKNOWN.
  fs__readlink_handle, fs__realpath_handle: when uv_utf16_to_wtf8 fails
    with UV_ENOMEM, set ERROR_OUTOFMEMORY via SetLastError and return -1
    so the caller's GetLastError() read is correct. fs__realpath previously
    only checked == -1 and so reported success with req->ptr == NULL.

tty.c:
  uv__tty_move_caret: return -1 after GetConsoleScreenBufferInfo failure
    instead of reading an uninitialized CONSOLE_SCREEN_BUFFER_INFO and
    potentially spinning on retry.
@robobun
robobun force-pushed the farm/e77a46b9/win-error-fixes branch from efe1c66 to 76367c0 Compare July 23, 2026 04:31
@robobun robobun changed the title win: fix error code translation and propagation in pipe/fs/poll/tty win: fix error translation in uv_pipe, pipe write_cb, realpath/readlink, filemap, tty Jul 23, 2026
robobun added a commit to oven-sh/bun that referenced this pull request Jul 23, 2026
oven-sh/libuv#11 was scoped down to the fixes reachable from Bun's own
code; poll.c is no longer touched so the two win-poll patches apply
unchanged.
@dylan-conway
dylan-conway merged commit 2881ce5 into oven-sh:bun Jul 23, 2026
robobun added a commit to oven-sh/bun that referenced this pull request Jul 23, 2026
Now on the bun branch, so the provenance comment is accurate again.
dylan-conway pushed a commit to oven-sh/bun that referenced this pull request Jul 23, 2026
…35209)

Companion to oven-sh/libuv#11 (merged as `2881ce5` on the `bun` branch).

## What changes for Bun

oven-sh/libuv#11 fixes seven Windows error-translation/propagation bugs
found by audit, scoped to those reachable from Bun's own code. Full
breakdown is in [this comment on the libuv
PR](oven-sh/libuv#11 (comment)).

The one user-visible change: on Windows, an async write to a pipe whose
read end has closed now delivers `{code: "EPIPE"}` to the write callback
instead of `{code: "EOF"}`. Before the fix, `uv__process_pipe_write_req`
used the read-side translator, which maps `ERROR_BROKEN_PIPE` /
`ERROR_NO_DATA` to `UV_EOF` / `UV_EAGAIN`; the write-side translator
maps both to `UV_EPIPE`, matching Node and the synchronous `uv_write()`
path.

This also fixes `shell::IOWriter::on_error`'s broken-pipe detection
(`src/runtime/shell/IOWriter.rs:867`), which keys on `E::PIPE` and so
was not firing when the failure arrived via the async write completion.

## Changes

- `scripts/build/deps/libuv.ts`: bump `LIBUV_COMMIT` to `2881ce5` (the
#11 merge) and add #11 to the provenance comment.
- `test/js/bun/spawn/spawn.test.ts`: new test `stdin.end() rejects with
EPIPE when the child exits before consuming the write`. The child reads
one byte and exits, leaving 16MB queued on the parent's stdin (large
enough to exceed macOS socket autotuning and the 64KB Windows named-pipe
buffer), so `end()` is still draining when the read end closes. Verified
fail-before on Windows (`caught.code === "EOF"` with the current
release) and pass on Linux/macOS.

The existing `child_process` `stdin write failure (EPIPE)` test is left
skipped on Windows: its child does `fs.closeSync(0)`, but libuv's
`fs__close` on Windows is a no-op for fd ≤ 2 (`src/win/fs.c:690`), so
the pipe never breaks and the test hangs. That skip is unrelated to #11.

## Not changed (reachable from Bun, strict improvement, no code change
needed)

- `uv_pipe()` (used by `spawn/process.rs:2000`, `sys/lib.rs:4393`,
`install/security_scanner.rs:1129`): previously returned a raw positive
Win32 error on failure; Bun's `ReturnCode` predicate is `< 0`, so that
leaked through as success with garbage fds. Now returns a negative
`UV_E*`. Bun-side check was already correct.
- `uv_fs_realpath` / `uv_fs_readlink` (used by `fs.realpath*`,
`fs.readlink*`, `fs.cp`, `fs.watch`): OOM during UTF-16 conversion now
reports `ENOMEM` instead of success-with-null or a stale error.
- `fs__filemap_ex_filter` (`UV_FS_O_FILEMAP`, exposed via
`fs.constants`): in-page errors now surface the real disk error instead
of `UNKNOWN`.
- `uv__tty_move_caret` (legacy console ANSI emulation): missing `return
-1` added; bypassed on Windows 10+ where Bun enables VT mode at startup.

<!-- robobun:evidence:begin -->

---

**no test proof** · iteration 3 · Platform-specific test-only change;
deferring to CI.

<!-- robobun:evidence:end -->

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
liooil pushed a commit to liooil/poly that referenced this pull request Aug 7, 2026
…#35209)

Companion to oven-sh/libuv#11 (merged as `2881ce5` on the `bun` branch).

## What changes for Bun

oven-sh/libuv#11 fixes seven Windows error-translation/propagation bugs
found by audit, scoped to those reachable from Bun's own code. Full
breakdown is in [this comment on the libuv
PR](oven-sh/libuv#11 (comment)).

The one user-visible change: on Windows, an async write to a pipe whose
read end has closed now delivers `{code: "EPIPE"}` to the write callback
instead of `{code: "EOF"}`. Before the fix, `uv__process_pipe_write_req`
used the read-side translator, which maps `ERROR_BROKEN_PIPE` /
`ERROR_NO_DATA` to `UV_EOF` / `UV_EAGAIN`; the write-side translator
maps both to `UV_EPIPE`, matching Node and the synchronous `uv_write()`
path.

This also fixes `shell::IOWriter::on_error`'s broken-pipe detection
(`src/runtime/shell/IOWriter.rs:867`), which keys on `E::PIPE` and so
was not firing when the failure arrived via the async write completion.

## Changes

- `scripts/build/deps/libuv.ts`: bump `LIBUV_COMMIT` to `2881ce5` (the
#11 merge) and add #11 to the provenance comment.
- `test/js/bun/spawn/spawn.test.ts`: new test `stdin.end() rejects with
EPIPE when the child exits before consuming the write`. The child reads
one byte and exits, leaving 16MB queued on the parent's stdin (large
enough to exceed macOS socket autotuning and the 64KB Windows named-pipe
buffer), so `end()` is still draining when the read end closes. Verified
fail-before on Windows (`caught.code === "EOF"` with the current
release) and pass on Linux/macOS.

The existing `child_process` `stdin write failure (EPIPE)` test is left
skipped on Windows: its child does `fs.closeSync(0)`, but libuv's
`fs__close` on Windows is a no-op for fd ≤ 2 (`src/win/fs.c:690`), so
the pipe never breaks and the test hangs. That skip is unrelated to #11.

## Not changed (reachable from Bun, strict improvement, no code change
needed)

- `uv_pipe()` (used by `spawn/process.rs:2000`, `sys/lib.rs:4393`,
`install/security_scanner.rs:1129`): previously returned a raw positive
Win32 error on failure; Bun's `ReturnCode` predicate is `< 0`, so that
leaked through as success with garbage fds. Now returns a negative
`UV_E*`. Bun-side check was already correct.
- `uv_fs_realpath` / `uv_fs_readlink` (used by `fs.realpath*`,
`fs.readlink*`, `fs.cp`, `fs.watch`): OOM during UTF-16 conversion now
reports `ENOMEM` instead of success-with-null or a stale error.
- `fs__filemap_ex_filter` (`UV_FS_O_FILEMAP`, exposed via
`fs.constants`): in-page errors now surface the real disk error instead
of `UNKNOWN`.
- `uv__tty_move_caret` (legacy console ANSI emulation): missing `return
-1` added; bypassed on Windows 10+ where Bun enables VT mode at startup.

<!-- robobun:evidence:begin -->

---

**no test proof** · iteration 3 · Platform-specific test-only change;
deferring to CI.

<!-- robobun:evidence:end -->

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
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.

2 participants