Skip to content

fix(plugin): scope Promise event iterators - #42832

Open
thdxr wants to merge 1 commit into
v2from
promise-event-scope
Open

fix(plugin): scope Promise event iterators#42832
thdxr wants to merge 1 commit into
v2from
promise-event-scope

Conversation

@thdxr

@thdxr thdxr commented Aug 15, 2026

Copy link
Copy Markdown
Member

Summary

  • replace the Promise event adapter's unowned Stream.toAsyncIterable bridge with one child Effect scope and scoped queue per async iterator
  • mark iterators terminal before child-scope queue shutdown so pending next() calls resolve terminally and buffered events cannot escape after unload
  • close iterator scopes on consumer return, stream completion, and stream failure while preserving source failures
  • independently own multiple iterators from the same iterable and from separate subscribe() calls

Design

Stream.toAsyncIterable creates an internal unsafe scope per iterator, but that scope is only closed by the iterator's own return(). Tracking those iterators initially looked sufficient, but Effect v4 can block return() behind a pending next() on a never-ending source.

The adapter now uses Stream.toQueue inside a child scope forked from the plugin generation. Consumer return closes and detaches that child; plugin replacement, unload, failed-load cleanup, and parent shutdown close it through normal Effect scope ownership. A LIFO child finalizer marks the wrapper terminal before queue shutdown wakes pending takes.

Tests

  • active never-ending stream finalizes on plugin-scope close with a pending next()
  • post-close and post-completion/error next() calls remain terminal
  • normal for await break releases the Bus subscription
  • source completion and failure finalize resources, with failure still propagated
  • multiple iterators and multiple subscriptions close independently
  • replacing a Promise plugin closes its active event iterator
  • bun test in packages/plugin
  • bun test test/plugin/promise.test.ts test/plugin.test.ts in packages/core
  • bun typecheck in packages/plugin
  • Prettier and oxlint on changed files

Verification Note

bun typecheck in packages/core and the pre-push repository-wide typecheck are currently blocked by unrelated baseline DOM typing errors for Headers, Request, and Response; the pre-push Turbo process also exited with SIGSEGV. The affected plugin package typecheck and focused core tests pass.

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