Skip to content

fix(io): Fix stale Blob cache and truncation in Bun.write - #25702

Closed
mattermoran wants to merge 8 commits into
oven-sh:mainfrom
mattermoran:fix/bun-write-truncation
Closed

mattermoran wants to merge 8 commits into
oven-sh:mainfrom
mattermoran:fix/bun-write-truncation

Conversation

@mattermoran

@mattermoran mattermoran commented Dec 26, 2025 •

Copy link
Copy Markdown
Contributor

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 .size etc.

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:

  • For full files size is always derived from the store.
  • For slices and other types the size comes from blob.size.
    this let's us invalidate the size after the write so that next time size is accessed it will be fresh.

is_slice flag 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

@coderabbitai

coderabbitai Bot commented Dec 26, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Introduces an internal _size backing and is_slice on Blob, replaces direct blob.size reads/writes with accessors (getSize(), setSize(), getSizeForJS()), updates structured-clone serialization versioning, routes JS-facing size access to getSizeForJS(), and adds regression tests for size-cache/truncation behaviors.

Changes

Cohort / File(s) Summary
Core Blob Type
src/bun.js/webcore/Blob.zig
Added _size: SizeType and is_slice: bool; added public getSize(), setSize(), getSizeForJS(); bumped serialization_version and updated constructors/serialization to use _size and is_slice.
Blob I/O & loaders
src/bun.js/webcore/blob/read_file.zig, src/bun.js/webcore/ByteBlobLoader.zig, src/bun.js/webcore/ReadableStream.zig, src/bun.js/webcore/Body.zig, src/bun.js/webcore/fetch.zig
Replaced direct blob.size reads with blob.getSize() and writes with blob.setSize(); updated truncation, chunking, max_size and read paths to use accessors.
S3 / File-backed paths
src/bun.js/webcore/S3File.zig, src/bun.js/webcore/ReadableStream.zig
Switched blob size retrieval in JS-exposed contexts to getSizeForJS() (S3/file-backed branch); other internal paths use getSize().
Server & bindings
src/bun.js/api/server/FileRoute.zig, src/bun.js/api/server/RequestContext.zig, src/bun.js/bindings/JSGlobalObject.zig
Replaced direct field accesses with getSize()/setSize() in stat, transfer, ReadableStream creation, and request content-length logic.
Output / Bundler
src/OutputFile.zig, src/bun.js/api/JSBundler.zig
Replaced blob.size = ... with blob.setSize(...); bundler now uses getSizeForJS() for JS-facing size retrieval.
JS bindings / response
src/bun.js/webcore/response.classes.ts
Blob.prototype.size getter changed to call getSizeForJS() (JS-visible getter).
Tests
test/regression/issue/22456.test.ts, test/regression/issue/4930.test.ts
Added regression tests ensuring Bun.write copies full contents despite cached destination sizes and that BunFile.exists() does not poison subsequent writes.

Possibly related PRs

Suggested reviewers

  • Jarred-Sumner
  • nektro
  • taylordotfish
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main fix: preventing stale Blob cache and truncation issues in Bun.write operations.
Description check ✅ Passed The PR description comprehensively explains the root cause, the fix approach, and includes references to linked issues and test additions.
Linked Issues check ✅ Passed The code changes directly address all three linked issues by using source size for copy limits, invalidating metadata after writes, and centralizing size handling with getSize().
Out of Scope Changes check ✅ Passed All changes are within scope: Blob size accessors, metadata invalidation, is_slice flag tracking, and two regression tests directly address the linked issues.

✏️ 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mattermoran mattermoran changed the title fix(fs): fix stale Blob cache and truncation in Bun.write fix(fs): Fix stale Blob cache and truncation in Bun.write Dec 26, 2025
@mattermoran mattermoran changed the title fix(fs): Fix stale Blob cache and truncation in Bun.write fix(io): Fix stale Blob cache and truncation in Bun.write Dec 26, 2025
Comment thread src/bun.js/node/types.zig Outdated
pub const PathOrBlob = union(enum) {
path: jsc.Node.PathOrFileDescriptor,
blob: Blob,
blob: *Blob,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is saved somewhere so the Blob pointer could become invalid?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup reverted that change and decided to only use store size as the source of truth for files

@Jarred-Sumner Jarred-Sumner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of tracking is_slice, what if we don't cache the size when the file doesn't exist?

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

📥 Commits

Reviewing files that changed from the base of the PR and between f8f9584 and 6a7935e.

📒 Files selected for processing (13)
  • src/OutputFile.zig
  • src/bun.js/api/JSBundler.zig
  • src/bun.js/api/server/FileRoute.zig
  • src/bun.js/api/server/RequestContext.zig
  • src/bun.js/bindings/JSGlobalObject.zig
  • src/bun.js/webcore/Blob.zig
  • src/bun.js/webcore/Body.zig
  • src/bun.js/webcore/ByteBlobLoader.zig
  • src/bun.js/webcore/ReadableStream.zig
  • src/bun.js/webcore/S3File.zig
  • src/bun.js/webcore/blob/read_file.zig
  • src/bun.js/webcore/fetch.zig
  • src/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 @import at the bottom of the file (auto formatter will move them automatically)

Files:

  • src/bun.js/webcore/blob/read_file.zig
  • src/bun.js/webcore/S3File.zig
  • src/bun.js/api/server/FileRoute.zig
  • src/bun.js/webcore/ReadableStream.zig
  • src/bun.js/webcore/ByteBlobLoader.zig
  • src/bun.js/webcore/Body.zig
  • src/bun.js/api/JSBundler.zig
  • src/OutputFile.zig
  • src/bun.js/webcore/fetch.zig
  • src/bun.js/api/server/RequestContext.zig
  • src/bun.js/bindings/JSGlobalObject.zig
  • 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/read_file.zig
  • src/bun.js/webcore/S3File.zig
  • src/bun.js/api/server/FileRoute.zig
  • src/bun.js/webcore/ReadableStream.zig
  • src/bun.js/webcore/ByteBlobLoader.zig
  • src/bun.js/webcore/Body.zig
  • src/bun.js/api/JSBundler.zig
  • src/OutputFile.zig
  • src/bun.js/webcore/fetch.zig
  • src/bun.js/api/server/RequestContext.zig
  • src/bun.js/bindings/JSGlobalObject.zig
  • src/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.zig
  • src/bun.js/api/server/FileRoute.zig
  • src/bun.js/webcore/response.classes.ts
  • src/bun.js/webcore/ReadableStream.zig
  • src/bun.js/webcore/ByteBlobLoader.zig
  • src/bun.js/webcore/fetch.zig
  • src/bun.js/api/server/RequestContext.zig
  • src/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.zig
  • src/bun.js/webcore/S3File.zig
  • src/OutputFile.zig
  • src/bun.js/api/server/RequestContext.zig
  • src/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.zig
  • src/bun.js/api/JSBundler.zig
  • src/bun.js/api/server/RequestContext.zig
  • src/bun.js/bindings/JSGlobalObject.zig
  • src/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.zig
  • src/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.zig
  • src/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 paths

Using blob.setSize(@as(jsc.WebCore.Blob.SizeType, @truncate(...))) in SavedFile.toJS, toJS’s .buffer arm, and toBlob’s .buffer arm 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 of blob.getSize() for WASM streaming is consistent with new Blob API

Passing blob.getSize() to ReadableStream.fromBlobCopyRef after blob.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 accessor

Switching getSize to delegate to Blob.getSizeForJS is 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 accessor

Mapping the size getter 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 uses this.Blob.getSize() == 0, which maintains previous behavior while honoring slice-aware size.
  • toReadableStream passes blob.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: S3 size() now delegates blob case to JS-facing Blob size

For 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

  • _size is 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_size parameter now also keys off this.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/S3

Both the file-backed and S3-backed branches now use blob.getSize() with the Blob.max_size sentinel 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 size field access with getSize() and setSize() 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_size parameter to NodeFS.readFile

Both 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 in setup() to initialize chunk_size and remain based on blob size
  • Line 114: setSize() correctly used in toAnyBlob() to set the reconstructed blob's size to remaining bytes

The 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 _size storage and the is_slice flag 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() use store.data.file.max_size for file-backed non-slice blobs, allowing size to track file changes
  • They use _size for slices and byte blobs, where size is immutable
  • getSizeForJS() properly handles edge cases (S3, non-seekable files) for JavaScript consumers

The is_slice flag 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_slice to 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 boundaries

This 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 trigger resolveSize() → 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.

Comment thread src/bun.js/webcore/Blob.zig Resolved
Comment thread src/bun.js/webcore/blob/read_file.zig Resolved
@mattermoran

mattermoran commented Dec 29, 2025 •

Copy link
Copy Markdown
Contributor Author

Instead of tracking is_slice, what if we don't cache the size when the file doesn't exist?

We still need is_slice to ensure we don't write past the slice boundary, but also for the new .getSize() method.

.getSize() always uses the store size for full files while slices and other types gets their size from blob size.

Without getSize we basically end up with 2 separate size properties for files. And as you noticed it also gets really hard to correctly invalidate the cached size for files after writes.

Also simply not caching the size when the file doesn’t exist wouldn’t solve the problem.
For example writing AAA and calling getSize() would cache 3, writing AAAAA afterward would still return 3, putting us back in the same situation.

Using .getSize() eliminates that issue because we only need to invalidate the store.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: Set is_slice = true when 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 ensuring is_slice = true for file stores. Since getSize() returns max_size directly for file blobs with is_slice = false (ignoring _size), the assignment of _size at line 3163 in resolveSize() has no effect for deserialized file blobs with offsets. This creates an inconsistency: getSliceFrom() correctly sets both offset and is_slice = true together, but deserialization does not. Either set is_slice = true when offset != 0 is deserialized for file stores, or revise the size computation to account for offset regardless of the is_slice flag.

📜 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 6a7935e and 1d28daa.

📒 Files selected for processing (2)
  • src/bun.js/webcore/Blob.zig
  • src/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 @import at the bottom of the file (auto formatter will move them automatically)

Files:

  • src/bun.js/webcore/Blob.zig
  • src/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.zig
  • src/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.zig
  • src/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.zig
  • src/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_size approach correctly avoids redundant getSize() calls while using the public accessors (getSize() and setSize()) 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 _size

This 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_modified to init_timestamp and max_size to Blob.max_size correctly 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 _size and is_slice here is appropriate because getSliceFrom() is constructing a new blob state. The accessor methods (getSize()/setSize()) will subsequently route to _size for 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:

  1. Fixes stale metadata (#23902): setSize() enables synchronous invalidation of destination blob size after writes
  2. Prevents truncation (#22456): Line 1057 correctly uses is_slice to distinguish explicit slice boundaries from whole-file writes
  3. 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

@mattermoran

Copy link
Copy Markdown
Contributor Author

If needed I can also pull out the getSize changes into a separate PR

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 for is_slice field.

This PR adds a new is_slice field to the serialization format but has multiple critical bugs:

  1. Wrong deserialization position: Line 434 reads is_slice immediately after store_tag, but serialization writes it at line 332 after store.serialize() (line 326). This means deserialization reads bytes that are actually store data, causing data corruption.

  2. 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 read is_slice that doesn't exist in the serialized data.

  3. 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_slice

Required fixes:

  • Bump serialization_version to 4 (line 69)
  • Move the is_slice read to after the versions: block (after line 518), not before blob creation
  • Gate is_slice serialization/deserialization on version >= 4
  • Default is_slice = false for 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 object
 const 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 @import at 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(), and getSizeForJS() methods correctly centralize size handling:

  • Full-file blobs use store.data.file.max_size
  • Slices use _size
  • The is_slice flag correctly prevents file stores from overwriting slice sizes

The usage throughout the file is consistent with direct _size access only in initialization and slice creation paths, which is appropriate.


1232-1234: Cache invalidation logic correctly implemented.

Resetting last_modified and max_size when 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_slice is 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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 _size assignments in resolveSize() are correct because this method is only called when getSize() == Blob.max_size (unknown size), which doesn't occur for slices:

Call sites verified (all check getSize() == max_size before calling):

  • Lines 241-242: FormData handling
  • Lines 2177-2179: getExistsSync()
  • Lines 3051-3053: getSizeForBindings()
  • Lines 3142-3147: getSizeForJS()

Why this works:

  • Slices: _size is set explicitly in getSliceFrom() (line 2804), so getSize() returns a known value, preventing resolveSize() from being called
  • Non-slice file blobs: _size assignments are benign; getSize() returns max_size (updated by resolveFileStat())
  • Bytes blobs: _size correctly 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.

📥 Commits

Reviewing files that changed from the base of the PR and between 9fc5374 and 9734c00.

📒 Files selected for processing (6)
  • src/bun.js/api/JSBundler.zig
  • src/bun.js/api/server/RequestContext.zig
  • src/bun.js/webcore/Blob.zig
  • src/bun.js/webcore/ReadableStream.zig
  • src/bun.js/webcore/S3File.zig
  • src/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 @import at the bottom of the file (auto formatter will move them automatically)

Files:

  • src/bun.js/api/server/RequestContext.zig
  • src/bun.js/webcore/S3File.zig
  • src/bun.js/webcore/ReadableStream.zig
  • src/bun.js/api/JSBundler.zig
  • src/bun.js/webcore/fetch.zig
  • 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/api/server/RequestContext.zig
  • src/bun.js/webcore/S3File.zig
  • src/bun.js/webcore/ReadableStream.zig
  • src/bun.js/api/JSBundler.zig
  • src/bun.js/webcore/fetch.zig
  • src/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.zig
  • src/bun.js/webcore/S3File.zig
  • src/bun.js/webcore/ReadableStream.zig
  • src/bun.js/api/JSBundler.zig
  • src/bun.js/webcore/fetch.zig
  • 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/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.zig
  • src/bun.js/api/JSBundler.zig
  • 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/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.zig
  • src/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: Route BuildArtifact.getSize through JS-facing Blob accessor

Using Blob.getSizeForJS here is correct: JS-visible size now 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 S3 size() with JS-visible Blob size

Switching the blob branch of size() to Blob.getSizeForJS correctly 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: Use Blob.getSize() for sendfile and fallback readFile limits

Replacing direct field access with body.AnyBlob.Blob.getSize() for original_size and .max_size keeps both the sendfile fast path and the synchronous NodeFS.readFile fallback 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: Use blob.getSize() when creating file/S3-backed ReadableStreams

The .file and .s3 branches now derive their max_size/length from blob.getSize(), which correctly respects slice boundaries and the unified Blob size/cache behavior. Mapping Blob.max_size to null preserves the previous “unbounded” semantics.

src/bun.js/api/server/RequestContext.zig (3)

909-1000: Sendfile path: switch to getSize()/setSize() for Blob sizing

renderSendFile now derives original_size from this.blob.Blob.getSize() and updates via setSize(...), keeping the Blob’s cached size in sync with stat_size and the shared size model. Regular files still use stat_size, while non-regular files are bounded by min(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 accessors

In onReadFile, the temporary-buffer case now uses this.blob.Blob.getSize() and setSize(...) 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 on blob.getSize()

After blob.resolveSize(), using blob.getSize() for the HEAD content-length header correctly reflects the canonical Blob size (including slices), with the existing Blob.max_size → 0 fallback 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(): Returns store.data.file.max_size for non-slice file blobs, _size otherwise. This centralizes the logic for determining which backing field holds the authoritative size.
  • setSize(): Mirrors getSize() for writes, ensuring size updates go to the correct backing field.
  • getSizeForJS(): Handles unknown sizes (max_size) by calling resolveSize() 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 direct max_size assignment) 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 truncation

This 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 of is_slice flag is correctly versioned.

The version 4 serialization change properly adds is_slice to 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

Comment on lines +1241 to +1243
// 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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 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 reset max_size (sentinel), triggering resolveSize() 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.

@robobun

robobun commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Closing as stale: this PR predates the Rust rewrite. Every src/ file it modifies has since been removed or relocated on main (Zig sources deleted; src/bun.js/ reorganized into src/jsc/), so it can no longer merge.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants