Repository navigation
node:fs: open files with O_CLOEXEC, probe trace_marker only under BUN_TRACE #41460
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
Merged
+94
−2
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9c82801
node:fs: open files with O_CLOEXEC, probe trace_marker only under BUN…
robobun 6ce53c0
node:fs: add O_CLOEXEC to readFile, writeFile and realpath opens
robobun 207586b
test: reject on stream error, make the O_APPEND check observable
robobun 684c3a2
test: check O_APPEND without a positional write, pwrite ignores it on…
robobun 0a49c24
sys: make O_CLOEXEC the default for every open
robobun fa481bc
sys: shorten the openat doc comment
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { dlopen } from "bun:ffi"; | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { isLinux, isMacOS, tempDir } from "harness"; | ||
| import fs from "node:fs"; | ||
| import { join } from "node:path"; | ||
|
|
||
| // libuv ORs O_CLOEXEC into every fs open, so a descriptor a script opens | ||
| // through node:fs never leaks into a child that a native addon forks | ||
| // outside of Bun.spawn (system(3), forkpty, posix_spawn). | ||
| function hasCloexec(fd: number): boolean { | ||
| if (isLinux) { | ||
| const info = fs.readFileSync(`/proc/self/fdinfo/${fd}`, "utf8"); | ||
| const flags = parseInt(info.match(/^flags:\s*(\d+)/m)![1], 8); | ||
| return (flags & 0o2000000) !== 0; | ||
| } | ||
| const libc = dlopen("/usr/lib/libSystem.B.dylib", { | ||
| fcntl: { args: ["int", "int"], returns: "int" }, | ||
| }); | ||
| try { | ||
| const F_GETFD = 1; | ||
| const FD_CLOEXEC = 1; | ||
| return (libc.symbols.fcntl(fd, F_GETFD) & FD_CLOEXEC) !== 0; | ||
| } finally { | ||
| libc.close(); | ||
| } | ||
| } | ||
|
|
||
| describe.skipIf(!isLinux && !isMacOS)("node:fs opens set O_CLOEXEC", () => { | ||
| test("fs.openSync", () => { | ||
| using dir = tempDir("fs-cloexec", { "a.txt": "hello" }); | ||
| for (const flags of ["r", "w", "a", "r+", fs.constants.O_RDONLY]) { | ||
| const fd = fs.openSync(join(String(dir), "a.txt"), flags); | ||
| try { | ||
| expect(hasCloexec(fd)).toBe(true); | ||
| } finally { | ||
| fs.closeSync(fd); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test("fs.promises.open", async () => { | ||
| using dir = tempDir("fs-cloexec", { "a.txt": "hello" }); | ||
| await using handle = await fs.promises.open(join(String(dir), "a.txt"), "r+"); | ||
| expect(hasCloexec(handle.fd)).toBe(true); | ||
| }); | ||
|
|
||
| test("fs.open callback", async () => { | ||
| using dir = tempDir("fs-cloexec", { "a.txt": "hello" }); | ||
| const fd = await new Promise<number>((resolve, reject) => | ||
| fs.open(join(String(dir), "a.txt"), "r", (err, fd) => (err ? reject(err) : resolve(fd))), | ||
| ); | ||
| try { | ||
| expect(hasCloexec(fd)).toBe(true); | ||
| } finally { | ||
| fs.closeSync(fd); | ||
| } | ||
| }); | ||
|
|
||
| test("fs.createReadStream and fs.createWriteStream", async () => { | ||
| using dir = tempDir("fs-cloexec", { "a.txt": "hello" }); | ||
| const rs = fs.createReadStream(join(String(dir), "a.txt")); | ||
| const ws = fs.createWriteStream(join(String(dir), "b.txt")); | ||
| const [rfd, wfd] = await Promise.all([ | ||
| new Promise<number>((resolve, reject) => rs.once("open", resolve).once("error", reject)), | ||
| new Promise<number>((resolve, reject) => ws.once("open", resolve).once("error", reject)), | ||
| ]); | ||
| try { | ||
| expect(hasCloexec(rfd)).toBe(true); | ||
| expect(hasCloexec(wfd)).toBe(true); | ||
| } finally { | ||
| rs.close(); | ||
| ws.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("user flags are kept", () => { | ||
| using dir = tempDir("fs-cloexec", { "log.txt": "ab" }); | ||
| const fd = fs.openSync(join(String(dir), "log.txt"), fs.constants.O_WRONLY | fs.constants.O_APPEND); | ||
| try { | ||
| // Without O_APPEND the write would land at offset 0 and give "xb". | ||
| fs.writeSync(fd, "x"); | ||
| expect(hasCloexec(fd)).toBe(true); | ||
| } finally { | ||
| fs.closeSync(fd); | ||
| } | ||
| expect(fs.readFileSync(join(String(dir), "log.txt"), "utf8")).toBe("abx"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.