Unverified Commit 1217af17 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

test: derive uncaughtException ALS context from native microtask behavior

The fast setImmediate patch rethrows sync errors from a queueMicrotask.
Whether AsyncLocalStorage context is visible in the resulting
uncaughtException depends on the runtime: it is only preserved when
AsyncContextFrame is enabled (the default since Node 24). The two
affected tests hardcoded `undefined` and failed on Node 24.

Probe what a native queueMicrotask throw sees and assert that exact
value instead.

<!-- NEXT_JS_LLM -->
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent fa1eab66
...@@ -1483,6 +1483,38 @@ describe('uncaught errors in setImmediate do not affect surrounding tasks or oth ...@@ -1483,6 +1483,38 @@ describe('uncaught errors in setImmediate do not affect surrounding tasks or oth
} }
} }
/**
* Sync errors from fast immediates and patched nextTicks are rethrown from a `queueMicrotask`.
* Whether async context survives into `uncaughtException` for such a rethrow depends on the runtime:
* Node's `queueMicrotask` unwinds its async scope before the error is reported,
* so the context is only visible when `AsyncLocalStorage` is backed by `AsyncContextFrame`
* (the default since Node 24). Probe what a native microtask throw sees on the current runtime.
*/
const getNativeMicrotaskUncaughtContext = async <T>(
Ctx: AsyncLocalStorage<T>,
value: T
): Promise<T | undefined> => {
const result = createPromiseWithResolvers<T | undefined>()
const probeError = new Error('native microtask probe')
using _ = trackUncaughtErrors((error, kind) => {
if (error === probeError && kind === 'uncaughtException') {
result.resolve(Ctx.getStore())
} else {
result.reject(
new Error(`Unexpected ${kind} while probing native microtask`, {
cause: error,
})
)
}
})
Ctx.run(value, () => {
queueMicrotask(() => {
throw probeError
})
})
return await result.promise
}
it('preserves native nextTick error timing without capture', async () => { it('preserves native nextTick error timing without capture', async () => {
const { log, logs } = createLogger() const { log, logs } = createLogger()
const done = createPromiseWithResolvers<void>() const done = createPromiseWithResolvers<void>()
...@@ -1608,6 +1640,10 @@ describe('uncaught errors in setImmediate do not affect surrounding tasks or oth ...@@ -1608,6 +1640,10 @@ describe('uncaught errors in setImmediate do not affect surrounding tasks or oth
const Ctx = new AsyncLocalStorage<string>() const Ctx = new AsyncLocalStorage<string>()
const contextValue = 'hello' const contextValue = 'hello'
const expectedUncaughtContext = await getNativeMicrotaskUncaughtContext(
Ctx,
contextValue
)
let triggeredError: TriggeredUncaught | undefined = undefined let triggeredError: TriggeredUncaught | undefined = undefined
using _ = trackUncaughtErrors((error, kind) => { using _ = trackUncaughtErrors((error, kind) => {
...@@ -1662,9 +1698,9 @@ describe('uncaught errors in setImmediate do not affect surrounding tasks or oth ...@@ -1662,9 +1698,9 @@ describe('uncaught errors in setImmediate do not affect surrounding tasks or oth
'timeout 1 -> immediate 1', 'timeout 1 -> immediate 1',
'timeout 1 -> immediate 1 -> nextTick', // undesirable (too early) but acceptable 'timeout 1 -> immediate 1 -> nextTick', // undesirable (too early) but acceptable
// FIXME: no async context in uncaughtException // FIXME: native immediates keep async context in uncaughtException on all Node versions,
// `uncaughtException - ${contextValue}`, // but we rethrow via `queueMicrotask`, which only keeps it with `AsyncContextFrame` (Node 24+).
`uncaughtException - undefined`, `uncaughtException - ${expectedUncaughtContext}`,
// ====================== // ======================
'timeout 1 -> immediate 2', 'timeout 1 -> immediate 2',
...@@ -1679,6 +1715,10 @@ describe('uncaught errors in setImmediate do not affect surrounding tasks or oth ...@@ -1679,6 +1715,10 @@ describe('uncaught errors in setImmediate do not affect surrounding tasks or oth
const Ctx = new AsyncLocalStorage<string>() const Ctx = new AsyncLocalStorage<string>()
const contextValue = 'hello' const contextValue = 'hello'
const expectedUncaughtContext = await getNativeMicrotaskUncaughtContext(
Ctx,
contextValue
)
let triggeredError: TriggeredUncaught | undefined = undefined let triggeredError: TriggeredUncaught | undefined = undefined
using _ = trackUncaughtErrors((error, kind) => { using _ = trackUncaughtErrors((error, kind) => {
...@@ -1728,9 +1768,9 @@ describe('uncaught errors in setImmediate do not affect surrounding tasks or oth ...@@ -1728,9 +1768,9 @@ describe('uncaught errors in setImmediate do not affect surrounding tasks or oth
'timeout 1 -> immediate 1', 'timeout 1 -> immediate 1',
`timeout 1 -> immediate 1 -> nextTick - ${contextValue}`, `timeout 1 -> immediate 1 -> nextTick - ${contextValue}`,
// FIXME: no async context in uncaughtException // FIXME: native nextTicks keep async context in uncaughtException on all Node versions,
// `uncaughtException - ${contextValue}`, // but we rethrow via `queueMicrotask`, which only keeps it with `AsyncContextFrame` (Node 24+).
`uncaughtException - undefined`, `uncaughtException - ${expectedUncaughtContext}`,
// ====================== // ======================
'timeout 1 -> immediate 2', 'timeout 1 -> immediate 2',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment