-
Notifications
You must be signed in to change notification settings - Fork 5.1k
s3: fix double free of a multipart part's bytes on worker terminate #43709
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
5
commits into
main
Choose a base branch
from
robobun/1bc79006/s3-multipart-part-owns-buffer
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
5 commits
Select commit
Hold shift + click to select a range
f1fb53a
s3: a multipart upload part owns the bytes it sends
robobun 77c5c0a
s3: keep a part's bytes alive for the request perform lends them to
robobun 0c3bdf3
s3: shorten the comments on the multipart part's bytes
robobun e0bca9c
s3: name a new part's bytes with an enum instead of a closure
robobun 0765a97
test: fail the s3 upload terminate test when a worker fails before it…
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // A streaming multipart upload buffers bytes until it has a part, then hands | ||
| // that part the bytes to send. When the buffer held exactly one part it handed | ||
| // the part the buffer's own allocation and gave the buffer up only after the | ||
| // part's request had been started, so a request that failed inside the enqueue | ||
| // left two owners of those bytes: the part, which the failure frees, and the | ||
| // local buffer, freed again when the error unwinds. | ||
| // | ||
| // terminate() is what makes the enqueue fail that way. The S3 client sends | ||
| // nothing new for a VM that is stopping, so it fails the request in place, and | ||
| // the upload reports that failure to its promise. Each worker below starts its | ||
| // uploads and is terminated as its first parts are being enqueued. A run that | ||
| // frees a part's bytes twice aborts the process under ASAN, so a clean exit is | ||
| // the check. | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
|
|
||
| // The terminate has to land while a worker enqueues a part, so each worker | ||
| // starts several uploads and several workers run at once. | ||
| const WORKERS = 4; | ||
| const UPLOADS_PER_WORKER = 8; | ||
| // The minimum S3 part size. One chunk is one part, so the first chunk of each | ||
| // upload fills the buffer with exactly one part. | ||
| const PART_SIZE = 5 * 1024 * 1024; | ||
|
|
||
| const worker = /* js */ ` | ||
| const { parentPort, workerData } = require("node:worker_threads"); | ||
| const client = new Bun.S3Client({ | ||
| endpoint: workerData.endpoint, | ||
| bucket: "bucket", | ||
| accessKeyId: "key", | ||
| secretAccessKey: "secret", | ||
| region: "us-east-1", | ||
| }); | ||
| const chunk = new Uint8Array(${PART_SIZE}).fill(97); | ||
| for (let i = 0; i < ${UPLOADS_PER_WORKER}; i++) { | ||
| const stream = new ReadableStream({ pull(controller) { controller.enqueue(chunk); } }); | ||
| client | ||
| .write(workerData.key + "-" + i, new Response(stream), { partSize: ${PART_SIZE}, queueSize: 1, retry: 0 }) | ||
| .catch(() => {}); | ||
| } | ||
| parentPort.postMessage("started"); | ||
| `; | ||
|
|
||
| const host = /* js */ ` | ||
| const { Worker } = require("node:worker_threads"); | ||
| const server = Bun.serve({ | ||
| port: 0, | ||
| async fetch(req) { | ||
| const url = new URL(req.url); | ||
| if (req.method === "POST" && url.searchParams.has("uploads")) { | ||
| return new Response( | ||
| "<InitiateMultipartUploadResult><UploadId>upload-1</UploadId></InitiateMultipartUploadResult>", | ||
| { headers: { "content-type": "application/xml" } }, | ||
| ); | ||
| } | ||
| if (req.method === "PUT") { | ||
| try { await req.arrayBuffer(); } catch {} | ||
| return new Response("", { headers: { ETag: '"etag"' } }); | ||
| } | ||
| try { await req.text(); } catch {} | ||
| return new Response( | ||
| '<CompleteMultipartUploadResult><ETag>"etag-1"</ETag></CompleteMultipartUploadResult>', | ||
| { headers: { "content-type": "application/xml" } }, | ||
| ); | ||
| }, | ||
| }); | ||
| await Promise.all( | ||
| Array.from({ length: ${WORKERS} }, (_, i) => { | ||
| const w = new Worker(${JSON.stringify(worker)}, { | ||
| eval: true, | ||
| workerData: { endpoint: server.url.origin, key: "key-" + i }, | ||
| }); | ||
| // Terminate as soon as this worker reports that its uploads are started. | ||
| // A worker that fails before that fails the run instead of hanging it. | ||
| return new Promise((resolve, reject) => { | ||
| w.once("message", resolve); | ||
| w.once("error", reject); | ||
| w.once("exit", code => reject(new Error("worker exited with code " + code + " before it started its uploads"))); | ||
| }).then(() => w.terminate()); | ||
| }), | ||
| ).catch(error => { | ||
| console.error(error); | ||
| process.exit(1); | ||
| }); | ||
| server.stop(true); | ||
| console.log("ok"); | ||
| `; | ||
|
|
||
| test("terminate() while a multipart upload enqueues a part frees the part's bytes once", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", host], | ||
| env: { | ||
| ...bunEnv, | ||
| // An inherited proxy would take the requests away from the loopback | ||
| // stand-in. | ||
| HTTP_PROXY: undefined, | ||
| HTTPS_PROXY: undefined, | ||
| ALL_PROXY: undefined, | ||
| http_proxy: undefined, | ||
| https_proxy: undefined, | ||
| all_proxy: undefined, | ||
| // An upload still open when its worker goes is not freed (#39692). That | ||
| // leak is not what this checks, and LeakSanitizer would report it. | ||
| ASAN_OPTIONS: [bunEnv.ASAN_OPTIONS, "detect_leaks=0"].filter(Boolean).join(":"), | ||
| }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect({ stdout: stdout.trim(), stderr: stderr.trim() }).toEqual({ stdout: "ok", stderr: "" }); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
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.