Skip to content

Commit 58bdc0b

Browse files
authored
[Flight] Ignore bound-anonymous-fn resources as they're not considered I/O (#34911)
When you create a snapshot from an AsyncLocalStorage in Node.js, that creates a new bound AsyncResource which everything runs inside of. https://github.com/nodejs/node/blob/3437e1c4bd529e51d96ea581b6435bbeb77ef524/lib/internal/async_local_storage/async_hooks.js#L61-L67 This resource is itself tracked by our async debug tracking as I/O. We can't really distinguish these in general from other AsyncResources which are I/O. However, by default they're given the name `"bound-anonymous-fn"` if you pass it an anonymous function or in the case of a snapshot, that's built-in: https://github.com/nodejs/node/blob/3437e1c4bd529e51d96ea581b6435bbeb77ef524/lib/async_hooks.js#L262-L263 We can at least assume that these are non-I/O. If you want to ensure that a bound resource is not considered I/O, you can ensure your function isn't assigned a name or give it this explicit name. The other issue here is that, the sequencing here is that we track the callsite of the `.snapshot()` or `.bind()` call as the trigger. So if that was outside of render for example, then it would be considered non-I/O. However, this might miss stuff if you resolve promises inside the `.run()` of the snapshot if the `.run()` call itself was spawned by I/O which should be tracked. Time will tell if those patterns appear. However, in cases like nested renders (e.g. Next.js's "use cache") then restoring it as if it was outside the parent render is what you do want.
1 parent bf11d2f commit 58bdc0b

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

packages/react-server/src/ReactFlightServerConfigDebugNode.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,28 @@ export function initAsyncDebugInfo(): void {
142142
}: UnresolvedPromiseNode);
143143
}
144144
} else if (
145-
type !== 'Microtask' &&
146-
type !== 'TickObject' &&
147-
type !== 'Immediate'
145+
// bound-anonymous-fn is the default name for snapshots and .bind() without a name.
146+
// This isn't I/O by itself but likely just a continuation. If the bound function
147+
// has a name, we might treat it as I/O but we can't tell the difference.
148+
type === 'bound-anonymous-fn' ||
149+
// queueMicroTask, process.nextTick and setImmediate aren't considered new I/O
150+
// for our purposes but just continuation of existing I/O.
151+
type === 'Microtask' ||
152+
type === 'TickObject' ||
153+
type === 'Immediate'
148154
) {
155+
// Treat the trigger as the node to carry along the sequence.
156+
// For "bound-anonymous-fn" this will be the callsite of the .bind() which may not
157+
// be the best if the callsite of the .run() call is within I/O which should be
158+
// tracked. It might be better to track the execution context of "before()" as the
159+
// execution context for anything spawned from within the run(). Basically as if
160+
// it wasn't an AsyncResource at all.
161+
if (trigger === undefined) {
162+
return;
163+
}
164+
node = trigger;
165+
} else {
166+
// New I/O
149167
if (trigger === undefined) {
150168
// We have begun a new I/O sequence.
151169
const owner = resolveOwner();
@@ -181,13 +199,6 @@ export function initAsyncDebugInfo(): void {
181199
// Otherwise, this is just a continuation of the same I/O sequence.
182200
node = trigger;
183201
}
184-
} else {
185-
// Ignore nextTick and microtasks as they're not considered I/O operations.
186-
// we just treat the trigger as the node to carry along the sequence.
187-
if (trigger === undefined) {
188-
return;
189-
}
190-
node = trigger;
191202
}
192203
pendingOperations.set(asyncId, node);
193204
},

0 commit comments

Comments
 (0)