|
| 1 | +import { InvariantError } from '../../shared/lib/invariant-error' |
| 2 | + |
| 3 | +/* |
| 4 | +========================== |
| 5 | +| Background | |
| 6 | +========================== |
| 7 | +
|
| 8 | +Node.js does not guarantee that two timers scheduled back to back will run |
| 9 | +on the same iteration of the event loop: |
| 10 | +
|
| 11 | +```ts |
| 12 | +setTimeout(one, 0) |
| 13 | +setTimeout(two, 0) |
| 14 | +``` |
| 15 | +
|
| 16 | +Internally, each timer is assigned a `_idleStart` property that holds |
| 17 | +an internal libuv timestamp in millisecond resolution. |
| 18 | +This will be used to determine if the timer is already "expired" and should be executed. |
| 19 | +However, even in sync code, it's possible for two timers to get different `_idleStart` values. |
| 20 | +This can cause one of the timers to be executed, and the other to be delayed until the next timer phase. |
| 21 | +
|
| 22 | +The delaying happens [here](https://github.com/nodejs/node/blob/c208ffc66bb9418ff026c4e3fa82e5b4387bd147/lib/internal/timers.js#L556-L564). |
| 23 | +and can be debugged by running node with `NODE_DEBUG=timer`. |
| 24 | +
|
| 25 | +The easiest way to observe it is to run this program in a loop until it exits with status 1: |
| 26 | +
|
| 27 | +``` |
| 28 | +// test.js |
| 29 | +
|
| 30 | +let immediateRan = false |
| 31 | +const t1 = setTimeout(() => { |
| 32 | + console.log('timeout 1') |
| 33 | + setImmediate(() => { |
| 34 | + console.log('immediate 1') |
| 35 | + immediateRan = true |
| 36 | + }) |
| 37 | +}) |
| 38 | +
|
| 39 | +const t2 = setTimeout(() => { |
| 40 | + console.log('timeout 2') |
| 41 | + if (immediateRan) { |
| 42 | + console.log('immediate ran before the second timeout!') |
| 43 | + console.log( |
| 44 | + `t1._idleStart: ${t1._idleStart}, t2_idleStart: ${t2._idleStart}` |
| 45 | + ); |
| 46 | + process.exit(1) |
| 47 | + } |
| 48 | +}) |
| 49 | +``` |
| 50 | +
|
| 51 | +```bash |
| 52 | +#!/usr/bin/env bash |
| 53 | +
|
| 54 | +i=1; |
| 55 | +while true; do |
| 56 | + output="$(NODE_DEBUG=timer node test.js 2>&1)"; |
| 57 | + if [ "$?" -eq 1 ]; then |
| 58 | + echo "failed after $i iterations"; |
| 59 | + echo "$output"; |
| 60 | + break; |
| 61 | + fi; |
| 62 | + i=$((i+1)); |
| 63 | +done |
| 64 | +``` |
| 65 | +
|
| 66 | +If `t2` is deferred to the next iteration of the event loop, |
| 67 | +then the immediate scheduled from inside `t1` will run first. |
| 68 | +When this occurs, `_idleStart` is reliably different between `t1` and `t2`. |
| 69 | +
|
| 70 | +========================== |
| 71 | +| Solution | |
| 72 | +========================== |
| 73 | +
|
| 74 | +We can guarantee that multiple timers (with the same delay, usually `0`) |
| 75 | +run together without any delays by making sure that their `_idleStart`s are the same, |
| 76 | +because that's what's used to determine if a timer should be deferred or not. |
| 77 | +Luckily, this property is currently exposed to userland and mutable, |
| 78 | +so we can patch it. |
| 79 | +
|
| 80 | +Another related trick we could potentially apply is making |
| 81 | +a timer immediately be considered expired by doing `timer._idleStart -= 2`. |
| 82 | +(the value must be more than `1`, the delay that actually gets set for `setTimeout(cb, 0)`). |
| 83 | +This makes node view this timer as "a 1ms timer scheduled 2ms ago", |
| 84 | +meaning that it should definitely run in the next timer phase. |
| 85 | +However, I'm not confident we know all the side effects of doing this, |
| 86 | +so for now, simply ensuring coordination is enough. |
| 87 | +*/ |
| 88 | + |
| 89 | +let shouldAttemptPatching = true |
| 90 | + |
| 91 | +function warnAboutTimers() { |
| 92 | + console.warn( |
| 93 | + "Next.js cannot guarantee that Cache Components will run as expected due to the current runtime's implementation of `setTimeout()`.\nPlease report a github issue here: https://github.com/vercel/next.js/issues/new/" |
| 94 | + ) |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Allows scheduling multiple timers (equivalent to `setTimeout(cb, delayMs)`) |
| 99 | + * that are guaranteed to run in the same iteration of the event loop. |
| 100 | + * |
| 101 | + * @param delayMs - the delay to pass to `setTimeout`. (default: 0) |
| 102 | + * |
| 103 | + * */ |
| 104 | +export function createAtomicTimerGroup(delayMs = 0) { |
| 105 | + if (process.env.NEXT_RUNTIME === 'edge') { |
| 106 | + throw new InvariantError( |
| 107 | + 'createAtomicTimerGroup cannot be called in the edge runtime' |
| 108 | + ) |
| 109 | + } else { |
| 110 | + let isFirstCallback = true |
| 111 | + let firstTimerIdleStart: number | null = null |
| 112 | + let didFirstTimerRun = false |
| 113 | + |
| 114 | + // As a sanity check, we schedule an immediate from the first timeout |
| 115 | + // to check if the execution was interrupted. |
| 116 | + let didImmediateRun = false |
| 117 | + function runFirstCallback(callback: () => void) { |
| 118 | + didFirstTimerRun = true |
| 119 | + if (shouldAttemptPatching) { |
| 120 | + setImmediate(() => { |
| 121 | + didImmediateRun = true |
| 122 | + }) |
| 123 | + } |
| 124 | + return callback() |
| 125 | + } |
| 126 | + |
| 127 | + function runSubsequentCallback(callback: () => void) { |
| 128 | + if (shouldAttemptPatching) { |
| 129 | + if (didImmediateRun) { |
| 130 | + // If the immediate managed to run between the timers, then we're not |
| 131 | + // able to provide the guarantees that we're supposed to |
| 132 | + shouldAttemptPatching = false |
| 133 | + warnAboutTimers() |
| 134 | + } |
| 135 | + } |
| 136 | + return callback() |
| 137 | + } |
| 138 | + |
| 139 | + return function scheduleTimeout(callback: () => void) { |
| 140 | + if (didFirstTimerRun) { |
| 141 | + throw new InvariantError( |
| 142 | + 'Cannot schedule more timers into a group that already executed' |
| 143 | + ) |
| 144 | + } |
| 145 | + |
| 146 | + const timer = setTimeout( |
| 147 | + isFirstCallback ? runFirstCallback : runSubsequentCallback, |
| 148 | + delayMs, |
| 149 | + callback |
| 150 | + ) |
| 151 | + isFirstCallback = false |
| 152 | + |
| 153 | + if (!shouldAttemptPatching) { |
| 154 | + // We already tried patching some timers, and it didn't work. |
| 155 | + // No point trying again. |
| 156 | + return timer |
| 157 | + } |
| 158 | + |
| 159 | + // NodeJS timers have a `_idleStart` property, but it doesn't exist e.g. in Bun. |
| 160 | + // If it's not present, we'll warn and try to continue. |
| 161 | + try { |
| 162 | + if ('_idleStart' in timer && typeof timer._idleStart === 'number') { |
| 163 | + // If this is the first timer that was scheduled, save its `_idleStart`. |
| 164 | + // We'll copy it onto subsequent timers to guarantee that they'll all be |
| 165 | + // considered expired in the same iteration of the event loop |
| 166 | + // and thus will all be executed in the same timer phase. |
| 167 | + if (firstTimerIdleStart === null) { |
| 168 | + firstTimerIdleStart = timer._idleStart |
| 169 | + } else { |
| 170 | + timer._idleStart = firstTimerIdleStart |
| 171 | + } |
| 172 | + } else { |
| 173 | + shouldAttemptPatching = false |
| 174 | + warnAboutTimers() |
| 175 | + } |
| 176 | + } catch (err) { |
| 177 | + // This should never fail in current Node, but it might start failing in the future. |
| 178 | + // We might be okay even without tweaking the timers, so warn and try to continue. |
| 179 | + console.error( |
| 180 | + new InvariantError( |
| 181 | + 'An unexpected error occurred while adjusting `_idleStart` on an atomic timer', |
| 182 | + { cause: err } |
| 183 | + ) |
| 184 | + ) |
| 185 | + shouldAttemptPatching = false |
| 186 | + warnAboutTimers() |
| 187 | + } |
| 188 | + |
| 189 | + return timer |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments