fix(shared): await fn() in runWithSpan / runInSpanContext so an already-rejected promise is not observed as unhandled - #1984
Conversation
…dy-rejected promise is not observed as unhandled
|
I reproduced this under workerd (the Why workerd reports it and Node does not
workerd already fixed this behind a compat flag workerd now defers the check to V8's microtasks-completed callback (same timing as Node), gated by the Results with the reproduction from this PR (plus a bare
So bumping About a test
|
|
Withdrawing this for now — I want to prepare it more carefully (with a proper test) before asking for a review. Sorry for the noise. |
Summary
runWithSpanandrunInSpanContextreturnfn()directly when no tracer / span is configured:When
fnis anasynchandler that throws before its firstawait(a very common shape — e.g. an auth guard as the first statement of a procedure handler),fn()returns an already-rejected promise. Returning it from anasyncfunction withoutawaitresolves the outer promise through aPromiseResolveThenableJob, so the rejection handler is attached a couple of microtasks after the rejection happened.Node ignores that gap, but workerd (Cloudflare Workers) reports it as an unhandled rejection. Under
@cloudflare/vitest-pool-workersevery such handler call shows up asUnhandled Rejectionin the test summary (and fails the run on vitest 4), even thoughRPCHandler.handle()correctly turns the error into the error response.This PR changes both no-tracer paths to
return await fn(), which attaches the handler immediately (the same thing the tracer path already does viareturn await fn(span)insidecallback). No behavior change otherwise.Reproduction
In a
@cloudflare/vitest-pool-workersproject (vitest 4, pool-workers 0.16 or 0.22):Bisect results in that environment:
throwasync,throwafter anawaitasync,throwbefore the firstawait@orpc/sharedWe currently carry this as a one-line
patchin our repo; happy to add a test if you can suggest a way to assert it outside of workerd (Node's unhandled-rejection tracking does not observe the gap).