Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions crates/perry-ext-fastify/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,22 @@ pub unsafe extern "C" fn js_fastify_listen(app_handle: Handle, opts: f64, callba
let upgrade_tx_for_spawn = upgrade_tx.clone();
let routes_for_spawn = routes_arc.clone();

perry_ffi::spawn_blocking(move || {
let handle = tokio::runtime::Handle::current();
handle.block_on(async move {
// The accept loop must run as a cooperative task on the shared
// multi-thread runtime. A plain `spawn_blocking` thread does not
// reliably carry the runtime's reactor/worker context: with
// `Handle::current().block_on(accept_loop)` the listener bound and
// accepted connections, but the per-connection
// `tokio::spawn(serve_connection)` tasks below were never driven — the
// request bytes sat unread and every response hung (the "in release the
// IO loop silently failed to start" brittleness the net/ws adapters
// hit). `spawn_blocking_with_reactor` runs the closure inside a worker
// task (`runtime().spawn(async { … })`), so `tokio::spawn`-ing the
// accept loop drives it and its fan-out serve tasks on the worker pool —
// mirroring perry-ext-http-server / -net / -ws. (A bare
// `Handle::current().block_on` here would panic "Cannot start a runtime
// from within a runtime" inside the worker task; spawn instead.)
perry_ffi::spawn_blocking_with_reactor(move || {
tokio::spawn(async move {
let addr = SocketAddr::from(([0, 0, 0, 0], port));
let listener = match TcpListener::bind(addr).await {
Ok(l) => l,
Expand Down
30 changes: 25 additions & 5 deletions crates/perry-hir/src/lower/expr_call/prescans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,37 @@ pub(super) fn run_call_prescans(
let fastify_handler_names: Option<(String, String)> =
pre_scan_fastify_handler_params(ctx, call);
if let Some((req_name, reply_name)) = &fastify_handler_names {
ctx.register_native_instance(
// The arrow's own `req`/`reply` param bindings would otherwise tombstone
// these fresh tags via `shadow_native_instance_if_present` (the arrow
// param-shadowing added in #5438) — protect them so `request.header(...)`
// and `reply.code(...).send(...)` inside the handler keep dispatching
// through the fastify Request/Reply table rows. Protect only when
// registration actually happened (it no-ops under a `perry.compilePackages`
// override), else a later same-named param would skip tombstoning a
// genuinely stale tag — same gating as the `wsId`/`sock` sites below.
//
// Without this, an untyped `(req, reply) =>` handler lost the Reply tag,
// so every `reply.*` lowered to the generic fallback and silently no-op'd:
// `reply.code(n)`/`status(n)` were ignored (status stayed 200),
// `reply.send(x)` left the body empty, and `reply.code(n).send(x)` threw
// (the un-dispatched `.code(n)` returned undefined). Typed
// `reply: FastifyReply` handlers were masked by the post-shadow type-based
// re-registration, which is why only the common untyped JS form broke.
if ctx.register_native_instance(
req_name.clone(),
"fastify".to_string(),
"Request".to_string(),
);
if !reply_name.is_empty() {
ctx.register_native_instance(
) {
ctx.protect_native_param(req_name.clone());
}
if !reply_name.is_empty()
&& ctx.register_native_instance(
reply_name.clone(),
"fastify".to_string(),
"Reply".to_string(),
);
)
{
ctx.protect_native_param(reply_name.clone());
}
}

Expand Down
Loading