Skip to content

fix(perry-ext-fastify): #747 + #746 — event_loop + wait_for_promise pump stdlib#751

Merged
proggeramlug merged 1 commit into
mainfrom
worktree-fix-746-747-748
May 13, 2026
Merged

fix(perry-ext-fastify): #747 + #746 — event_loop + wait_for_promise pump stdlib#751
proggeramlug merged 1 commit into
mainfrom
worktree-fix-746-747-748

Conversation

@proggeramlug
Copy link
Copy Markdown
Contributor

Summary

Three edits in crates/perry-ext-fastify/src/server.rs:

  1. extern "C" block: declare js_run_stdlib_pump (defined in crates/perry-runtime/src/lib.rs:177, dispatches through the STDLIB_PUMP_FN AtomicPtr that perry-stdlib registers at startup).
  2. event_loop: call it ahead of js_promise_run_microtasks(), matching perry-stdlib's order.
  3. wait_for_promise: same call inside the per-iteration body, for parity with perry-stdlib's wait_for_promise at crates/perry-stdlib/src/fastify/server.rs:432 — defense-in-depth for the rare case of an externally-resolved Promise returned by a route handler without an inner await chain.

Test plan

@proggeramlug proggeramlug force-pushed the worktree-fix-746-747-748 branch from 85c82cb to c87e101 Compare May 13, 2026 09:32
…ump stdlib (v0.5.904)

The perry-stdlib→perry-ext port (v0.5.572) dropped the
`js_stdlib_process_pending` call that the original
`crates/perry-stdlib/src/fastify/server.rs` had at the top of every
event-loop tick. As a result, any program combining `fastify` with
a perry-ext-* wrapper (ws, net, http, fetch) saw its listener
callbacks stranded the moment `app.listen()` entered its loop —
events accumulated on tokio workers but the JS main thread never
drained the per-wrapper queues. The user's hub reproduced this as
"`wss.on('connection', cb)` never fires" on v0.5.700–v0.5.892, filed
as #746; the underlying defect is in perry-ext-fastify, filed as

Three edits in `crates/perry-ext-fastify/src/server.rs`:

1. extern "C" block: declare `js_run_stdlib_pump` (defined in
   `crates/perry-runtime/src/lib.rs:177`).
2. `event_loop`: call `js_run_stdlib_pump()` ahead of
   `js_promise_run_microtasks()`, matching perry-stdlib's order.
3. `wait_for_promise`: same call inside the per-iteration body,
   for parity with `crates/perry-stdlib/src/fastify/server.rs:432`
   (defense-in-depth — the codegen-emitted `await` loop at
   `crates/perry-codegen/src/expr.rs:9500` already pumps stdlib,
   so this only matters for the rare case where a route handler
   returns a Promise resolved by an external mechanism without an
   inner `await` chain).

Validation:

- Fastify + WS combo before fix: `[wss listening]` + `[wss connection
  fired]` never print; after fix: both fire on the next tick.
- Pure-WS minimal repro: already passed on macOS at HEAD (covered
  by the codegen-emitted main event loop's existing pump call). The
  combined fastify+ws path is what the hub actually exercises, so
  this closes #746 in practice.
- Multi-await fastify handler (3× Promise.resolve + reply.code(201))
  returns correct JSON in 4 ms.
- Multi-await perry-ext-bcrypt chain (2× hash + 1× compare inside a
  POST handler) returns correct JSON in 4 ms.
- `cargo test --release -p perry-ext-fastify`: 10 passed.
- `cargo test --release --workspace --exclude perry-ui-{ios,tvos,
  watchos,visionos,android,windows,gtk4}`: exit 0.

wait_for_promise omission caused @perryts/mysql's `await
pool.exec(...)` chain to stall after the first INSERT, but a
perry-ext-bcrypt chain (same shape, same queue_promise_resolution
mechanism) works fine without the wait_for_promise pump — the
codegen-emitted await loop pumps stdlib on every iteration, so
inner awaits don't strand. The actual #748 root cause needs
reproduction with a real @perryts/mysql + MySQL setup and is left
open.

Closes #747.
Closes #746 (for the realistic fastify+ws use case; the pure-WS-only
Linux symptom from the minimal repro doesn't reproduce on macOS at
HEAD — left as a Linux follow-up if it persists).
Refs #748 (investigated but not closed; see CHANGELOG for full
context).
@proggeramlug proggeramlug force-pushed the worktree-fix-746-747-748 branch from c87e101 to 38cb7d6 Compare May 13, 2026 12:05
@proggeramlug proggeramlug merged commit 209ba11 into main May 13, 2026
9 checks passed
@proggeramlug proggeramlug deleted the worktree-fix-746-747-748 branch May 13, 2026 12:12
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.

WebSocketServer.on() callbacks never fire on incoming connections (regression v0.5.491–v0.5.700+)

1 participant