From fbee5c696988489bfa39ba0c2cf471937654a8d4 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 17 Sep 2026 08:50:58 +0000 Subject: [PATCH 1/3] s3: stop retrying an AbortMultipartUpload that succeeded When a multipart upload fails, the writer sends AbortMultipartUpload to delete the stored parts. The success answer to that request is 204 No Content (AWS service model, MinIO). The rollback registered its callback as S3Callback::Upload, and that arm of on_response accepts only 200. So the handler saw each 204 as a failure and sent the abort again: 1 + retry requests for each failed upload, 4 with the default. Register the rollback as S3Callback::Delete. That arm accepts 200 and 204 as success. It reports 404 as NotFound, and the handler stops there too: the upload id is already gone. Every other failure still retries. --- src/runtime/webcore/s3/multipart.rs | 13 ++-- test/js/bun/s3/s3.test.ts | 99 +++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/src/runtime/webcore/s3/multipart.rs b/src/runtime/webcore/s3/multipart.rs index 46f7e67acb4c..07efdfb708e4 100644 --- a/src/runtime/webcore/s3/multipart.rs +++ b/src/runtime/webcore/s3/multipart.rs @@ -113,8 +113,8 @@ use bun_s3_signing::storage_class::StorageClass; // re-export hub instead. use crate::webcore::s3::multipart_options::MultiPartUploadOptions; use crate::webcore::s3::simple_request::{ - self as s3_simple_request, S3CommitResult, S3DownloadResult, S3PartResult, S3UploadResult, - execute_simple_s3_request, + self as s3_simple_request, S3CommitResult, S3DeleteResult, S3DownloadResult, S3PartResult, + S3UploadResult, execute_simple_s3_request, }; use crate::webcore::s3::xml_response; use bun_collections::index_sort; @@ -758,7 +758,7 @@ impl MultiPartUpload { /// We do a best effort to rollback the multipart upload, if it fails we will retry, if it still we just deinit the upload pub(crate) fn on_rollback_multi_part_request( - result: S3UploadResult, + result: S3DeleteResult, this: *mut c_void, ) -> bun_jsc::JsResult<()> { let this = this.cast::(); @@ -771,7 +771,7 @@ impl MultiPartUpload { BStr::new(self_.upload_id.get()) ); match result { - S3UploadResult::Failure(_err) => { + S3DeleteResult::Failure(_err) => { let mut options = self_.options.get(); if options.retry > 0 { options.retry -= 1; @@ -783,7 +783,8 @@ impl MultiPartUpload { MultiPartUpload::deref_(this); Ok(()) } - S3UploadResult::Success => { + // NotFound: the upload id is already gone, a retry cannot change that. + S3DeleteResult::Success | S3DeleteResult::NotFound(_) => { MultiPartUpload::deref_(this); Ok(()) } @@ -845,7 +846,7 @@ impl MultiPartUpload { request_payer: self.request_payer, ..Default::default() }, - s3_simple_request::S3Callback::Upload(Self::on_rollback_multi_part_request), + s3_simple_request::S3Callback::Delete(Self::on_rollback_multi_part_request), self.as_ctx_ptr(), ) } diff --git a/test/js/bun/s3/s3.test.ts b/test/js/bun/s3/s3.test.ts index ff6001c67df8..51905c1da0ef 100644 --- a/test/js/bun/s3/s3.test.ts +++ b/test/js/bun/s3/s3.test.ts @@ -1802,6 +1802,105 @@ describe("s3 multipart upload id validation", () => { }, 60_000); }); +describe.concurrent("s3 multipart upload rollback", () => { + // A multipart upload that fails is rolled back with AbortMultipartUpload + // (DELETE ?uploadId=). S3 answers that request with 204 No Content. + // + // The stub fails every part, so the writer gives up and rolls back. It + // records each request it gets. The rollback request keeps the event loop + // alive, so the list the child prints at exit is final. + function fixture(abort: { status: number; body: string | null }) { + return ` + const abort = ${JSON.stringify(abort)}; + const requests = []; + const server = Bun.serve({ + port: 0, + async fetch(req) { + const url = new URL(req.url); + await req.arrayBuffer(); + if (req.method === "POST" && url.searchParams.has("uploads")) { + requests.push("initiate"); + return new Response( + "my_bucketobjupload-1", + { headers: { "Content-Type": "application/xml" } }, + ); + } + if (req.method === "PUT" && url.searchParams.has("partNumber")) { + requests.push("part"); + return new Response( + "InternalErrorWe encountered an internal error. Please try again.", + { status: 500, headers: { "Content-Type": "application/xml" } }, + ); + } + if (req.method === "DELETE" && url.searchParams.get("uploadId") === "upload-1") { + requests.push("abort"); + return new Response(abort.body, { status: abort.status, headers: { "Content-Type": "application/xml" } }); + } + requests.push(req.method + " " + url.pathname + url.search); + return new Response(null, { status: 400 }); + }, + }); + // Only the upload may keep the child alive. + server.unref(); + + const client = new Bun.S3Client({ + accessKeyId: "test", + secretAccessKey: "test", + region: "eu-west-3", + bucket: "my_bucket", + endpoint: \`http://127.0.0.1:\${server.port}\`, + }); + // Exactly one part, so the order of the requests is fixed. + const writer = client.file("obj").writer({ partSize: 5 * 1024 * 1024, retry: 3 }); + writer.write(new Uint8Array(5 * 1024 * 1024)); + const outcome = await writer.end().then( + () => "resolved", + e => "rejected " + e.code, + ); + process.on("exit", () => console.log(JSON.stringify({ outcome, requests }))); + `; + } + + it.each([ + // The abort worked. + ["204 No Content", 1, { status: 204, body: null }], + // The upload id is already gone. A retry cannot change that. + [ + "404 NoSuchUpload", + 1, + { + status: 404, + body: "NoSuchUploadThe specified upload does not exist.", + }, + ], + // A real failure: the best-effort rollback still retries, 1 + retry times. + [ + "500 InternalError", + 4, + { + status: 500, + body: "InternalErrorWe encountered an internal error. Please try again.", + }, + ], + ])("AbortMultipartUpload answered with %s is sent %d time(s)", async (_, aborts, abort) => { + await using proc = Bun.spawn({ + cmd: [bunExe(), "-e", fixture(abort)], + // The S3 client honors the proxy environment; the stub is on loopback. + 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]); + expect(stderr).toBe(""); + expect(JSON.parse(stdout.trim())).toEqual({ + outcome: "rejected InternalError", + // retry: 3 gives the part 4 attempts before the writer rolls back. + requests: ["initiate", "part", "part", "part", "part", ...Array(aborts).fill("abort")], + }); + expect(exitCode).toBe(0); + }); +}); + describe("s3 upload stream body error", () => { // The readStreamIntoSink abrupt path dispatches a single-file PUT before // the pump promise rejects; the PUT's response callback must not read a From e6c2af7caf5f2ef89139f4374dcf22f4cb30f614 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 17 Sep 2026 15:00:09 +0000 Subject: [PATCH 2/3] s3: retry the rollback on a 404 that is not NoSuchUpload Only a 404 with the code NoSuchUpload says the upload id is gone. A proxy in front of the store can answer 404 too, and that answer says nothing about the upload. Such a 404 retries like any other failure, as it did before. --- src/runtime/webcore/s3/multipart.rs | 17 +++++++++++------ test/js/bun/s3/s3.test.ts | 5 ++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/runtime/webcore/s3/multipart.rs b/src/runtime/webcore/s3/multipart.rs index 07efdfb708e4..c17fc7db0017 100644 --- a/src/runtime/webcore/s3/multipart.rs +++ b/src/runtime/webcore/s3/multipart.rs @@ -771,7 +771,17 @@ impl MultiPartUpload { BStr::new(self_.upload_id.get()) ); match result { - S3DeleteResult::Failure(_err) => { + // NoSuchUpload: the upload id is already gone, a retry cannot change that. + S3DeleteResult::Success + | S3DeleteResult::NotFound(S3Error { + code: b"NoSuchUpload", + .. + }) => { + MultiPartUpload::deref_(this); + Ok(()) + } + // Any other 404 can come from a proxy in front of the store. + S3DeleteResult::NotFound(_) | S3DeleteResult::Failure(_) => { let mut options = self_.options.get(); if options.retry > 0 { options.retry -= 1; @@ -783,11 +793,6 @@ impl MultiPartUpload { MultiPartUpload::deref_(this); Ok(()) } - // NotFound: the upload id is already gone, a retry cannot change that. - S3DeleteResult::Success | S3DeleteResult::NotFound(_) => { - MultiPartUpload::deref_(this); - Ok(()) - } } } diff --git a/test/js/bun/s3/s3.test.ts b/test/js/bun/s3/s3.test.ts index 51905c1da0ef..fa6d4e888c61 100644 --- a/test/js/bun/s3/s3.test.ts +++ b/test/js/bun/s3/s3.test.ts @@ -1834,7 +1834,7 @@ describe.concurrent("s3 multipart upload rollback", () => { } if (req.method === "DELETE" && url.searchParams.get("uploadId") === "upload-1") { requests.push("abort"); - return new Response(abort.body, { status: abort.status, headers: { "Content-Type": "application/xml" } }); + return new Response(abort.body, { status: abort.status }); } requests.push(req.method + " " + url.pathname + url.search); return new Response(null, { status: 400 }); @@ -1873,6 +1873,9 @@ describe.concurrent("s3 multipart upload rollback", () => { body: "NoSuchUploadThe specified upload does not exist.", }, ], + // A 404 without that code can come from a proxy in front of the store. It + // says nothing about the upload, so the rollback retries. + ["404 from a proxy", 4, { status: 404, body: "404 page not found" }], // A real failure: the best-effort rollback still retries, 1 + retry times. [ "500 InternalError", From 189e1de8cc274d87778e84a4827781beac7f22fb Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 17 Sep 2026 15:09:21 +0000 Subject: [PATCH 3/3] s3: bind the rollback error so clippy sees the result consumed needless_pass_by_value is denied in the workspace. The callback type fixes the by-value signature, so the retry arm binds the error, as the handler did before. --- src/runtime/webcore/s3/multipart.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/webcore/s3/multipart.rs b/src/runtime/webcore/s3/multipart.rs index c17fc7db0017..c082b25080c3 100644 --- a/src/runtime/webcore/s3/multipart.rs +++ b/src/runtime/webcore/s3/multipart.rs @@ -781,7 +781,7 @@ impl MultiPartUpload { Ok(()) } // Any other 404 can come from a proxy in front of the store. - S3DeleteResult::NotFound(_) | S3DeleteResult::Failure(_) => { + S3DeleteResult::NotFound(_err) | S3DeleteResult::Failure(_err) => { let mut options = self_.options.get(); if options.retry > 0 { options.retry -= 1;