Skip to content

fix(runtime,codegen): stop shape-probing plain-double receivers — Web Streams handle ids past k=512 dereferenced unmapped memory - #6599

Merged
proggeramlug merged 4 commits into
PerryTS:mainfrom
proggeramlug:fix/stream-band-double-shape-probe
Jul 19, 2026
Merged

fix(runtime,codegen): stop shape-probing plain-double receivers — Web Streams handle ids past k=512 dereferenced unmapped memory#6599
proggeramlug merged 4 commits into
PerryTS:mainfrom
proggeramlug:fix/stream-band-double-shape-probe

Conversation

@proggeramlug

@proggeramlug proggeramlug commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes the deterministic request-12 SIGSEGV in Next.js 16 app-router SSR under Perry (the long-hunted gscmaster "~10-render crash", previously misattributed to a missing GC root).

Root cause. Web Streams handles are raw numeric f64 ids allocated from STREAM_ID_BAND_START = 0x100000 (one shared counter across the five stream registries). A for await over a render stream resolves @@asyncIterator through the #1545 number-typed stream probe to a bound values re-dispatch, which lands in js_native_call_method with the numeric handle as receiver. The #5961 URLSearchParams fast-path block there extracted object.to_bits() & 0xFFFF_FFFF_FFFF with no pointer-tag gate, reinterpreting the double's low 48 bits as a heap address.

For a stream id 0x100000 + k, those bits decode to k * 2^32:

  • k < 512 → below the macOS heap floor (is_valid_obj_ptr's 0x200_0000_0000) → the shape probe bails benignly and the call falls through to the primitive-methods stream dispatch that owns it (why the first 11 requests work).
  • k >= 512 → ≥ 2 TB → passes is_plausible_heap_addrtry_read_gc_header dereferences unmapped memory → EXC_BAD_ACCESS.

A gscmaster render burns ~48 stream-family ids per request, so request 12's render stream (observed k = 526, receiver 1049102.0 = bits 0x4130_020E_0000_0000, faulting address 0x20E00000000) was the first past the threshold. On Linux the heap floor is 0x1000, so this same block probes low memory from the very first dynamic stream call (the #6271 "handle-band deref, Linux-only" family).

Under PERRY_GEN_GC=0 the same misprobe reads mapped-but-wrong memory instead of faulting → dispatch miss → render abort → the previously-observed "permanent 500s from ~request 11". The GC mode was never the variable; the id counter was.

Fix. Gate the URLSearchParams block on a pointer-shaped receiver — NaN-boxed pointer (0x7FFD) above the handle band, or a raw untagged heap address — mirroring the discipline of the adjacent AbortSignal block. Numeric stream receivers now always fall through to the primitive-methods stream dispatch.

Verification

Summary by CodeRabbit

  • Bug Fixes

    • Improved keys(), values(), and entries() behavior for arrays, typed collections, and stream-like values.
    • Prevented numeric values from being misinterpreted as object references during dynamic method calls.
    • Improved error handling for unsupported iterator methods.
    • Fixed stream iteration and completion behavior in type-erased and asynchronous iteration scenarios.
  • Tests

    • Added coverage for stream-handle reuse, dynamic dispatch, invalid numeric receivers, and iterator completion.

Ralph Küpper added 3 commits July 18, 2026 17:44
…inter-shaped receivers

A plain-double receiver in js_native_call_method's URLSearchParams
fast-path had its low 48 bits reinterpreted as a heap address. Web
Streams handles are raw numeric f64 ids starting at 0x100000: id
0x100000+k extracts to k*2^32, which crosses the macOS 2 TB heap floor
once k >= 512 and the shape probe then dereferences unmapped memory.
A Next.js app render burns ~48 stream ids per request, so request 12's
for-await over the render stream (@@asynciterator -> bound 'values'
re-dispatch) was the first to segfault. On Linux the heap floor is
0x1000, so low ids probed low memory from the start.

Gate the block on a pointer-shaped receiver (NaN-boxed pointer above
the handle band, or a raw untagged heap address), mirroring the
AbortSignal block below it. Numeric stream receivers fall through to
the primitive-methods stream dispatch that owns them.
… iterator fold

The PerryTS#597 any-typed .values()/.keys()/.entries() fold masked the receiver
to 48 bits in codegen before calling js_array_*_iter_obj, so a plain
double receiver (a Web Streams handle id among them) became an
indistinguishable garbage 'heap address' that the Map/Set/URLSearchParams
registry probes dereferenced — the second half of the gscmaster
request-12 SIGSEGV.

Codegen now passes the full NaN-box bits (raw heap pointers still arrive
untagged and take the legacy path bit-for-bit); the runtime routes
pointer-shaped bits as before, dispatches live Web Streams handles
through js_native_call_method to the stdlib stream arms, and throws the
spec TypeError for other primitive receivers instead of dereferencing
their bits.
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@proggeramlug, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 1 minute

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 53ef6bad-7dda-4c2d-b23b-13bc39b1a282

📥 Commits

Reviewing files that changed from the base of the PR and between 4c88e2b and ebc16a6.

📒 Files selected for processing (2)
  • crates/perry-runtime/src/array/iter_object.rs
  • test-files/test_gap_stream_id_band_dynamic_dispatch.ts
📝 Walkthrough

Walkthrough

Iterator lowering now preserves full NaN-boxed receiver bits. Runtime iterator routing distinguishes heap pointers, stream handles, and primitive values, while URLSearchParams dispatch only probes pointer-shaped receivers. A regression test covers high stream IDs and erased iterator calls.

Changes

Iterator receiver routing

Layer / File(s) Summary
Full receiver-bit preservation
crates/perry-codegen/src/expr/arrays_finds.rs, crates/perry-codegen/src/lower_array_method.rs
entries, keys, and values pass full boxed-double bits to runtime iterator helpers.
Runtime receiver dispatch
crates/perry-runtime/src/array/iter_object.rs, crates/perry-runtime/src/object/native_call_method.rs
Iterator receivers are routed as pointers, stream handles, or primitive values; URLSearchParams probing is limited to pointer-shaped receivers.
Dynamic dispatch regression coverage
test-files/test_gap_stream_id_band_dynamic_dispatch.ts
Tests primitive receiver errors, high stream-handle IDs, erased async iteration, and direct .values() iteration.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant IteratorLowering
  participant IterObject
  participant NativeCall
  participant ReadableStream
  IteratorLowering->>IterObject: pass full receiver bits
  IterObject->>NativeCall: dispatch stream iterator method
  NativeCall->>ReadableStream: invoke values or async iterator
  ReadableStream-->>NativeCall: return iterator result
  NativeCall-->>IterObject: return routed iterator
Loading

Possibly related PRs

Suggested reviewers: thehypnoo

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description has a summary and verification notes, but it is missing required template sections like Changes, Related issue, Test plan, and Checklist. Add the missing template sections with concrete change bullets, a related issue or n/a, test commands, optional screenshots, and checklist items.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is specific, single-sentence, and accurately reflects the runtime/codegen fix for Web Streams handle-id receiver probing.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/perry-runtime/src/array/iter_object.rs`:
- Around line 380-390: Update the value classification before pointer routing in
the iterator receiver logic around array_iter_obj_raw: reject zero-valued
numbers and boolean payloads with the existing TypeError path instead of
converting them to tiny/null pointers. Preserve NaN-boxing invariants and treat
values below 0x100000 as handles for small-pointer detection; add regressions
covering 0 as any and true as any for all three iterator names.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 930b303f-1236-4ad0-a6ea-7e5beb6c696b

📥 Commits

Reviewing files that changed from the base of the PR and between b389a58 and 4c88e2b.

📒 Files selected for processing (5)
  • crates/perry-codegen/src/expr/arrays_finds.rs
  • crates/perry-codegen/src/lower_array_method.rs
  • crates/perry-runtime/src/array/iter_object.rs
  • crates/perry-runtime/src/object/native_call_method.rs
  • test-files/test_gap_stream_id_band_dynamic_dispatch.ts

Comment thread crates/perry-runtime/src/array/iter_object.rs Outdated
proggeramlug added a commit that referenced this pull request Jul 18, 2026
…g-running servers (#6608)

* fix(streams): recycle Web Streams handle ids so the band survives long-running servers

The stream/reader/writer/transform id counter was monotonic over a band
only 0x100000 wide and ids were never reused, so a server minting ~48
stream-family ids per request exhausted the band after ~21k requests;
past STREAM_ID_BAND_END every band-gated classification path stops
recognizing handles (#6602, same failure class as the in-band #6599 bug
with a ~20k-request fuse).

New streams/idalloc.rs: an id RETIRES when its object reaches a terminal
state (readable closed-and-drained/errored, writable closed/errored with
no in-flight write, reader/writer releaseLock or death of their terminal
stream, transform via its writable side, tee source at unlink, pipeTo
lock ids — which never owned a registry entry at all — at release). It
then sits in a FIFO quarantine with its registry entry INTACT, keeping
full post-terminal semantics for held wrappers; only when the quarantine
overflows PERRY_STREAM_ID_QUARANTINE (default 16384) is the oldest id
evicted — registry + side tables (byob/tee/transform/expando) cleaned,
GC roots dropped — and recycled through a free list. This also fixes the
unbounded registry growth: entries used to survive for the process
lifetime.

Also: tee_error_branches now stamps the orphaned source Errored (it was
left Readable forever), and the pipe-lock acquire failure paths recycle
their freshly minted ids.

Fixes #6602.

* style: rustfmt idalloc.rs

* fix(streams): address CodeRabbit review on id recycling

- Pipe lock ids now carry an ownership mark set atomically at allocation
  in the allocator (next_pipe_lock_id) and cleared exactly once at
  retire; the old kind==0 registry probe could race a reused id's
  alloc→register window and retire a live id.
- tee::evict_ids scrubs BOTH directions of a tee relationship: a
  cancelled branch keeps its links, so a one-directional key removal
  left the source fan-out pointing at an evicted (later reused) branch
  id — chunks would land in an unrelated stream. Evicted branch slots
  zero out so the live sibling keeps receiving; dangling
  TEE_BRANCH_SOURCE rows drop.
- Branches minted from an already-errored source are born terminal with
  no tee lifecycle — retire them at creation so repeated tee() of an
  errored stream can't exhaust the band.
- Band test takes the allocator serial guard so parallel test runs
  can't steal the recycle test's free-list id.

---------

Co-authored-by: Ralph Küpper <ralph@skelpo.com>
…router

Review follow-up: top16 == 0 covered 0.0 and denormal-range doubles,
which flowed into the pointer path as null/garbage; 0x7FFC covered
booleans (payloads 3/4), which guard_coercible_this passes through to
the junk-pointer deref path. Gate the raw-pointer arm on
is_plausible_heap_addr and forward only undefined/null (payloads 1/2)
for the coercibility TypeError; zero, booleans, and denormals now reach
the spec TypeError. Gap test extended with all nine zero/boolean x
entries/keys/values cases, byte-identical to node.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant