Skip to content

fix: capture promisified setImmediate separately#88346

Merged
lubieowoce merged 1 commit intovercel:canaryfrom
jgeschwendt:fast-set-immediate-fix
Jan 16, 2026
Merged

fix: capture promisified setImmediate separately#88346
lubieowoce merged 1 commit intovercel:canaryfrom
jgeschwendt:fast-set-immediate-fix

Conversation

@jgeschwendt
Copy link
Contributor

@jgeschwendt jgeschwendt commented Jan 9, 2026

What?

Capture setImmediate[promisify.custom] at module initialization instead of lazily accessing it when needed.

    RangeError: Maximum call stack size exceeded

      at patchedSetImmediatePromise (node_modules/.pnpm/next@16.1.1_@babel+core@7.28.5_react-dom@19.2.3_react@19.2.3__react@19.2.3/node_modules/next/src/server/node-environment-extensions/fast-set-immediate.external.ts:626:27)
      at originalPromisify (node_modules/.pnpm/next@16.1.1_@babel+core@7.28.5_react-dom@19.2.3_react@19.2.3__react@19.2.3/node_modules/next/src/server/node-environment-extensions/fast-set-immediate.external.ts:627:12)

This was discovered upgrading from 16.0.10 to 16.1.0

Why?

When util.promisify(setImmediate) is called and currentExecution === null, the code was accessing originalSetImmediate[promisify.custom] at call time. However, promisify.custom can get lost or return undefined after the module patches setImmediate, causing promisify(setImmediate) to fail. Specifically, when used with jest.

Reproduction: https://github.com/jgeschwendt/nextjs-setimmediate-promisify-repro

How?

Move the capture of originalSetImmediate[promisify.custom] to module initialization time (alongside originalSetImmediate, originalClearImmediate, and originalNextTick), ensuring the original promisified implementation is preserved before any patching occurs.

@nextjs-bot
Copy link
Collaborator

nextjs-bot commented Jan 9, 2026

Allow CI Workflow Run

  • approve CI run for commit: 4b8ae56

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@vercel vercel deleted a comment from nextjs-bot Jan 10, 2026
@lubieowoce lubieowoce force-pushed the fast-set-immediate-fix branch from 4b8ae56 to 2be5e32 Compare January 16, 2026 20:27
@lubieowoce lubieowoce changed the title fix: fast setImmediate implementation fix: capture promisified setImmediate separately Jan 16, 2026
@lubieowoce
Copy link
Member

huh, how weird. nice find, thanks!

@lubieowoce lubieowoce merged commit 5a12867 into vercel:canary Jan 16, 2026
149 checks passed
lubieowoce added a commit that referenced this pull request Jan 17, 2026
…#88685)

Follow up to #88346. Looks like the change there crashes in edge because
`globalThis.setImmediate` isn't defined, so we check before accessing.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 31, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants