-
Notifications
You must be signed in to change notification settings - Fork 5k
s3: free the NetworkSink behind writer() via intrusive refcount #34999
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
Open
robobun
wants to merge
7
commits into
main
Choose a base branch
from
farm/b6bf5ab7/s3-networksink-leak
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
51e6126
s3: free the NetworkSink behind writer() via intrusive refcount
robobun abccf8d
release the wrapper's ref in __doClose; address review feedback
robobun b7a1ae2
shorten code comments to three lines
robobun 3451a9f
split .close() release from GC finalize; wrapper_callback derefs dire…
robobun 92969a0
detach the task synchronously in wrapper_callback
robobun 4d2f779
make wrapper_callback teardown unconditional; add FileSink .close() b…
robobun 4c1d8cf
drop stderr-empty assertion in the retained-writer test
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
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,92 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isASAN } from "harness"; | ||
|
|
||
| // LSAN byte-count diff between N and N+20 writers: symbolize=0 keeps each | ||
| // subprocess fast, and one-time at-exit allocations cancel out in the diff. | ||
| // https://github.com/oven-sh/bun/pull/34999 | ||
| async function runWriters(count: number, fail: boolean, finish: "end" | "close") { | ||
| const script = ` | ||
| const server = Bun.serve({ | ||
| port: 0, | ||
| async fetch(req) { | ||
| await req.arrayBuffer(); | ||
| ${ | ||
| fail | ||
| ? `return new Response( | ||
| '<?xml version="1.0" encoding="UTF-8"?><Error><Code>AccessDenied</Code><Message>nope</Message></Error>', | ||
| { status: 403 }, | ||
| );` | ||
| : `return new Response("", { status: 200, headers: { etag: '"e"' } });` | ||
| } | ||
| }, | ||
| }); | ||
| server.unref(); | ||
| const s3 = new Bun.S3Client({ | ||
| accessKeyId: "k", | ||
| secretAccessKey: "s", | ||
| bucket: "b", | ||
| endpoint: \`http://127.0.0.1:\${server.port}\`, | ||
| }); | ||
| process.once("beforeExit", () => { Bun.gc(true); console.log("done"); }); | ||
| for (let i = 0; i < ${count}; i++) { | ||
| const w = s3.file("key-" + i).writer({ retry: 0 }); | ||
| w.write("hello"); | ||
| ${finish === "end" ? `try { await w.end(); } catch {}` : `w.close();`} | ||
| } | ||
| `; | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", script], | ||
| env: { | ||
| ...bunEnv, | ||
| ASAN_OPTIONS: [bunEnv.ASAN_OPTIONS, "detect_leaks=1", "symbolize=0"].filter(Boolean).join(":"), | ||
| // The S3 client does not honor NO_PROXY for writer(), so an inherited | ||
| // proxy would hijack the request to the in-process mock server. | ||
| http_proxy: undefined, | ||
| HTTP_PROXY: undefined, | ||
| https_proxy: undefined, | ||
| HTTPS_PROXY: undefined, | ||
| }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stdout.trim()).toBe("done"); | ||
| return Number(/SUMMARY: AddressSanitizer: (\d+) byte\(s\) leaked/.exec(stderr)?.[1] ?? 0); | ||
| } | ||
|
|
||
| describe.skipIf(!isASAN)("S3 writer() NetworkSink is freed", () => { | ||
| for (const fail of [true, false]) { | ||
| for (const finish of ["end", "close"] as const) { | ||
| test.concurrent(`via .${finish}() when the upload ${fail ? "fails" : "succeeds"}`, async () => { | ||
| const small = await runWriters(2, fail, finish); | ||
| const large = await runWriters(22, fail, finish); | ||
| // Before the fix the diff is >= 20 * sizeof(NetworkSink) ~= 1600. | ||
| expect(large - small).toBeLessThan(400); | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test("S3 writer() unrefs the event loop once end() resolves, even if the writer is retained", async () => { | ||
| const script = ` | ||
| const server = Bun.serve({ | ||
| port: 0, | ||
| async fetch(req) { await req.arrayBuffer(); return new Response("", { status: 200, headers: { etag: '"e"' } }); }, | ||
| }); | ||
| server.unref(); | ||
| const s3 = new Bun.S3Client({ accessKeyId: "k", secretAccessKey: "s", bucket: "b", endpoint: \`http://127.0.0.1:\${server.port}\` }); | ||
| const w = s3.file("k").writer({ retry: 0 }); | ||
| w.write("hi"); | ||
| await w.end(); | ||
| globalThis.keep = w; | ||
| process.once("beforeExit", () => console.log("beforeExit")); | ||
| `; | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", script], | ||
| env: { ...bunEnv, http_proxy: undefined, HTTP_PROXY: undefined, https_proxy: undefined, HTTPS_PROXY: undefined }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, , exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect({ stdout: stdout.trim(), exitCode }).toEqual({ stdout: "beforeExit", exitCode: 0 }); | ||
| }); |
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
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.