Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/next/src/server/web/sandbox/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getModuleContext } from './context'
import { clearAllModuleContexts, getModuleContext } from './context'
import { validateURL } from '../utils'

jest.mock('../utils', () => ({
Expand Down Expand Up @@ -69,4 +69,22 @@ describe('Next.js sandbox Request constructor', () => {
'Please use only absolute URLs'
)
})

it('should release a timeout after its callback runs', async () => {
const clearTimeoutSpy = jest.spyOn(globalThis, 'clearTimeout')

try {
await new Promise<void>((resolve) => {
moduleContext.runtime.context.setTimeout(resolve, 0)
})
clearTimeoutSpy.mockClear()

await clearAllModuleContexts()

expect(clearTimeoutSpy).not.toHaveBeenCalled()
} finally {
clearTimeoutSpy.mockRestore()
await clearAllModuleContexts()
}
})
})
6 changes: 3 additions & 3 deletions packages/next/src/server/web/sandbox/resource-managers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ function webSetTimeoutPolyfill<TArgs extends any[]>(
// a `setTimeout` whose Timeout was converted to a primitive will leak.
// See: https://github.com/nodejs/node/issues/53335
// We can work around this by explicitly calling `clearTimeout` after the callback runs.
clearTimeout(timeout)
timeoutsManager.remove(timeout)
}
}
const timeout = setTimeout(wrappedCallback, ms)
return timeout[Symbol.toPrimitive]()
const timeout = setTimeout(wrappedCallback, ms)[Symbol.toPrimitive]()
return timeout
}

export const intervalsManager = new IntervalsManager()
Expand Down