Repository navigation
sys, node:fs: close reports every errno except EINTR and EINPROGRESS #44330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: robobun/a27cb684/sys-single-shot-close
Are you sure you want to change the base?
Changes from 3 commits
22c65c8
a113f11
c921d2b
410496a
c37bac7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1638,7 +1638,11 @@ async function writeFileAsyncIterator(fdOrPath, iterable, optionsOrEncoding, fla | |
| } | ||
|
|
||
| if (signal?.aborted) { | ||
| if (mustClose) await fs.close(fdOrPath); | ||
| if (mustClose) { | ||
| try { | ||
| await fs.close(fdOrPath); | ||
| } catch {} | ||
| } | ||
| throw $makeAbortError(undefined, { cause: signal.reason }); | ||
| } | ||
|
|
||
|
|
@@ -1660,7 +1664,12 @@ async function writeFileAsyncIterator(fdOrPath, iterable, optionsOrEncoding, fla | |
| } catch {} | ||
| } | ||
|
|
||
| await fs.close(fdOrPath); | ||
| try { | ||
| await fs.close(fdOrPath); | ||
|
Comment on lines
+1671
to
+1672
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟣 pre-existing, not blocking: pre-existing: users writing to SMB (or a FUSE daemon that reports a flush error once) with fs.promises.writeFile(path, iterable) still get a resolved promise after a lost write, which Node rejects with ENOSPC. writeFileAsyncIteratorInner writes through Bun.file(fd).writer(), a dup of the fd (src/io/openForWriting.rs:34), and its close drops the errno; cifs clears the error on the first close, so fs.close at src/js/node/fs.promises.ts:1668 sees 0. Fix: carry the sink's close error (or write through the original fd so one close reports), covering FileHandle.writeFile(iterable) too. The shim faults every close, so the test cannot see this. Why this was flaggedA user calls fs.promises.writeFile(path, asyncIterable) on a cifs mount that is out of space; write(2) succeeds and the error is reported at close. writeFileAsyncIteratorInner (src/js/node/fs.promises.ts:1574) creates Bun.file(fd).writer(), which open_for_writing turns into dup_with_flags(fd, 0) (src/io/openForWriting.rs:34). Verification: pre-existing. Trigger: fs.promises.writeFile(path, asyncIterable) on a cifs mount, when the write error surfaces at close. The sink writes to a dup (src/io/openForWriting.rs:34); Closer::run drops the errno (src/io/lib.rs:2224); cifs_flush clears the error, so fs.close at fs.promises.ts:1668 returns 0 and the promise resolves after a lost write. The shim fails every close, so the dup also fails.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. The writer of This PR does not change that. Downsides lists The comment of the fixture now says that the shim fails both closes, and that on NFS and SMB only the first one gets the error. |
||
| } catch (closeError) { | ||
| // As node's handleFdClose, the error of the write keeps its place: https://github.com/nodejs/node/blob/v26.3.0/lib/internal/fs/promises.js#L1075-L1086 | ||
| error = error ? require("internal/errors").aggregateTwoErrors(closeError as Error, error) : (closeError as Error); | ||
| } | ||
| } | ||
|
|
||
| // Abort signal shadows other errors | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -862,8 +862,9 @@ class WASI { | |
| }), | ||
| fd_close: wrap(fd => { | ||
| const stats = CHECK_FD(fd, BigInt(0)); | ||
| fs.closeSync(stats.real); | ||
| // close(2) releases the descriptor even when it reports an error, so the entry goes first. uvwasi keeps it: https://github.com/nodejs/node/blob/v26.3.0/deps/uvwasi/src/uvwasi.c#L813-L816 | ||
| this.FD_MAP.delete(fd); | ||
| fs.closeSync(stats.real); | ||
| return WASI_ESUCCESS; | ||
| }), | ||
| fd_datasync: wrap(fd => { | ||
|
|
@@ -1167,9 +1168,9 @@ class WASI { | |
| fd_renumber: wrap((from, to) => { | ||
| const fromEntry = CHECK_FD(from, BigInt(0)); | ||
| const toEntry = CHECK_FD(to, BigInt(0)); | ||
| fs.closeSync(fromEntry.real); | ||
| this.FD_MAP.set(from, toEntry); | ||
| this.FD_MAP.delete(to); | ||
| fs.closeSync(fromEntry.real); | ||
|
Comment on lines
1169
to
+1173
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟣 pre-existing, not blocking: pre-existing: WASI guests calling fd_renumber(from, to) get the two descriptors swapped the wrong way, so the file they meant to keep is closed and Why this was flaggedA guest calls fd_renumber(5, 3) through wasiImport, expecting fd 3 to afterwards refer to the file that fd 5 held and fd 5 to be released (wasi_snapshot_preview1 fd_renumber; uvwasi_fd_table_renumber(dst=to, src=from) closes dst's file and copies src into dst). In src/js/node/wasi.ts:1169-1173 the code instead keeps entry 5 pointing at the old fd 3 file, deletes entry 3, and closes fromEntry.real, the file the guest wanted to keep. The guest's next read or write on fd 3 returns WASI_EBADF, and any further use of fd 5 hits the file that should have been discarded, while the real host descriptor of the kept file is already closed. The base branch behaves the same way (only the closeSync call moved), so this is pre-existing; the new test test/js/bun/wasm/wasi.test.js:298-306 pins the inverted result ({ from: secondEntry, to: false }), so it locks the bug in rather than guarding against it. Verification: pre-existing — the base branch already has the inverted mapping; this PR reorders the body and adds a test that pins the inverted behavior. Triggering condition: any WASI guest calling fd_renumber(from, to). At src/js/node/wasi.ts:1168-1175
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, and it is older than this PR. uvwasi closes This PR only moves the map update before the close, so I left the direction for a change of its own. The test no longer pins it: it now asserts that no guest number maps to the descriptor that the call closed. The Notes list the inversion under "Found, not changed here". |
||
| return WASI_ESUCCESS; | ||
| }), | ||
| fd_seek: wrap((fd, offset, whence, newOffsetPtr) => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1951,12 +1951,13 @@ mod posix_impl { | |
| if rc < 0 { Err(last_errno()) } else { Ok(()) } | ||
| } | ||
| } | ||
| /// Closes `fd` and returns the error that callers see. Only EBADF surfaces. | ||
| /// Closes `fd` and returns the error for the caller. A file system can report a write error first at close. | ||
| #[inline] | ||
| pub(crate) fn close_error(fd: Fd) -> Option<Error> { | ||
| match close_once(fd) { | ||
| Err(libc::EBADF) => Some(Error::from_code_int(libc::EBADF, Tag::close).with_fd(fd)), | ||
| _ => None, | ||
| // A close in progress: https://github.com/nodejs/node/blob/v26.3.0/deps/uv/src/unix/fs.c#L158-L167 | ||
| Ok(()) | Err(libc::EINTR | libc::EINPROGRESS) => None, | ||
| Err(errno) => Some(Error::from_code_int(errno, Tag::close).with_fd(fd)), | ||
|
Comment on lines
+1959
to
+1960
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟣 pre-existing, not blocking: Apps that call Why this was flaggedUser code opens a file on NFS/SMB/FUSE, writes, and calls Verification: src/sys/lib.rs:1957-1961 now returns
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. Node throws the error from To move it to the callback arm changes what an |
||
| } | ||
| } | ||
| pub fn close(fd: Fd) -> Maybe<()> { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // LD_PRELOAD shim that stands in for a file system that reports a write error | ||
| // at close(2), as NFS, SMB and FUSE can do for ENOSPC, EDQUOT and EIO. | ||
| // | ||
| // It acts on a file whose name ends with ".<errno>.close-fault", for example | ||
| // "copy.28.close-fault" for ENOSPC, through a descriptor that is open for | ||
| // writing. A close of such a descriptor releases it, as the kernel does, then | ||
| // fails with that errno and writes one line to stderr. With | ||
| // CLOSE_FAULT_READERS set, a descriptor that is open for reading counts too | ||
| // (FUSE calls flush for each close). | ||
| // | ||
| // bun closes through syscall(SYS_close, fd), so syscall() is the symbol to | ||
| // interpose. glibc only. | ||
| #define _GNU_SOURCE | ||
| #include <ctype.h> | ||
| #include <dlfcn.h> | ||
| #include <errno.h> | ||
| #include <fcntl.h> | ||
| #include <stdarg.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <sys/syscall.h> | ||
| #include <unistd.h> | ||
|
|
||
| #define SUFFIX ".close-fault" | ||
|
|
||
| static long (*next_syscall)(long, ...); | ||
|
|
||
| // The errno that the close of `fd` reports, or 0. | ||
| static int fault_for(int fd) { | ||
| char link[64], target[4096]; | ||
| if (fd < 3) return 0; | ||
| int flags = fcntl(fd, F_GETFL); | ||
| if (flags < 0) return 0; | ||
| if ((flags & O_ACCMODE) == O_RDONLY && !getenv("CLOSE_FAULT_READERS")) return 0; | ||
| snprintf(link, sizeof link, "/proc/self/fd/%d", fd); | ||
| ssize_t n = readlink(link, target, sizeof target - 1); | ||
| if (n <= (ssize_t)strlen(SUFFIX)) return 0; | ||
| target[n] = 0; | ||
| char *suffix = target + n - strlen(SUFFIX); | ||
| if (strcmp(suffix, SUFFIX) != 0) return 0; | ||
| char *digits = suffix; | ||
| while (digits > target && isdigit((unsigned char)digits[-1])) digits--; | ||
| if (digits == suffix || digits == target || digits[-1] != '.') return 0; | ||
| return atoi(digits); | ||
| } | ||
|
|
||
| long syscall(long nr, ...) { | ||
| va_list ap; | ||
| va_start(ap, nr); | ||
| long a1 = va_arg(ap, long), a2 = va_arg(ap, long), a3 = va_arg(ap, long); | ||
| long a4 = va_arg(ap, long), a5 = va_arg(ap, long), a6 = va_arg(ap, long); | ||
| va_end(ap); | ||
| if (!next_syscall) next_syscall = dlsym(RTLD_NEXT, "syscall"); | ||
| if (nr != SYS_close) return next_syscall(nr, a1, a2, a3, a4, a5, a6); | ||
|
|
||
| int fault = fault_for((int)a1); | ||
| long rc = next_syscall(nr, a1); | ||
| if (fault == 0 || rc != 0) return rc; | ||
| dprintf(2, "close-fault: errno %d\n", fault); | ||
| errno = fault; | ||
| return -1; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.