Skip to content

fix(Stream): avoid mutable Channel.runFold accumulator in mkUint8Array#2127

Closed
stephanschielke wants to merge 1 commit intoEffect-TS:mainfrom
stephanschielke:fix/stream-mkuint8array-mutable-fold
Closed

fix(Stream): avoid mutable Channel.runFold accumulator in mkUint8Array#2127
stephanschielke wants to merge 1 commit intoEffect-TS:mainfrom
stephanschielke:fix/stream-mkuint8array-mutable-fold

Conversation

@stephanschielke
Copy link
Copy Markdown

@stephanschielke stephanschielke commented May 6, 2026

Fixes #2126

What

Stream.mkUint8Array was refactored in 4.0.0-beta.59 to use Channel.runFold with a mutable accumulator (acc.bytes +=, acc.arrays.push). In Bun --compile --minify binaries, mutating an object that is passed repeatedly as the accumulator to a fold step callback throws Attempting to define property on object that is not extensible.

This does NOT reproduce with:

  • bun run (interpreted mode)
  • bun build --compile without --minify
  • Any Node.js runtime

Fix

Revert to the pre-beta.59 immutable Channel.runFold pattern where each fold iteration returns a new Uint8Array instead of mutating a shared { bytes, arrays } object. This matches the established convention used by Multipart.collectUint8Array (line 472) and what mkUint8Array itself used before beta.59.

Verification

All commands run from the repo root:

  • pnpm --filter effect check -- zero type errors
  • pnpm lint-fix -- zero warnings, zero errors
  • pnpm --filter effect test run -- Stream.test.ts -- all Stream tests pass. 7 pre-existing failures in unrelated files (EffectKeepAlive, Command, OtlpMetrics) -- same count on upstream/main without this change.

Tests

Added three tests for mkUint8Array:

  • basic concatenation across two chunks
  • many-chunk regression (10 single-byte chunks -- the exact failing scenario)
  • empty stream edge case

Real-world impact

opencode v1.14.34+ crashes on every second LLM tool call in compiled binaries because snapshot/index.ts calls Stream.mkUint8Array(handle.stdout) to collect git cat-file --batch output. See anomalyco/opencode#25873 and anomalyco/opencode#25867.

Note

Stream.runCollect (line 9907) also mutates its fold accumulator (acc.push(chunk[i])). It may be affected by the same Bun compiled-mode behavior. Not fixing here to keep this PR focused on the confirmed crash site.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 6, 2026

🦋 Changeset detected

Latest commit: 6d88699

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
Name Type
effect Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/vitest Patch
@effect/ai-anthropic Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/openapi-generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@stephanschielke
Copy link
Copy Markdown
Author

Note on CI: there are 7 pre-existing test failures on main that are unrelated to this change:

  • test/EffectKeepAlive.test.ts (2 failures -- timing assertions)
  • test/unstable/cli/Command.test.ts (4 failures -- toMatchInlineSnapshot context error)
  • test/unstable/observability/OtlpMetrics.test.ts (1 failure -- gauge delta assertion)

Verified locally by running the test suite on upstream/main without this change -- same 7 failures. With this change, the count stays at 7 (zero regressions introduced).

@stephanschielke stephanschielke force-pushed the fix/stream-mkuint8array-mutable-fold branch from 7ed780a to b44edfb Compare May 6, 2026 07:36
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

📊 JSDoc Documentation Analysis

📈 Current Analysis Results
Analyzing 136 TypeScript files in packages/effect/src/ (including schema and config subdirectories)...

============================================================
         EFFECT JSDOC ANALYSIS REPORT
============================================================

📊 SUMMARY STATISTICS
------------------------------
Total files analyzed: 136
Total exported members: 4318
Missing @example: 2184 (50.6%)
Missing @category: 397 (9.2%)

🎯 TOP FILES NEEDING ATTENTION
----------------------------------------
1. Schema.ts
   📝 520 missing examples, 🏷️  203 missing categories
   📦 520 total exports
2. Array.ts
   📝 135 missing examples, 🏷️  2 missing categories
   📦 140 total exports
3. SchemaRepresentation.ts
   📝 96 missing examples, 🏷️  12 missing categories
   📦 96 total exports
4. SchemaAST.ts
   📝 77 missing examples, 🏷️  21 missing categories
   📦 77 total exports
5. Cause.ts
   📝 79 missing examples, 🏷️  2 missing categories
   📦 79 total exports
6. Channel.ts
   📝 81 missing examples, 🏷️  0 missing categories
   📦 152 total exports
7. SchemaTransformation.ts
   📝 43 missing examples, 🏷️  31 missing categories
   📦 43 total exports
8. Option.ts
   📝 66 missing examples, 🏷️  3 missing categories
   📦 66 total exports
9. Sink.ts
   📝 64 missing examples, 🏷️  2 missing categories
   📦 81 total exports
10. Predicate.ts
   📝 57 missing examples, 🏷️  0 missing categories
   📦 57 total exports
11. SchemaGetter.ts
   📝 51 missing examples, 🏷️  0 missing categories
   📦 51 total exports
12. Result.ts
   📝 46 missing examples, 🏷️  0 missing categories
   📦 46 total exports
13. Config.ts
   📝 35 missing examples, 🏷️  6 missing categories
   📦 35 total exports
14. Types.ts
   📝 39 missing examples, 🏷️  0 missing categories
   📦 39 total exports
15. Effect.ts
   📝 32 missing examples, 🏷️  2 missing categories
   📦 255 total exports

✅ PERFECTLY DOCUMENTED FILES
-----------------------------------
   Chunk.ts (86 exports)
   Clock.ts (5 exports)
   FiberHandle.ts (15 exports)
   FiberMap.ts (19 exports)
   FiberSet.ts (14 exports)
   HKT.ts (4 exports)
   HashMap.ts (44 exports)
   HashSet.ts (21 exports)
   Match.ts (57 exports)
   MutableRef.ts (17 exports)
   NonEmptyIterable.ts (3 exports)
   Random.ts (9 exports)
   Redacted.ts (9 exports)
   RegExp.ts (3 exports)
   Symbol.ts (1 exports)
   Trie.ts (29 exports)
   TxChunk.ts (22 exports)
   TxDeferred.ts (7 exports)
   TxHashMap.ts (41 exports)
   TxHashSet.ts (24 exports)
   TxPriorityQueue.ts (19 exports)
   TxReentrantLock.ts (17 exports)
   TxRef.ts (7 exports)
   TxSemaphore.ts (14 exports)
   TxSubscriptionRef.ts (12 exports)
   Unify.ts (8 exports)
   index.ts (0 exports)

🔍 SAMPLE MISSING ITEMS FROM Schema.ts
-----------------------------------
   Optionality (type, line 150): missing example, category
   Mutability (type, line 159): missing example, category
   ConstructorDefault (type, line 169): missing example, category
   MakeOptions (interface, line 183): missing example, category
   Bottom (interface, line 211): missing example, category
   declareConstructor (interface, line 269): missing example
   declareConstructor (function, line 333): missing example
   declare (interface, line 360): missing example
   declare (function, line 393): missing example
   revealBottom (function, line 433): missing example, category

📋 BREAKDOWN BY EXPORT TYPE
-----------------------------------
const: 1126 missing examples, 114 missing categories
interface: 346 missing examples, 91 missing categories
type: 253 missing examples, 79 missing categories
function: 353 missing examples, 93 missing categories
namespace: 48 missing examples, 20 missing categories
class: 58 missing examples, 0 missing categories

📈 DOCUMENTATION PROGRESS
------------------------------
Examples: 2134/4318 (49.4% complete)
Categories: 3921/4318 (90.8% complete)

============================================================
Analysis complete! 2581 items need attention.
============================================================

📄 Detailed results saved to: jsdoc-analysis-results.json

This comment is automatically updated on each push. View the analysis script for details.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

Bundle Size Analysis

File Name Current Size Previous Size Difference
basic.ts 6.73 KB 6.72 KB +0.00 KB (+0.03%)
batching.ts 9.23 KB 9.23 KB +0.00 KB (+0.02%)
brand.ts 6.45 KB 6.45 KB 0.00 KB (0.00%)
cache.ts 9.99 KB 9.99 KB +0.00 KB (+0.01%)
config.ts 18.45 KB 18.45 KB +0.01 KB (+0.03%)
differ.ts 16.88 KB 16.88 KB +0.01 KB (+0.03%)
http-client.ts 20.20 KB 20.20 KB +0.00 KB (+0.01%)
logger.ts 10.20 KB 10.20 KB +0.00 KB (+0.01%)
metric.ts 8.57 KB 8.57 KB +0.00 KB (+0.05%)
optic.ts 7.61 KB 7.61 KB 0.00 KB (0.00%)
pubsub.ts 14.10 KB 14.10 KB +0.00 KB (+0.01%)
queue.ts 11.09 KB 11.09 KB +0.00 KB (+0.02%)
schedule.ts 10.31 KB 10.31 KB +0.01 KB (+0.05%)
schema-representation-roundtrip.ts 26.19 KB 26.19 KB +0.00 KB (+0.01%)
schema-string-transformation.ts 12.62 KB 12.61 KB +0.01 KB (+0.05%)
schema-string.ts 10.36 KB 10.36 KB +0.00 KB (+0.03%)
schema-template-literal.ts 13.98 KB 13.97 KB +0.00 KB (+0.03%)
schema-toArbitraryLazy.ts 18.51 KB 18.51 KB +0.01 KB (+0.03%)
schema-toCodeDocument.ts 21.19 KB 21.18 KB +0.00 KB (+0.01%)
schema-toCodecJson.ts 17.75 KB 17.75 KB +0.01 KB (+0.03%)
schema-toEquivalence.ts 17.68 KB 17.68 KB +0.00 KB (+0.02%)
schema-toFormatter.ts 17.52 KB 17.52 KB +0.00 KB (+0.02%)
schema-toJsonSchemaDocument.ts 20.03 KB 20.03 KB +0.00 KB (+0.01%)
schema-toRepresentation.ts 18.08 KB 18.08 KB +0.00 KB (+0.02%)
schema.ts 17.09 KB 17.08 KB +0.01 KB (+0.03%)
stm.ts 11.97 KB 11.96 KB +0.01 KB (+0.04%)
stream.ts 9.26 KB 9.26 KB +0.00 KB (+0.04%)

@IMax153
Copy link
Copy Markdown
Member

IMax153 commented May 9, 2026

Closing this PR as the issue is not on our end.

@IMax153 IMax153 closed this May 9, 2026
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.

Stream.mkUint8Array crashes in Bun compiled+minified binaries: mutable Channel.runFold accumulator

2 participants