Repository navigation
fix(io): Fix stale Blob cache and truncation in Bun.write - #25702
mattermoran wants to merge 8 commits into
Conversation
WalkthroughIntroduces an internal Changes
Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Blob cache and truncation in Bun.writeBlob cache and truncation in Bun.write
Blob cache and truncation in Bun.writeBlob cache and truncation in Bun.write
| pub const PathOrBlob = union(enum) { | ||
| path: jsc.Node.PathOrFileDescriptor, | ||
| blob: Blob, | ||
| blob: *Blob, |
There was a problem hiding this comment.
I think this is saved somewhere so the Blob pointer could become invalid?
There was a problem hiding this comment.
yup reverted that change and decided to only use store size as the source of truth for files
Jarred-Sumner
left a comment
There was a problem hiding this comment.
Instead of tracking is_slice, what if we don't cache the size when the file doesn't exist?
There was a problem hiding this comment.
Actionable comments posted: 2
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (13)
src/OutputFile.zigsrc/bun.js/api/JSBundler.zigsrc/bun.js/api/server/FileRoute.zigsrc/bun.js/api/server/RequestContext.zigsrc/bun.js/bindings/JSGlobalObject.zigsrc/bun.js/webcore/Blob.zigsrc/bun.js/webcore/Body.zigsrc/bun.js/webcore/ByteBlobLoader.zigsrc/bun.js/webcore/ReadableStream.zigsrc/bun.js/webcore/S3File.zigsrc/bun.js/webcore/blob/read_file.zigsrc/bun.js/webcore/fetch.zigsrc/bun.js/webcore/response.classes.ts
🧰 Additional context used
📓 Path-based instructions (2)
src/**/*.zig
📄 CodeRabbit inference engine (src/CLAUDE.md)
src/**/*.zig: Private fields in Zig are fully supported using the#prefix:struct { #foo: u32 };
Use decl literals in Zig for declaration initialization:const decl: Decl = .{ .binding = 0, .value = 0 };
Prefer@importat the bottom of the file (auto formatter will move them automatically)
Files:
src/bun.js/webcore/blob/read_file.zigsrc/bun.js/webcore/S3File.zigsrc/bun.js/api/server/FileRoute.zigsrc/bun.js/webcore/ReadableStream.zigsrc/bun.js/webcore/ByteBlobLoader.zigsrc/bun.js/webcore/Body.zigsrc/bun.js/api/JSBundler.zigsrc/OutputFile.zigsrc/bun.js/webcore/fetch.zigsrc/bun.js/api/server/RequestContext.zigsrc/bun.js/bindings/JSGlobalObject.zigsrc/bun.js/webcore/Blob.zig
**/*.zig
📄 CodeRabbit inference engine (CLAUDE.md)
In Zig code, be careful with allocators and use defer for cleanup
Files:
src/bun.js/webcore/blob/read_file.zigsrc/bun.js/webcore/S3File.zigsrc/bun.js/api/server/FileRoute.zigsrc/bun.js/webcore/ReadableStream.zigsrc/bun.js/webcore/ByteBlobLoader.zigsrc/bun.js/webcore/Body.zigsrc/bun.js/api/JSBundler.zigsrc/OutputFile.zigsrc/bun.js/webcore/fetch.zigsrc/bun.js/api/server/RequestContext.zigsrc/bun.js/bindings/JSGlobalObject.zigsrc/bun.js/webcore/Blob.zig
🧠 Learnings (14)
📚 Learning: 2025-10-17T20:50:58.644Z
Learnt from: taylordotfish
Repo: oven-sh/bun PR: 23755
File: src/bun.js/api/bun/socket/Handlers.zig:154-159
Timestamp: 2025-10-17T20:50:58.644Z
Learning: In Bun socket configuration error messages (src/bun.js/api/bun/socket/Handlers.zig), use the user-facing JavaScript names "data" and "drain" instead of internal field names "onData" and "onWritable", as these are the names users see in the API according to SocketConfig.bindv2.ts.
Applied to files:
src/bun.js/webcore/blob/read_file.zigsrc/bun.js/api/server/FileRoute.zigsrc/bun.js/webcore/response.classes.tssrc/bun.js/webcore/ReadableStream.zigsrc/bun.js/webcore/ByteBlobLoader.zigsrc/bun.js/webcore/fetch.zigsrc/bun.js/api/server/RequestContext.zigsrc/bun.js/bindings/JSGlobalObject.zig
📚 Learning: 2025-10-14T04:04:47.894Z
Learnt from: Jarred-Sumner
Repo: oven-sh/bun PR: 23636
File: src/shell/ParsedShellScript.zig:15-0
Timestamp: 2025-10-14T04:04:47.894Z
Learning: In src/shell/ParsedShellScript.zig, the JavaScript garbage collector's memory reporting API only allows reporting extra memory allocation once at object creation time. Therefore, `estimated_size_for_gc` is intentionally set once during initialization (via `#computeEstimatedSizeForGC()`) and not updated afterward, even though fields like `args`, `export_env`, and `cwd` may be mutated by methods like `setCwd()`, `setEnv()`, and `take()`. The `estimatedSize()` method returns this cached initial value, while `memoryCost()` computes the size on-demand.
Applied to files:
src/bun.js/webcore/blob/read_file.zigsrc/bun.js/webcore/S3File.zigsrc/OutputFile.zigsrc/bun.js/api/server/RequestContext.zigsrc/bun.js/webcore/Blob.zig
📚 Learning: 2025-10-14T04:04:17.132Z
Learnt from: Jarred-Sumner
Repo: oven-sh/bun PR: 23636
File: src/shell/interpreter.zig:672-0
Timestamp: 2025-10-14T04:04:17.132Z
Learning: In Bun's shell interpreter (src/shell/interpreter.zig), the JavaScript garbage collector's memory reporting API only allows reporting extra memory allocation once at object creation time. Therefore, `estimated_size_for_gc` is intentionally set once during initialization and not updated afterward, even though fields like `root_io`, `vm_args_utf8`, and `export_env` may be mutated later. The `estimatedSize()` method returns this cached initial value, while `memoryCost()` computes the size on-demand.
Applied to files:
src/bun.js/webcore/S3File.zigsrc/bun.js/api/JSBundler.zigsrc/bun.js/api/server/RequestContext.zigsrc/bun.js/bindings/JSGlobalObject.zigsrc/bun.js/webcore/Blob.zig
📚 Learning: 2025-10-26T04:50:17.892Z
Learnt from: Jarred-Sumner
Repo: oven-sh/bun PR: 24086
File: src/bun.js/api/server.zig:2534-2535
Timestamp: 2025-10-26T04:50:17.892Z
Learning: In src/bun.js/api/server.zig, u32 is acceptable for route indices and websocket_context_index fields, as the practical limit of 4 billion contexts will never be reached.
Applied to files:
src/bun.js/api/server/FileRoute.zigsrc/bun.js/api/server/RequestContext.zig
📚 Learning: 2025-11-24T18:37:47.899Z
Learnt from: CR
Repo: oven-sh/bun PR: 0
File: src/bun.js/bindings/v8/AGENTS.md:0-0
Timestamp: 2025-11-24T18:37:47.899Z
Learning: Applies to src/bun.js/bindings/v8/**/<UNKNOWN> : <UNKNOWN>
Applied to files:
src/bun.js/webcore/response.classes.ts
📚 Learning: 2025-10-18T20:50:47.750Z
Learnt from: theshadow27
Repo: oven-sh/bun PR: 23798
File: src/bun.js/telemetry.zig:366-373
Timestamp: 2025-10-18T20:50:47.750Z
Learning: In Bun's Zig codebase (src/bun.js/bindings/JSValue.zig), the JSValue enum uses `.null` (not `.js_null`) for JavaScript's null value. Only `js_undefined` has the `js_` prefix to avoid collision with Zig's built-in `undefined` keyword. The correct enum fields are: `js_undefined`, `null`, `true`, `false`, and `zero`.
Applied to files:
src/bun.js/webcore/Body.zig
📚 Learning: 2025-09-19T19:55:22.427Z
Learnt from: markovejnovic
Repo: oven-sh/bun PR: 22568
File: src/valkey/js_valkey_functions.zig:853-854
Timestamp: 2025-09-19T19:55:22.427Z
Learning: In Bun's JSPromise API, `resolvedPromiseValue(globalThis: *JSGlobalObject, value: JSValue) JSValue` returns a JSValue directly (not a JSPromise that needs `.toJS()` called on it). This is different from `resolvedPromise()` which returns a *JSPromise that would need `.toJS()`.
Applied to files:
src/bun.js/webcore/Body.zig
📚 Learning: 2025-12-16T00:21:32.179Z
Learnt from: CR
Repo: oven-sh/bun PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-16T00:21:32.179Z
Learning: Applies to src/bun.js/bindings/**/*.cpp : Cache structures in ZigGlobalObject for JavaScript class bindings
Applied to files:
src/bun.js/api/JSBundler.zigsrc/bun.js/bindings/JSGlobalObject.zig
📚 Learning: 2025-08-31T09:08:12.104Z
Learnt from: Jarred-Sumner
Repo: oven-sh/bun PR: 22279
File: test/js/web/structured-clone-fastpath.test.ts:12-12
Timestamp: 2025-08-31T09:08:12.104Z
Learning: In Bun, process.memoryUsage.rss() is called as a direct function, not as process.memoryUsage().rss like in Node.js. This is the correct API shape for Bun.
Applied to files:
src/bun.js/webcore/fetch.zig
📚 Learning: 2025-10-18T20:59:45.579Z
Learnt from: theshadow27
Repo: oven-sh/bun PR: 23798
File: src/bun.js/telemetry.zig:458-475
Timestamp: 2025-10-18T20:59:45.579Z
Learning: In src/bun.js/telemetry.zig, the RequestId (u64) to JavaScript number (f64) conversion in jsRequestId() is intentionally allowed to lose precision beyond 2^53-1. This is acceptable because: (1) at 1M requests/sec it takes ~285 years to overflow, (2) the counter resets per-process, and (3) these are observability IDs, not critical distributed IDs. Precision loss is an acceptable trade-off for this use case.
Applied to files:
src/bun.js/api/server/RequestContext.zig
📚 Learning: 2025-10-01T21:59:54.571Z
Learnt from: taylordotfish
Repo: oven-sh/bun PR: 23169
File: src/bun.js/bindings/webcore/JSDOMConvertEnumeration.h:47-74
Timestamp: 2025-10-01T21:59:54.571Z
Learning: In the new bindings generator (bindgenv2) for `src/bun.js/bindings/webcore/JSDOMConvertEnumeration.h`, the context-aware enumeration conversion overloads intentionally use stricter validation (requiring `value.isString()` without ToString coercion), diverging from Web IDL semantics. This is a design decision documented in comments.
Applied to files:
src/bun.js/bindings/JSGlobalObject.zig
📚 Learning: 2025-09-02T17:14:46.924Z
Learnt from: taylordotfish
Repo: oven-sh/bun PR: 22227
File: src/safety/alloc.zig:93-95
Timestamp: 2025-09-02T17:14:46.924Z
Learning: In bun's Zig codebase, they use a custom extension of Zig that supports private field syntax with the `#` prefix (e.g., `#allocator`, `#trace`). This is not standard Zig syntax but is valid in their custom implementation. Fields prefixed with `#` are private fields that cannot be accessed from outside the defining struct.
Applied to files:
src/bun.js/webcore/Blob.zig
📚 Learning: 2025-11-19T05:15:22.823Z
Learnt from: pfgithub
Repo: oven-sh/bun PR: 23764
File: src/bun.js/test/timers/FakeTimers.zig:240-0
Timestamp: 2025-11-19T05:15:22.823Z
Learning: In Bun's Zig codebase, `CallFrame.argumentsAsArray(N)` always returns an array of exactly N elements. If fewer than N arguments were passed to the function, the missing slots are filled with `.undefined` JSValue. This means accessing `argumentsAsArray(N)[i]` where i < N is always safe and will never trap—it will simply return `.undefined` if that argument wasn't provided by the caller.
Applied to files:
src/bun.js/webcore/Blob.zig
📚 Learning: 2025-09-05T18:44:43.223Z
Learnt from: markovejnovic
Repo: oven-sh/bun PR: 21728
File: src/bun.js/bindings/bindings.cpp:6429-6432
Timestamp: 2025-09-05T18:44:43.223Z
Learning: The JSC::JSMap::size() method in JavaScriptCore returns a value that can be represented as uint32_t, and the binding functions in src/bun.js/bindings/bindings.cpp correctly use uint32_t as the return type for JSC__JSMap__size. JavaScript Maps are practically limited to 2^32 - 1 elements.
Applied to files:
src/bun.js/webcore/Blob.zig
🔇 Additional comments (15)
src/OutputFile.zig (1)
172-180: Blob size now consistently set via accessor in all OutputFile pathsUsing
blob.setSize(@as(jsc.WebCore.Blob.SizeType, @truncate(...)))inSavedFile.toJS,toJS’s.bufferarm, andtoBlob’s.bufferarm preserves previous semantics while aligning with the new accessor-based size management. The cast/truncate pattern is unchanged and appropriate here.Also applies to: 417-426, 498-515
src/bun.js/bindings/JSGlobalObject.zig (1)
912-917: Use ofblob.getSize()for WASM streaming is consistent with new Blob APIPassing
blob.getSize()toReadableStream.fromBlobCopyRefafterblob.resolveSize()keeps the previous intent (size-aware streaming) while routing through the new size accessor. No control-flow or correctness issues introduced.src/bun.js/api/JSBundler.zig (1)
1515-1517: BuildArtifact size now correctly uses JS-facing Blob accessorSwitching
getSizeto delegate toBlob.getSizeForJSis appropriate for this JS-visible API and keeps size representation logic centralized in Blob.src/bun.js/webcore/response.classes.ts (1)
183-190: Blob.prototype.size now wired to JS-specific size accessorMapping the
sizegetter to"getSizeForJS"matches the new Blob accessor design and ensures JS observes the intended size semantics.src/bun.js/webcore/Body.zig (1)
264-272: Body value now consults Blob size via accessor in emptiness and streaming paths
isDefinitelyEmpty’s Blob case now usesthis.Blob.getSize() == 0, which maintains previous behavior while honoring slice-aware size.toReadableStreampassesblob.getSize()as the recommended chunk size, aligning this path with the new Blob size API.Also applies to: 458-462
src/bun.js/webcore/S3File.zig (1)
186-200: S3size()now delegates blob case to JS-facing Blob sizeFor blob-backed S3 files, returning
Blob.getSizeForJS(blob, globalThis)is consistent with the new accessor pattern and keeps S3 logic focused on path vs blob selection rather than size formatting.src/bun.js/api/server/FileRoute.zig (1)
191-215: FileRoute static file sizing now consistently uses Blob size accessor
_sizeis derived via@min(stat_size, this.blob.getSize()), preserving the previous “cap by declared blob size” behavior while respecting the new Blob size model (including slices).- The transfer
max_sizeparameter now also keys offthis.blob.getSize(), so sendfile-style transfers and Content-Length remain aligned.Also applies to: 271-275
src/bun.js/webcore/ReadableStream.zig (1)
317-323: ReadableStream.fromBlobCopyRef now respects Blob size via accessor for file/S3Both the file-backed and S3-backed branches now use
blob.getSize()with theBlob.max_sizesentinel check, maintaining prior slice vs whole-file semantics while adopting the new Blob API.Also applies to: 333-339
src/bun.js/api/server/RequestContext.zig (1)
972-977: LGTM: Accessor migration is correct.The changes consistently replace direct
sizefield access withgetSize()andsetSize()accessor methods across multiple code paths:
- Sendfile rendering (lines 972-977)
- File read handling (lines 1064-1069)
- HEAD response content-length calculation (lines 1488-1491)
The logic is preserved correctly in all cases, properly handling conditional size updates based on file type and stat information.
Also applies to: 1064-1069, 1488-1491
src/bun.js/webcore/fetch.zig (1)
1143-1143: LGTM: Correct accessor usage for blob size.The changes properly use
getSize()accessor in both code paths:
- Line 1143: Capturing original size for sendfile optimization
- Line 1176: Passing blob size as
max_sizeparameter toNodeFS.readFileBoth usages are read-only and correctly aligned with the accessor migration pattern.
Also applies to: 1176-1176
src/bun.js/webcore/ByteBlobLoader.zig (1)
56-56: LGTM: Accessor methods used correctly.The changes properly migrate from direct field access to accessor methods:
- Lines 56 & 59:
getSize()correctly used insetup()to initializechunk_sizeandremainbased on blob size- Line 114:
setSize()correctly used intoAnyBlob()to set the reconstructed blob's size to remaining bytesThe logic is preserved and the accessor usage is consistent with the broader refactoring.
Also applies to: 59-59, 114-114
src/bun.js/webcore/Blob.zig (4)
28-28: Good encapsulation with accessor methods.The introduction of private
_sizestorage and theis_sliceflag properly supports the PR's goal of distinguishing whole-file blobs (whose size should track the underlying file and can change on writes) from explicit slices (whose size is fixed at slice-creation time). This allows writes to a whole-file destination to use the full source size rather than being incorrectly truncated to a stale destination size.Also applies to: 34-34
3102-3140: LGTM! Well-designed accessor methods.The three accessor methods correctly implement the dual-storage model for blob size:
getSize()/setSize()usestore.data.file.max_sizefor file-backed non-slice blobs, allowing size to track file changes- They use
_sizefor slices and byte blobs, where size is immutablegetSizeForJS()properly handles edge cases (S3, non-seekable files) for JavaScript consumersThe
is_sliceflag discriminator ensures writes to whole-file destinations use the full source size (fixing the truncation bug), while slice destinations correctly bound the write to the slice size.
1057-1057: Correct write size logic fixes the truncation bug.The use of
destination_blob.is_sliceto determine the copy size correctly implements the PR's objective:
- Whole-file destination (is_slice=false): writes full source content, preventing truncation when source is larger than the previous destination file size
- Slice destination (is_slice=true): bounds write to
min(source, dest), preserving explicit slice boundariesThis addresses the issue where writing a larger source into a smaller destination file incorrectly used the stale destination size as the limit, truncating the source content.
Also applies to: 1064-1065
1227-1229: Metadata invalidation correctly forces size reload after write.Setting
max_size = Blob.max_size(sentinel value) ensures the next access to the blob's size will triggerresolveSize()→resolveFileStat()to reload the actual file size. This fixes the stale metadata issue described in the PR objectives.The lazy-reload pattern (invalidate now, reload on next access) avoids an immediate
stat()syscall, improving write performance.Note: The TODO comment correctly observes that resetting metadata at the beginning (before the write completes) is suboptimal. Consider moving the reset to success paths in each write operation for better performance.
We still need
Without Also simply not caching the size when the file doesn’t exist wouldn’t solve the problem. Using |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/bun.js/webcore/Blob.zig (1)
3158-3166: Setis_slice = truewhen deserializing file blobs with non-zero offsets, or document the invariant.At line 517 in
_onStructuredCloneDeserialize(), the deserialized blob's offset is set for all store types (bytes, file, empty) without ensuringis_slice = truefor file stores. SincegetSize()returnsmax_sizedirectly for file blobs withis_slice = false(ignoring_size), the assignment of_sizeat line 3163 inresolveSize()has no effect for deserialized file blobs with offsets. This creates an inconsistency:getSliceFrom()correctly sets bothoffsetandis_slice = truetogether, but deserialization does not. Either setis_slice = truewhen offset != 0 is deserialized for file stores, or revise the size computation to account for offset regardless of theis_sliceflag.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
src/bun.js/webcore/Blob.zigsrc/bun.js/webcore/blob/read_file.zig
🧰 Additional context used
📓 Path-based instructions (2)
src/**/*.zig
📄 CodeRabbit inference engine (src/CLAUDE.md)
src/**/*.zig: Private fields in Zig are fully supported using the#prefix:struct { #foo: u32 };
Use decl literals in Zig for declaration initialization:const decl: Decl = .{ .binding = 0, .value = 0 };
Prefer@importat the bottom of the file (auto formatter will move them automatically)
Files:
src/bun.js/webcore/Blob.zigsrc/bun.js/webcore/blob/read_file.zig
**/*.zig
📄 CodeRabbit inference engine (CLAUDE.md)
In Zig code, be careful with allocators and use defer for cleanup
Files:
src/bun.js/webcore/Blob.zigsrc/bun.js/webcore/blob/read_file.zig
🧠 Learnings (7)
📚 Learning: 2025-10-14T04:04:17.132Z
Learnt from: Jarred-Sumner
Repo: oven-sh/bun PR: 23636
File: src/shell/interpreter.zig:672-0
Timestamp: 2025-10-14T04:04:17.132Z
Learning: In Bun's shell interpreter (src/shell/interpreter.zig), the JavaScript garbage collector's memory reporting API only allows reporting extra memory allocation once at object creation time. Therefore, `estimated_size_for_gc` is intentionally set once during initialization and not updated afterward, even though fields like `root_io`, `vm_args_utf8`, and `export_env` may be mutated later. The `estimatedSize()` method returns this cached initial value, while `memoryCost()` computes the size on-demand.
Applied to files:
src/bun.js/webcore/Blob.zigsrc/bun.js/webcore/blob/read_file.zig
📚 Learning: 2025-10-14T04:04:47.894Z
Learnt from: Jarred-Sumner
Repo: oven-sh/bun PR: 23636
File: src/shell/ParsedShellScript.zig:15-0
Timestamp: 2025-10-14T04:04:47.894Z
Learning: In src/shell/ParsedShellScript.zig, the JavaScript garbage collector's memory reporting API only allows reporting extra memory allocation once at object creation time. Therefore, `estimated_size_for_gc` is intentionally set once during initialization (via `#computeEstimatedSizeForGC()`) and not updated afterward, even though fields like `args`, `export_env`, and `cwd` may be mutated by methods like `setCwd()`, `setEnv()`, and `take()`. The `estimatedSize()` method returns this cached initial value, while `memoryCost()` computes the size on-demand.
Applied to files:
src/bun.js/webcore/Blob.zigsrc/bun.js/webcore/blob/read_file.zig
📚 Learning: 2025-10-18T20:50:47.750Z
Learnt from: theshadow27
Repo: oven-sh/bun PR: 23798
File: src/bun.js/telemetry.zig:366-373
Timestamp: 2025-10-18T20:50:47.750Z
Learning: In Bun's Zig codebase (src/bun.js/bindings/JSValue.zig), the JSValue enum uses `.null` (not `.js_null`) for JavaScript's null value. Only `js_undefined` has the `js_` prefix to avoid collision with Zig's built-in `undefined` keyword. The correct enum fields are: `js_undefined`, `null`, `true`, `false`, and `zero`.
Applied to files:
src/bun.js/webcore/Blob.zig
📚 Learning: 2025-11-19T05:15:22.823Z
Learnt from: pfgithub
Repo: oven-sh/bun PR: 23764
File: src/bun.js/test/timers/FakeTimers.zig:240-0
Timestamp: 2025-11-19T05:15:22.823Z
Learning: In Bun's Zig codebase, `CallFrame.argumentsAsArray(N)` always returns an array of exactly N elements. If fewer than N arguments were passed to the function, the missing slots are filled with `.undefined` JSValue. This means accessing `argumentsAsArray(N)[i]` where i < N is always safe and will never trap—it will simply return `.undefined` if that argument wasn't provided by the caller.
Applied to files:
src/bun.js/webcore/Blob.zig
📚 Learning: 2025-09-05T18:44:43.223Z
Learnt from: markovejnovic
Repo: oven-sh/bun PR: 21728
File: src/bun.js/bindings/bindings.cpp:6429-6432
Timestamp: 2025-09-05T18:44:43.223Z
Learning: The JSC::JSMap::size() method in JavaScriptCore returns a value that can be represented as uint32_t, and the binding functions in src/bun.js/bindings/bindings.cpp correctly use uint32_t as the return type for JSC__JSMap__size. JavaScript Maps are practically limited to 2^32 - 1 elements.
Applied to files:
src/bun.js/webcore/Blob.zig
📚 Learning: 2025-10-17T20:50:58.644Z
Learnt from: taylordotfish
Repo: oven-sh/bun PR: 23755
File: src/bun.js/api/bun/socket/Handlers.zig:154-159
Timestamp: 2025-10-17T20:50:58.644Z
Learning: In Bun socket configuration error messages (src/bun.js/api/bun/socket/Handlers.zig), use the user-facing JavaScript names "data" and "drain" instead of internal field names "onData" and "onWritable", as these are the names users see in the API according to SocketConfig.bindv2.ts.
Applied to files:
src/bun.js/webcore/blob/read_file.zig
📚 Learning: 2025-10-24T10:43:09.398Z
Learnt from: fmguerreiro
Repo: oven-sh/bun PR: 23774
File: src/install/PackageManager/updatePackageJSONAndInstall.zig:548-548
Timestamp: 2025-10-24T10:43:09.398Z
Learning: In Bun's Zig codebase, the `as(usize, intCast(...))` cast pattern triggers a Zig compiler bug that causes compilation to hang indefinitely when used in complex control flow contexts (loops + short-circuit operators + optional unwrapping). Avoid this pattern and use simpler alternatives like just `intCast(...)` if type casting is necessary.
Applied to files:
src/bun.js/webcore/blob/read_file.zig
🔇 Additional comments (5)
src/bun.js/webcore/blob/read_file.zig (1)
19-21: LGTM! Efficient accessor usage.The cached
current_sizeapproach correctly avoids redundantgetSize()calls while using the public accessors (getSize()andsetSize()) as intended by the refactor. The logic appropriately truncates the blob size to not exceed the bytes actually read.src/bun.js/webcore/Blob.zig (4)
3102-3140: LGTM! Clean accessor design with correct dual-path size management.The accessor trio (
getSize(),setSize(),getSizeForJS()) correctly implements the split storage strategy:
- File-backed non-slice blobs: size stored in
store.data.file.max_size- All others (bytes, slices): size stored in
_sizeThis design enables the synchronous destination blob invalidation that fixes the reported stale-cache issues.
1227-1229: LGTM! Proper metadata invalidation after write initiation.Resetting
last_modifiedtoinit_timestampandmax_sizetoBlob.max_sizecorrectly invalidates the destination file's cached metadata. This ensures subsequent reads (like.text()or.size) will reload fresh values from the filesystem, addressing the stale cache issues reported in #23902.
2784-2785: LGTM! Correct slice construction with direct field initialization.Directly setting
_sizeandis_slicehere is appropriate becausegetSliceFrom()is constructing a new blob state. The accessor methods (getSize()/setSize()) will subsequently route to_sizefor this slice, ensuring consistent behavior.
28-34: Well-executed size management refactor.The introduction of
_size,is_slice, and the accessor methods (getSize(),setSize(),getSizeForJS()) successfully addresses the PR objectives:
- Fixes stale metadata (#23902):
setSize()enables synchronous invalidation of destination blob size after writes- Prevents truncation (#22456): Line 1057 correctly uses
is_sliceto distinguish explicit slice boundaries from whole-file writes- Clean accessor pattern: Direct field access is properly limited to initialization and internal resolution logic
The split storage strategy (max_size for file non-slices, _size otherwise) is consistently implemented throughout the file.
Also applies to: 3102-3140
|
If needed I can also pull out the |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/bun.js/webcore/Blob.zig (1)
69-69: 🔴 Critical: Broken serialization format foris_slicefield.This PR adds a new
is_slicefield to the serialization format but has multiple critical bugs:
Wrong deserialization position: Line 434 reads
is_sliceimmediately afterstore_tag, but serialization writes it at line 332 afterstore.serialize()(line 326). This means deserialization reads bytes that are actually store data, causing data corruption.Missing version bump: The serialization version (line 69) remains
3, but a new field has been added. This breaks backward compatibility—deserializing old blobs will fail when trying to readis_slicethat doesn't exist in the serialized data.Correct serialization order should be:
- Write:
store_tag→store.serialize()→is_jsdom_file→last_modified→is_slice- Read:
store_tag→ [create blob, reading store data] →is_jsdom_file→last_modified→is_sliceRequired fixes:
- Bump
serialization_versionto4(line 69)- Move the
is_sliceread to after theversions:block (after line 518), not before blob creation- Gate
is_sliceserialization/deserialization onversion >= 4- Default
is_slice = falsefor older versions🔎 Suggested fix
-const serialization_version: u8 = 3; +const serialization_version: u8 = 4;try writer.writeInt(u8, @intFromBool(this.is_jsdom_file), .little); try writeFloat(f64, this.last_modified, Writer, writer); -try writer.writeInt(u8, @intFromBool(this.is_slice), .little); - +// Version 4: serialize is_slice +if (serialization_version >= 4) { + try writer.writeInt(u8, @intFromBool(this.is_slice), .little); +} + // Serialize File name if this is a File objectconst store_tag = try reader.readEnum(Store.SerializeTag, .little); -const is_slice = try reader.readInt(u8, .little) != 0; - const blob: *Blob = switch (store_tag) {if (version == 3) break :versions; + + // Version 4: Read is_slice + if (version >= 4) { + blob.is_slice = try reader.readInt(u8, .little) != 0; + } } bun.assertf(blob.isHeapAllocated(), "expected blob to be heap-allocated", .{}); blob.offset = @as(u52, @intCast(offset)); - blob.is_slice = is_slice;Also applies to: 332-333, 434-435, 522-522
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📥 Commits
Reviewing files that changed from the base of the PR and between 1d28daa and 22d1aeb6a7df0d32897a1ce6fb36cbcc9a4a9723.
📒 Files selected for processing (1)
src/bun.js/webcore/Blob.zig
🧰 Additional context used
📓 Path-based instructions (2)
src/**/*.zig
📄 CodeRabbit inference engine (src/CLAUDE.md)
src/**/*.zig: Private fields in Zig are fully supported using the#prefix:struct { #foo: u32 };
Use decl literals in Zig for declaration initialization:const decl: Decl = .{ .binding = 0, .value = 0 };
Prefer@importat the bottom of the file (auto formatter will move them automatically)
Files:
src/bun.js/webcore/Blob.zig
**/*.zig
📄 CodeRabbit inference engine (CLAUDE.md)
In Zig code, be careful with allocators and use defer for cleanup
Files:
src/bun.js/webcore/Blob.zig
🧠 Learnings (5)
📚 Learning: 2025-10-14T04:04:17.132Z
Learnt from: Jarred-Sumner
Repo: oven-sh/bun PR: 23636
File: src/shell/interpreter.zig:672-0
Timestamp: 2025-10-14T04:04:17.132Z
Learning: In Bun's shell interpreter (src/shell/interpreter.zig), the JavaScript garbage collector's memory reporting API only allows reporting extra memory allocation once at object creation time. Therefore, `estimated_size_for_gc` is intentionally set once during initialization and not updated afterward, even though fields like `root_io`, `vm_args_utf8`, and `export_env` may be mutated later. The `estimatedSize()` method returns this cached initial value, while `memoryCost()` computes the size on-demand.
Applied to files:
src/bun.js/webcore/Blob.zig
📚 Learning: 2025-10-14T04:04:47.894Z
Learnt from: Jarred-Sumner
Repo: oven-sh/bun PR: 23636
File: src/shell/ParsedShellScript.zig:15-0
Timestamp: 2025-10-14T04:04:47.894Z
Learning: In src/shell/ParsedShellScript.zig, the JavaScript garbage collector's memory reporting API only allows reporting extra memory allocation once at object creation time. Therefore, `estimated_size_for_gc` is intentionally set once during initialization (via `#computeEstimatedSizeForGC()`) and not updated afterward, even though fields like `args`, `export_env`, and `cwd` may be mutated by methods like `setCwd()`, `setEnv()`, and `take()`. The `estimatedSize()` method returns this cached initial value, while `memoryCost()` computes the size on-demand.
Applied to files:
src/bun.js/webcore/Blob.zig
📚 Learning: 2025-10-18T20:50:47.750Z
Learnt from: theshadow27
Repo: oven-sh/bun PR: 23798
File: src/bun.js/telemetry.zig:366-373
Timestamp: 2025-10-18T20:50:47.750Z
Learning: In Bun's Zig codebase (src/bun.js/bindings/JSValue.zig), the JSValue enum uses `.null` (not `.js_null`) for JavaScript's null value. Only `js_undefined` has the `js_` prefix to avoid collision with Zig's built-in `undefined` keyword. The correct enum fields are: `js_undefined`, `null`, `true`, `false`, and `zero`.
Applied to files:
src/bun.js/webcore/Blob.zig
📚 Learning: 2025-11-19T05:15:22.823Z
Learnt from: pfgithub
Repo: oven-sh/bun PR: 23764
File: src/bun.js/test/timers/FakeTimers.zig:240-0
Timestamp: 2025-11-19T05:15:22.823Z
Learning: In Bun's Zig codebase, `CallFrame.argumentsAsArray(N)` always returns an array of exactly N elements. If fewer than N arguments were passed to the function, the missing slots are filled with `.undefined` JSValue. This means accessing `argumentsAsArray(N)[i]` where i < N is always safe and will never trap—it will simply return `.undefined` if that argument wasn't provided by the caller.
Applied to files:
src/bun.js/webcore/Blob.zig
📚 Learning: 2025-09-05T18:44:43.223Z
Learnt from: markovejnovic
Repo: oven-sh/bun PR: 21728
File: src/bun.js/bindings/bindings.cpp:6429-6432
Timestamp: 2025-09-05T18:44:43.223Z
Learning: The JSC::JSMap::size() method in JavaScriptCore returns a value that can be represented as uint32_t, and the binding functions in src/bun.js/bindings/bindings.cpp correctly use uint32_t as the return type for JSC__JSMap__size. JavaScript Maps are practically limited to 2^32 - 1 elements.
Applied to files:
src/bun.js/webcore/Blob.zig
🔇 Additional comments (3)
src/bun.js/webcore/Blob.zig (3)
3107-3145: Accessor pattern implementation looks good.The
getSize(),setSize(), andgetSizeForJS()methods correctly centralize size handling:
- Full-file blobs use
store.data.file.max_size- Slices use
_size- The
is_sliceflag correctly prevents file stores from overwriting slice sizesThe usage throughout the file is consistent with direct
_sizeaccess only in initialization and slice creation paths, which is appropriate.
1232-1234: Cache invalidation logic correctly implemented.Resetting
last_modifiedandmax_sizewhen writing to file blobs ensures that subsequent reads (getLastModified(),getSize()) reload fresh metadata from the filesystem. This directly addresses issue #23902 where.text()was returning stale content after writes.
1062-1062: Slice boundary logic correctly prevents truncation and overruns.The conditional size calculation ensures:
- When
destination_blob.is_sliceis true, the copy respects the explicit slice boundary by using@min(source_size, destination_size)- When false (full file overwrite), the full source content is written, fixing the truncation issue in #22456
The source offset (line 1069) is also correctly propagated, ensuring sliced sources copy from the right starting position.
Also applies to: 1069-1070
fix serialization
22d1aeb to
9fc5374
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/bun.js/webcore/Blob.zig (1)
3162-3192:resolveSize()implementation is correct but could be more explicit about slice handling.The direct
_sizeassignments inresolveSize()are correct because this method is only called whengetSize() == Blob.max_size(unknown size), which doesn't occur for slices:Call sites verified (all check
getSize() == max_sizebefore calling):
- Lines 241-242: FormData handling
- Lines 2177-2179:
getExistsSync()- Lines 3051-3053:
getSizeForBindings()- Lines 3142-3147:
getSizeForJS()Why this works:
- Slices:
_sizeis set explicitly ingetSliceFrom()(line 2804), sogetSize()returns a known value, preventingresolveSize()from being called- Non-slice file blobs:
_sizeassignments are benign;getSize()returnsmax_size(updated byresolveFileStat())- Bytes blobs:
_sizecorrectly holds the computed size♻️ Optional: Make slice handling explicit
For clarity,
resolveSize()could early-return for slices to document that slice sizes are immutable:pub fn resolveSize(this: *Blob) void { // Slices have immutable sizes set in getSliceFrom() if (this.is_slice) return; if (this.store) |store| { // existing logic...This is purely a readability improvement; the current logic is correct.
🤖 Fix all issues with AI agents
In @src/bun.js/webcore/Blob.zig:
- Around line 1241-1243: The current code resets
blob_store.data.file.last_modified and blob_store.data.file.max_size to sentinel
values (jsc.init_timestamp and Blob.max_size) before performing writes, which
forces metadata reloads even if the write fails; move these two assignments out
of the pre-write path and into the post-write success path inside the Blob write
routine in Blob.zig so the metadata is only invalidated after a successful
write, keeping the same sentinel values and using the same symbols
(blob_store.data.file.last_modified, blob_store.data.file.max_size,
jsc.init_timestamp, Blob.max_size) to locate and apply the change.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (6)
src/bun.js/api/JSBundler.zigsrc/bun.js/api/server/RequestContext.zigsrc/bun.js/webcore/Blob.zigsrc/bun.js/webcore/ReadableStream.zigsrc/bun.js/webcore/S3File.zigsrc/bun.js/webcore/fetch.zig
🧰 Additional context used
📓 Path-based instructions (2)
src/**/*.zig
📄 CodeRabbit inference engine (src/CLAUDE.md)
src/**/*.zig: Private fields in Zig are fully supported using the#prefix:struct { #foo: u32 };
Use decl literals in Zig for declaration initialization:const decl: Decl = .{ .binding = 0, .value = 0 };
Prefer@importat the bottom of the file (auto formatter will move them automatically)
Files:
src/bun.js/api/server/RequestContext.zigsrc/bun.js/webcore/S3File.zigsrc/bun.js/webcore/ReadableStream.zigsrc/bun.js/api/JSBundler.zigsrc/bun.js/webcore/fetch.zigsrc/bun.js/webcore/Blob.zig
**/*.zig
📄 CodeRabbit inference engine (CLAUDE.md)
In Zig code, be careful with allocators and use defer for cleanup
Files:
src/bun.js/api/server/RequestContext.zigsrc/bun.js/webcore/S3File.zigsrc/bun.js/webcore/ReadableStream.zigsrc/bun.js/api/JSBundler.zigsrc/bun.js/webcore/fetch.zigsrc/bun.js/webcore/Blob.zig
🧠 Learnings (9)
📚 Learning: 2025-10-26T04:50:17.892Z
Learnt from: Jarred-Sumner
Repo: oven-sh/bun PR: 24086
File: src/bun.js/api/server.zig:2534-2535
Timestamp: 2025-10-26T04:50:17.892Z
Learning: In src/bun.js/api/server.zig, u32 is acceptable for route indices and websocket_context_index fields, as the practical limit of 4 billion contexts will never be reached.
Applied to files:
src/bun.js/api/server/RequestContext.zig
📚 Learning: 2026-01-05T16:32:07.551Z
Learnt from: alii
Repo: oven-sh/bun PR: 25474
File: src/bun.js/event_loop/Sigusr1Handler.zig:0-0
Timestamp: 2026-01-05T16:32:07.551Z
Learning: In Zig codebases (e.g., Bun), treat std.posix.sigaction as returning void and do not perform runtime error handling for its failure. The Zig standard library views sigaction failures as programmer errors (unreachable) because they only occur with invalid signals like SIGKILL/SIGSTOP. Apply this pattern across Zig files that call sigaction (e.g., crash_handler.zig, main.zig, filter_run.zig, process.zig) and ensure failures are not handled as recoverable errors; prefer reaching an explicit unreachable/compile-time assumption when such failures are detected.
Applied to files:
src/bun.js/api/server/RequestContext.zigsrc/bun.js/webcore/S3File.zigsrc/bun.js/webcore/ReadableStream.zigsrc/bun.js/api/JSBundler.zigsrc/bun.js/webcore/fetch.zigsrc/bun.js/webcore/Blob.zig
📚 Learning: 2025-10-14T04:04:47.894Z
Learnt from: Jarred-Sumner
Repo: oven-sh/bun PR: 23636
File: src/shell/ParsedShellScript.zig:15-0
Timestamp: 2025-10-14T04:04:47.894Z
Learning: In src/shell/ParsedShellScript.zig, the JavaScript garbage collector's memory reporting API only allows reporting extra memory allocation once at object creation time. Therefore, `estimated_size_for_gc` is intentionally set once during initialization (via `#computeEstimatedSizeForGC()`) and not updated afterward, even though fields like `args`, `export_env`, and `cwd` may be mutated by methods like `setCwd()`, `setEnv()`, and `take()`. The `estimatedSize()` method returns this cached initial value, while `memoryCost()` computes the size on-demand.
Applied to files:
src/bun.js/webcore/S3File.zig
📚 Learning: 2025-10-14T04:04:17.132Z
Learnt from: Jarred-Sumner
Repo: oven-sh/bun PR: 23636
File: src/shell/interpreter.zig:672-0
Timestamp: 2025-10-14T04:04:17.132Z
Learning: In Bun's shell interpreter (src/shell/interpreter.zig), the JavaScript garbage collector's memory reporting API only allows reporting extra memory allocation once at object creation time. Therefore, `estimated_size_for_gc` is intentionally set once during initialization and not updated afterward, even though fields like `root_io`, `vm_args_utf8`, and `export_env` may be mutated later. The `estimatedSize()` method returns this cached initial value, while `memoryCost()` computes the size on-demand.
Applied to files:
src/bun.js/webcore/S3File.zigsrc/bun.js/api/JSBundler.zigsrc/bun.js/webcore/Blob.zig
📚 Learning: 2025-10-18T20:50:47.750Z
Learnt from: theshadow27
Repo: oven-sh/bun PR: 23798
File: src/bun.js/telemetry.zig:366-373
Timestamp: 2025-10-18T20:50:47.750Z
Learning: In Bun's Zig codebase (src/bun.js/bindings/JSValue.zig), the JSValue enum uses `.null` (not `.js_null`) for JavaScript's null value. Only `js_undefined` has the `js_` prefix to avoid collision with Zig's built-in `undefined` keyword. The correct enum fields are: `js_undefined`, `null`, `true`, `false`, and `zero`.
Applied to files:
src/bun.js/webcore/S3File.zig
📚 Learning: 2025-10-17T20:50:58.644Z
Learnt from: taylordotfish
Repo: oven-sh/bun PR: 23755
File: src/bun.js/api/bun/socket/Handlers.zig:154-159
Timestamp: 2025-10-17T20:50:58.644Z
Learning: In Bun socket configuration error messages (src/bun.js/api/bun/socket/Handlers.zig), use the user-facing JavaScript names "data" and "drain" instead of internal field names "onData" and "onWritable", as these are the names users see in the API according to SocketConfig.bindv2.ts.
Applied to files:
src/bun.js/webcore/ReadableStream.zigsrc/bun.js/webcore/fetch.zig
📚 Learning: 2025-12-16T00:21:32.179Z
Learnt from: CR
Repo: oven-sh/bun PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-16T00:21:32.179Z
Learning: Applies to src/bun.js/bindings/**/*.cpp : Cache structures in ZigGlobalObject for JavaScript class bindings
Applied to files:
src/bun.js/api/JSBundler.zig
📚 Learning: 2025-11-19T05:15:22.823Z
Learnt from: pfgithub
Repo: oven-sh/bun PR: 23764
File: src/bun.js/test/timers/FakeTimers.zig:240-0
Timestamp: 2025-11-19T05:15:22.823Z
Learning: In Bun's Zig codebase, `CallFrame.argumentsAsArray(N)` always returns an array of exactly N elements. If fewer than N arguments were passed to the function, the missing slots are filled with `.undefined` JSValue. This means accessing `argumentsAsArray(N)[i]` where i < N is always safe and will never trap—it will simply return `.undefined` if that argument wasn't provided by the caller.
Applied to files:
src/bun.js/webcore/Blob.zig
📚 Learning: 2025-09-05T18:44:43.223Z
Learnt from: markovejnovic
Repo: oven-sh/bun PR: 21728
File: src/bun.js/bindings/bindings.cpp:6429-6432
Timestamp: 2025-09-05T18:44:43.223Z
Learning: The JSC::JSMap::size() method in JavaScriptCore returns a value that can be represented as uint32_t, and the binding functions in src/bun.js/bindings/bindings.cpp correctly use uint32_t as the return type for JSC__JSMap__size. JavaScript Maps are practically limited to 2^32 - 1 elements.
Applied to files:
src/bun.js/webcore/Blob.zig
🔇 Additional comments (10)
src/bun.js/api/JSBundler.zig (1)
1520-1522: RouteBuildArtifact.getSizethrough JS-facing Blob accessorUsing
Blob.getSizeForJShere is correct: JS-visiblesizenow reflects the unified Blob size model (including slices and cache invalidation) without changing control flow.src/bun.js/webcore/S3File.zig (1)
186-201: Align S3size()with JS-visible Blob sizeSwitching the blob branch of
size()toBlob.getSizeForJScorrectly exposes the JS-facing Blob size (respecting slices and cache invalidation) and matches the function’s JS API role. No behavioral regressions apparent.src/bun.js/webcore/fetch.zig (1)
1138-1179: UseBlob.getSize()for sendfile and fallbackreadFilelimitsReplacing direct field access with
body.AnyBlob.Blob.getSize()fororiginal_sizeand.max_sizekeeps both the sendfile fast path and the synchronousNodeFS.readFilefallback in sync with the centralized Blob sizing logic (including slices and size invalidation after writes). The surrounding clamping on regular files and error handling remain unchanged.src/bun.js/webcore/ReadableStream.zig (1)
315-340: Useblob.getSize()when creating file/S3-backed ReadableStreamsThe
.fileand.s3branches now derive theirmax_size/length fromblob.getSize(), which correctly respects slice boundaries and the unified Blob size/cache behavior. MappingBlob.max_sizetonullpreserves the previous “unbounded” semantics.src/bun.js/api/server/RequestContext.zig (3)
909-1000: Sendfile path: switch togetSize()/setSize()for Blob sizing
renderSendFilenow derivesoriginal_sizefromthis.blob.Blob.getSize()and updates viasetSize(...), keeping the Blob’s cached size in sync withstat_sizeand the shared size model. Regular files still usestat_size, while non-regular files are bounded bymin(original_size, stat_size)as before, so semantics are preserved while integrating with the new accessors.
1063-1070: Temporary readFile path: update Blob size via accessorsIn
onReadFile, the temporary-buffer case now usesthis.blob.Blob.getSize()andsetSize(...)instead of touching the size field directly. The monotonic sizing rule (max(original_size, stat_size)unless size was unknown/0) is retained, but now goes through the centralized Blob size logic and cache invalidation.
1488-1492: HEAD responses: base Content-Length onblob.getSize()After
blob.resolveSize(), usingblob.getSize()for the HEADcontent-lengthheader correctly reflects the canonical Blob size (including slices), with the existingBlob.max_size → 0fallback preserved. This aligns HEAD behavior with other size-aware paths that now rely on the accessor.src/bun.js/webcore/Blob.zig (3)
3122-3160: LGTM: Size accessor methods correctly centralize size handling.The accessor trio (
getSize(),setSize(),getSizeForJS()) cleanly separates concerns:
getSize(): Returnsstore.data.file.max_sizefor non-slice file blobs,_sizeotherwise. This centralizes the logic for determining which backing field holds the authoritative size.setSize(): MirrorsgetSize()for writes, ensuring size updates go to the correct backing field.getSizeForJS(): Handles unknown sizes (max_size) by callingresolveSize()and returns appropriate values (NaN for S3, infinity for non-seekable) for JS consumers.The implementation correctly addresses the stale cache issue by providing a single point of invalidation (via
setSize()or directmax_sizeassignment) and ensures slices maintain their user-defined boundaries independently of the underlying store's size.
1068-1069: Excellent: Copy size logic correctly fixes truncation bug.The conditional copy size calculation addresses the core issue:
Before (buggy behavior): Used destination size as copy limit → larger sources truncated to smaller destinations.
After (fixed behavior):
- Destination is a slice: Respects user-defined boundary with
@min(source_blob.getSize(), destination_blob.getSize())- Destination is whole-file: Uses full
source_blob.getSize()— no truncationThis correctly implements the fix described in PR objective #22456 (prevent short/truncated writes when copying larger source into smaller destination) while preserving slice boundary semantics when the user explicitly created a slice destination.
Also applies to: 1075-1076
346-348: Serialization ofis_sliceflag is correctly versioned.The version 4 serialization change properly adds
is_sliceto the structured clone format:Serialization (346-348):
if (serialization_version >= 4) { try writer.writeInt(u8, @intFromBool(this.is_slice), .little); }Deserialization (521-523):
if (version >= 4) { blob.is_slice = try reader.readInt(u8, .little) != 0; }Correctness:
- Version guard ensures forward/backward compatibility
- Consistent boolean-to-byte encoding
- Version 3 and earlier blobs default to
is_slice = false(line 34)Also applies to: 521-523
| // reset last_modified and max_size to force getLastModified() and getActualSize() to reload after writing. | ||
| blob_store.data.file.last_modified = jsc.init_timestamp; | ||
| blob_store.data.file.max_size = Blob.max_size; |
There was a problem hiding this comment.
🧹 Nitpick | 🔵 Trivial
Metadata invalidation correctly fixes stale cache issue.
Resetting last_modified and max_size to sentinel values before writes ensures subsequent reads fetch fresh metadata. This addresses PR objectives #23902 (.text() returning stale content after writes) and #4930 (metadata reads causing empty writes).
Note: The TODO comment indicates this happens before writes rather than after success, which means metadata is invalidated even if writes fail. While this trades performance for safety, consider moving invalidation to success paths if write failures leave files unchanged.
How invalidation propagates:
- Non-slice blobs sharing this store:
getSize()returns the resetmax_size(sentinel), triggeringresolveSize()on next access - Slice blobs: unaffected —
getSize()returns_size, preserving immutable slice boundaries
♻️ Optional: Move invalidation to success-only paths
Per the TODO comment, moving invalidation to success paths would avoid unnecessary reloads after failed writes:
// After successful write completion, invalidate metadata:
blob_store.data.file.last_modified = jsc.init_timestamp;
blob_store.data.file.max_size = Blob.max_size;This is a minor optimization and not required for correctness.
🤖 Prompt for AI Agents
In @src/bun.js/webcore/Blob.zig around lines 1241 - 1243, The current code
resets blob_store.data.file.last_modified and blob_store.data.file.max_size to
sentinel values (jsc.init_timestamp and Blob.max_size) before performing writes,
which forces metadata reloads even if the write fails; move these two
assignments out of the pre-write path and into the post-write success path
inside the Blob write routine in Blob.zig so the metadata is only invalidated
after a successful write, keeping the same sentinel values and using the same
symbols (blob_store.data.file.last_modified, blob_store.data.file.max_size,
jsc.init_timestamp, Blob.max_size) to locate and apply the change.
|
Closing as stale: this PR predates the Rust rewrite. Every If the underlying change is still wanted, it will need to be redone against the current Rust/C++ tree. Apologies for the churn, and thank you for the contribution. |
Fixes #22456
Closes #23902
Similar to #25696 this one also fixes #4930 but from a different angle. Merging this would remove the need for #25696
The main issue was that
Bun.write(dest, source)incorrectly used the destination size as the copy limit so that writing longer content into a smaller file would lead to truncated output.Also after a successful write the metadata was not fully invalidated leading to incorrect data returned when using
.sizeetc.The fix is to use the source size as the copy limit and reset the metadata after the write.
Added
getSize()method to centralize size handling:blob.size.this let's us invalidate the size after the write so that next time size is accessed it will be fresh.
is_sliceflag was also added to distinguish between the whole file writes (which should full overwrite the destination) from explicit slices to make sure user defined boundaries are still respected and also return correct size.Added 2 regression tests that reproduce reporters' scenarios