Unverified Commit d735d317 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix static worker restart behavior (#56728)

In [55841](https://github.com/vercel/next.js/pull/55841), this file was reworked to improve type safety and readability, but it changed the behavior of how we were invoking methods on the worker. Specifically, when a restart occurred, this timeout wrapping function was referencing an already ended worker, resulting in a "Farm is ended, no more calls can be done to it" build error.

This PR ensures that we're fetching the method from the current `this._worker` at the time of invocation, not at the time of method creation.

[Slack x-ref](https://vercel.slack.com/archives/C04KC8A53T7/p1697064752635179?thread_ts=1696952142.759769&cid=C04KC8A53T7)
parent f0dd49e4
...@@ -133,9 +133,7 @@ export class Worker<T extends object = object, Args extends any[] = any[]> { ...@@ -133,9 +133,7 @@ export class Worker<T extends object = object, Args extends any[] = any[]> {
} }
const wrapMethodWithTimeout = const wrapMethodWithTimeout =
<M extends (...args: unknown[]) => Promise<unknown> | unknown>( (methodName: keyof T) =>
method: M
) =>
async (...args: Args) => { async (...args: Args) => {
activeTasks++ activeTasks++
...@@ -149,7 +147,8 @@ export class Worker<T extends object = object, Args extends any[] = any[]> { ...@@ -149,7 +147,8 @@ export class Worker<T extends object = object, Args extends any[] = any[]> {
const result = await Promise.race([ const result = await Promise.race([
// Either we'll get the result from the worker, or we'll get the // Either we'll get the result from the worker, or we'll get the
// restart promise to fire. // restart promise to fire.
method(...args), // @ts-expect-error - we're grabbing a dynamic method on the worker
this._worker[methodName](...args),
restartPromise, restartPromise,
]) ])
...@@ -160,7 +159,7 @@ export class Worker<T extends object = object, Args extends any[] = any[]> { ...@@ -160,7 +159,7 @@ export class Worker<T extends object = object, Args extends any[] = any[]> {
} }
// Otherwise, we'll need to restart the worker, and try again. // Otherwise, we'll need to restart the worker, and try again.
if (onRestart) onRestart(method.name, args, ++attempts) if (onRestart) onRestart(methodName.toString(), args, ++attempts)
} }
} finally { } finally {
activeTasks-- activeTasks--
...@@ -174,7 +173,7 @@ export class Worker<T extends object = object, Args extends any[] = any[]> { ...@@ -174,7 +173,7 @@ export class Worker<T extends object = object, Args extends any[] = any[]> {
// @ts-expect-error - we're grabbing a dynamic method on the worker // @ts-expect-error - we're grabbing a dynamic method on the worker
let method = this._worker[name].bind(this._worker) let method = this._worker[name].bind(this._worker)
if (timeout) { if (timeout) {
method = wrapMethodWithTimeout(method) method = wrapMethodWithTimeout(name)
} }
// @ts-expect-error - we're dynamically creating methods // @ts-expect-error - we're dynamically creating methods
......
export async function generateMetadata() {
await new Promise((resolve) => setTimeout(resolve, 11000))
return {
title: 'Test',
}
}
export default function Page() {
return <div>Hello World</div>
}
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <div>Root Page</div>
}
module.exports = {
staticPageGenerationTimeout: 10,
}
import { nextBuild } from 'next-test-utils'
describe('worker-restart', () => {
it('should properly exhaust all restart attempts and not fail with any worker errors', async () => {
const { stdout, stderr } = await nextBuild(__dirname, [], {
stdout: true,
stderr: true,
})
const output = stdout + stderr
expect(output).toContain(
'Restarted static page generation for /bad-page because it took more than 10 seconds'
)
expect(output).toContain(
'Static page generation for /bad-page is still timing out after 3 attempts'
)
expect(output).not.toContain(
'Error: Farm is ended, no more calls can be done to it'
)
})
})
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