perf: buffer UploadZstd archive in memory instead of temp file#161
Merged
perf: buffer UploadZstd archive in memory instead of temp file#161
Conversation
Replace the temp file with a bytes.Buffer to avoid double disk I/O (write to temp file, then reopen and read) during archive upload. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Bugbot Autofix prepared fixes for 1 of the 1 bugs found in the latest run.
Or push these changes by commenting: Preview (70d1608f61)diff --git a/server/cmd/api/api/fs.go b/server/cmd/api/api/fs.go
--- a/server/cmd/api/api/fs.go
+++ b/server/cmd/api/api/fs.go
@@ -907,6 +907,7 @@
return oapi.UploadZstd400JSONResponse{BadRequestErrorJSONResponse: oapi.BadRequestErrorJSONResponse{Message: "request body required"}}, nil
}
+ const maxArchiveSize = 512 << 20 // 512 MB
var archiveBuf bytes.Buffer
var destPath string
var stripComponents int
@@ -925,10 +926,14 @@
switch part.FormName() {
case "archive":
archiveReceived = true
- if _, err := io.Copy(&archiveBuf, part); err != nil {
+ limited := io.LimitReader(part, maxArchiveSize+1)
+ if _, err := io.Copy(&archiveBuf, limited); err != nil {
log.Error("failed to read archive data", "err", err)
return oapi.UploadZstd400JSONResponse{BadRequestErrorJSONResponse: oapi.BadRequestErrorJSONResponse{Message: "failed to read archive"}}, nil
}
+ if archiveBuf.Len() > maxArchiveSize {
+ return oapi.UploadZstd400JSONResponse{BadRequestErrorJSONResponse: oapi.BadRequestErrorJSONResponse{Message: "archive exceeds maximum size of 512 MB"}}, nil
+ }
case "dest_path":
data, err := io.ReadAll(part)
if err != nil { |
hiroTamada
approved these changes
Feb 24, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Summary
UploadZstdwith abytes.Buffer, eliminating double disk I/O (write to temp file then reopen and read back) during archive upload.UntarZstdviabytes.NewReader. No changes to the API contract or multipart field ordering.Test plan
go buildpassesgo test ./cmd/api/api/)Made with Cursor
Note
Medium Risk
Changes archive upload/extraction plumbing and can increase memory usage significantly for large uploads, potentially impacting server stability under load.
Overview
UploadZstdnow buffers the uploaded tar.zst multipart payload in memory (bytes.Buffer) and streams it directly intozstdutil.UntarZstd, removing the temp-file write/close/reopen/read path.Behavior and API remain the same (same required fields and error mapping for path traversal), but uploads will trade disk I/O for increased memory usage proportional to archive size.
Written by Cursor Bugbot for commit c76d31c. This will update automatically on new commits. Configure here.