-
Notifications
You must be signed in to change notification settings - Fork 5k
s3: S3File.writer().end(error) aborts the upload instead of committing #33681
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
9
commits into
main
Choose a base branch
from
farm/b1f3dbf2/s3-writer-end-error-aborts
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 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
82dda2c
s3: S3File.writer().end(error) aborts the upload instead of committing
robobun 12d8fab
[autofix.ci] apply automated fixes
autofix-ci[bot] ab9cb3b
Address review: root returned promise via EnsureStillAlive; drop stri…
robobun 1a3facb
fail_from_js: detach task and swap in no-op callback before fail()
robobun be1c53d
multipart: roll back UploadId received after fail()
robobun 72562c7
ci: retrigger
robobun 1af7195
multipart: extract parse_upload_id helper
robobun 7cd5724
Only treat Error-like end() argument as abort; align pipeTo with pipe…
robobun 48f520f
isAnyError: recognize DOMException
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
|
|
||
| // S3File.writer().end(new Error(...)) must abort the multipart upload and | ||
| // reject. Previously the error argument was ignored: the buffered tail was | ||
| // uploaded, CompleteMultipartUpload was sent, and the promise resolved, | ||
| // silently publishing a truncated object. | ||
|
|
||
| const fixture = ` | ||
| import * as net from "node:net"; | ||
|
|
||
| const reqs: string[] = []; | ||
| const committed = new Set<string>(); | ||
| let nextId = 0; | ||
|
|
||
| const server = net.createServer(sock => { | ||
| let buf = Buffer.alloc(0); | ||
| sock.on("error", () => {}); | ||
| sock.on("data", chunk => { | ||
| buf = Buffer.concat([buf, chunk]); | ||
| for (;;) { | ||
| const headerEnd = buf.indexOf("\\r\\n\\r\\n"); | ||
| if (headerEnd < 0) return; | ||
| const head = buf.toString("latin1", 0, headerEnd); | ||
| const len = Number(/^content-length: *(\\d+)/im.exec(head)?.[1] ?? 0); | ||
| if (buf.length < headerEnd + 4 + len) return; | ||
| const body = Buffer.from(buf.subarray(headerEnd + 4, headerEnd + 4 + len)); | ||
| buf = buf.subarray(headerEnd + 4 + len); | ||
| const [method, target] = head.split("\\r\\n")[0].split(" "); | ||
| const key = decodeURIComponent(target.split("?")[0]).replace(/^\\/bucket\\//, ""); | ||
| const q = new URLSearchParams(target.split("?")[1] ?? ""); | ||
| let status = 200, out = "", extra = ""; | ||
| if (method === "POST" && q.has("uploads")) { | ||
| const id = "up" + ++nextId; | ||
| reqs.push("INIT " + key); | ||
| out = '<?xml version="1.0"?><InitiateMultipartUploadResult><Bucket>bucket</Bucket><Key>k</Key><UploadId>' + id + '</UploadId></InitiateMultipartUploadResult>'; | ||
| } else if (method === "PUT" && q.has("partNumber")) { | ||
| reqs.push("PART " + key + " " + q.get("partNumber") + " " + body.length); | ||
| extra = 'ETag: "p' + q.get("partNumber") + '"\\r\\n'; | ||
| } else if (method === "POST" && q.has("uploadId")) { | ||
| reqs.push("COMMIT " + key); | ||
| committed.add(key); | ||
| out = '<?xml version="1.0"?><CompleteMultipartUploadResult><Key>k</Key><ETag>"e"</ETag></CompleteMultipartUploadResult>'; | ||
| } else if (method === "DELETE" && q.has("uploadId")) { | ||
| reqs.push("ABORT " + key); | ||
| status = 204; | ||
| } else if (method === "PUT") { | ||
| reqs.push("PUT " + key + " " + body.length); | ||
| committed.add(key); | ||
| } | ||
| const b = Buffer.from(out); | ||
| sock.write("HTTP/1.1 " + status + " X\\r\\n" + extra + "Connection: keep-alive\\r\\nContent-Length: " + (status === 204 ? 0 : b.length) + "\\r\\n\\r\\n"); | ||
| if (status !== 204 && b.length) sock.write(b); | ||
| } | ||
| }); | ||
| }); | ||
| await new Promise<void>(r => server.listen(0, "127.0.0.1", () => r())); | ||
| const port = (server.address() as net.AddressInfo).port; | ||
|
|
||
| const s3 = new Bun.S3Client({ | ||
| endpoint: "http://127.0.0.1:" + port, | ||
| bucket: "bucket", | ||
| accessKeyId: "AK", | ||
| secretAccessKey: "SK", | ||
| region: "us-east-1", | ||
| }); | ||
| const PART = 5 * 1024 * 1024; | ||
|
|
||
| async function settle(p: Promise<unknown>) { | ||
| try { | ||
| await p; | ||
| return "resolved"; | ||
| } catch (e: any) { | ||
| return "rejected:" + e.message; | ||
| } | ||
| } | ||
|
|
||
| function summary(key: string) { | ||
| return { | ||
| committed: committed.has(key), | ||
| commits: reqs.filter(r => r.startsWith("COMMIT ")).length, | ||
| puts: reqs.filter(r => r.startsWith("PUT ")).length, | ||
| aborts: reqs.filter(r => r.startsWith("ABORT ")).length, | ||
| inits: reqs.filter(r => r.startsWith("INIT ")).length, | ||
| }; | ||
| } | ||
|
|
||
| async function waitFor(predicate: () => boolean, limit = 500) { | ||
| for (let i = 0; i < limit && !predicate(); i++) await Bun.sleep(10); | ||
| } | ||
|
|
||
| const results: Record<string, unknown> = {}; | ||
|
|
||
| { | ||
| // Multipart already initiated and a part uploaded; then the source fails. | ||
| reqs.length = 0; | ||
| const w = s3.file("multi.bin").writer({ partSize: PART, queueSize: 1, retry: 0 }); | ||
| w.write(new Uint8Array(PART)); | ||
| await w.flush(); | ||
| w.write(new Uint8Array(100)); | ||
| const outcome = await settle(w.end(new Error("source failed mid-stream"))); | ||
| await waitFor(() => reqs.some(r => r.startsWith("ABORT ") || r.startsWith("COMMIT "))); | ||
| results.multipart = { outcome, ...summary("multi.bin") }; | ||
| } | ||
|
|
||
| { | ||
| // Buffered data below partSize; multipart never started. | ||
| reqs.length = 0; | ||
| const w = s3.file("single.bin").writer({ partSize: PART, queueSize: 1, retry: 0 }); | ||
| w.write(new Uint8Array(100)); | ||
| const outcome = await settle(w.end(new Error("source failed mid-stream"))); | ||
| // A buggy build uploads the buffered bytes as a single-file PUT and only | ||
| // then settles, so reqs already reflects it here. Give any late request a | ||
| // bounded window to appear; the fixed build leaves reqs empty. | ||
| await waitFor(() => reqs.length > 0, 50); | ||
| results.single = { outcome, ...summary("single.bin") }; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| { | ||
| // Control: end() with no error still commits. | ||
| reqs.length = 0; | ||
| const w = s3.file("ok.bin").writer({ partSize: PART, queueSize: 1, retry: 0 }); | ||
| w.write(new Uint8Array(PART)); | ||
| w.write(new Uint8Array(100)); | ||
| const outcome = await settle(w.end()); | ||
| await waitFor(() => reqs.some(r => r.startsWith("COMMIT "))); | ||
| results.ok = { outcome, ...summary("ok.bin") }; | ||
| } | ||
|
|
||
| console.log(JSON.stringify(results)); | ||
| server.close(); | ||
| process.exit(0); | ||
| `; | ||
|
|
||
| test("S3File.writer().end(error) aborts the upload and rejects", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", fixture], | ||
| env: { | ||
| ...bunEnv, | ||
| HTTP_PROXY: undefined, | ||
| HTTPS_PROXY: undefined, | ||
| http_proxy: undefined, | ||
| https_proxy: undefined, | ||
| }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| let result; | ||
| try { | ||
| result = JSON.parse(stdout.trim()); | ||
| } catch { | ||
| throw new Error(`fixture did not emit JSON\nstdout: ${stdout}\nstderr: ${stderr}`); | ||
| } | ||
|
|
||
| // After a part has been uploaded, end(error) must reject with the caller's | ||
| // error, send AbortMultipartUpload, and never send CompleteMultipartUpload. | ||
| expect(result.multipart).toEqual({ | ||
| outcome: "rejected:source failed mid-stream", | ||
| committed: false, | ||
| commits: 0, | ||
| puts: 0, | ||
| aborts: 1, | ||
| inits: 1, | ||
| }); | ||
|
|
||
| // Before anything is sent, end(error) must reject without uploading the | ||
| // buffered bytes as a single-file PUT. | ||
| expect(result.single).toEqual({ | ||
| outcome: "rejected:source failed mid-stream", | ||
| committed: false, | ||
| commits: 0, | ||
| puts: 0, | ||
| aborts: 0, | ||
| inits: 0, | ||
| }); | ||
|
|
||
| // end() with no error still commits normally. | ||
| expect(result.ok).toEqual({ | ||
| outcome: "resolved", | ||
| committed: true, | ||
| commits: 1, | ||
| puts: 0, | ||
| aborts: 0, | ||
| inits: 1, | ||
| }); | ||
|
|
||
| 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.