Commit 35894d06 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by Niklas Mischkulnig

Fix CI failures when workerThreads is enabled by default

- Add exit/message event listeners for worker threads in worker.ts, mirroring
  the existing child process handlers for error propagation and progress tracking
- Fix export/worker.ts to use parentPort.postMessage() in worker thread mode
  instead of process.send() (which is undefined in worker_threads)
- Update config-experimental-warning tests: workerThreads:true is now the default,
  so tests use workerThreads:false to show a non-default experimental value
- Add workerThreads:false to worker-kill fixture (SIGKILL only works with child processes)
- Add workerThreads:false to prerender-native-module fixture (sqlite3 is not thread-safe)
- Update cpu-profiling test to expect an additional build-static-worker profile
  now that the static worker runs as a thread and exits cleanly (saving its profile)
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent f3395cfc
......@@ -605,8 +605,14 @@ async function exportPage(
return { error: true, duration: Date.now() - start }
}
// Notify the parent process that we processed a page (used by the progress activity indicator)
process.send?.([3, { type: 'activity' }])
// Notify the parent process that we processed a page (used by the progress activity indicator).
// In child process mode, use IPC (process.send). In worker thread mode, use parentPort.
if (process.send) {
process.send([3, { type: 'activity' }])
} else {
(require('worker_threads') as typeof import('worker_threads')).parentPort?.postMessage([3, { type: 'activity' }])
}
// Otherwise we can return the result.
return {
......
import type { ChildProcess } from 'child_process'
import type { Worker as WorkerThread } from 'worker_threads'
import { Worker as JestWorker } from 'next/dist/compiled/jest-worker'
import { Transform } from 'stream'
import {
......@@ -198,6 +199,36 @@ export class Worker {
}
})
}
} else {
for (const worker of ((this._worker as any)._workerPool?._workers ||
[]) as {
_worker?: WorkerThread
}[]) {
worker._worker?.on('exit', (code) => {
if (code && code !== 0 && this._worker) {
logger.error(
`Next.js build worker exited with code: ${code} and signal: null`
)
// if a worker thread doesn't exit gracefully, bubble up the exit code to the parent process
process.exit(code ?? 1)
}
})
// if a worker thread emits a custom message (type 3 = PARENT_MESSAGE_CUSTOM),
// track activity so the parent process can keep track of progress
worker._worker?.on('message', ([type, data]: [number, unknown]) => {
if (
type === 3 && // PARENT_MESSAGE_CUSTOM
data &&
typeof data === 'object' &&
'type' in data &&
(data as any).type === 'activity'
) {
onActivityImpl()
}
})
}
}
let aborted = false
......
......@@ -35,14 +35,17 @@ describe('CPU Profiling - next build', () => {
expect(cpuProfiles.some((f) => f.startsWith('build-main-'))).toBe(true)
if (isTurbopack) {
// Turbopack mode generates: build-main, build-turbopack
expect(cpuProfiles.length).toBe(2)
// Turbopack mode generates: build-main, build-turbopack, build-static-worker
expect(cpuProfiles.length).toBe(3)
expect(cpuProfiles.some((f) => f.startsWith('build-turbopack-'))).toBe(
true
)
expect(
cpuProfiles.some((f) => f.startsWith('build-static-worker-'))
).toBe(true)
} else {
// Webpack mode generates: build-main, build-webpack-client, build-webpack-server, build-webpack-edge-server
expect(cpuProfiles.length).toBe(4)
// Webpack mode generates: build-main, build-webpack-client, build-webpack-server, build-webpack-edge-server, build-static-worker
expect(cpuProfiles.length).toBe(5)
expect(
cpuProfiles.some((f) => f.startsWith('build-webpack-client-'))
).toBe(true)
......@@ -52,6 +55,9 @@ describe('CPU Profiling - next build', () => {
expect(
cpuProfiles.some((f) => f.startsWith('build-webpack-edge-server-'))
).toBe(true)
expect(
cpuProfiles.some((f) => f.startsWith('build-static-worker-'))
).toBe(true)
}
})
})
......@@ -17,6 +17,14 @@ describe('prerender native module', () => {
'data.sqlite': new FileRef(
path.join(__dirname, 'prerender-native-module/data.sqlite')
),
'next.config.js': `
module.exports = {
experimental: {
// sqlite3 native addon is not thread-safe and crashes in worker_threads
workerThreads: false,
},
}
`,
},
dependencies: {
sqlite: '4.0.22',
......
......@@ -88,35 +88,35 @@ describe('Config Experimental Warning', () => {
configFile.write(`
module.exports = {
experimental: {
workerThreads: true
workerThreads: false
}
}
`)
const stdout = await collectStdoutFromDev(appDir)
expect(stdout).toMatch(experimentalHeader)
expect(stdout).toMatch(' ✓ workerThreads')
expect(stdout).toMatch(' ⨯ workerThreads')
})
it('should show warning with config from function with experimental', async () => {
configFile.write(`
module.exports = (phase) => ({
experimental: {
workerThreads: true
workerThreads: false
}
})
`)
const stdout = await collectStdoutFromDev(appDir)
expect(stdout).toMatch(experimentalHeader)
expect(stdout).toMatch(' ✓ workerThreads')
expect(stdout).toMatch(' ⨯ workerThreads')
})
it('should not show warning with default value', async () => {
configFile.write(`
module.exports = (phase) => ({
experimental: {
workerThreads: false,
workerThreads: true,
// We enable this by default in CI
strictRouteTypes: false,
}
......@@ -175,7 +175,7 @@ describe('Config Experimental Warning', () => {
configFile.write(`
module.exports = {
experimental: {
workerThreads: true,
workerThreads: false,
scrollRestoration: true,
}
}
......@@ -183,7 +183,7 @@ describe('Config Experimental Warning', () => {
const stdout = await collectStdoutFromDev(appDir)
expect(stdout).toContain(experimentalHeader)
expect(stdout).toContain(' ✓ workerThreads')
expect(stdout).toContain(' ⨯ workerThreads')
expect(stdout).toContain(' ✓ scrollRestoration')
})
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
......@@ -193,7 +193,6 @@ describe('Config Experimental Warning', () => {
configFile.write(`
module.exports = {
experimental: {
workerThreads: true,
scrollRestoration: true,
parallelServerCompiles: true,
cpus: 2,
......@@ -216,7 +215,7 @@ describe('Config Experimental Warning', () => {
configFile.write(`
module.exports = {
experimental: {
workerThreads: true,
workerThreads: false,
scrollRestoration: true,
parallelServerCompiles: true,
prerenderEarlyExit: false,
......@@ -227,7 +226,7 @@ describe('Config Experimental Warning', () => {
const stdout = await collectStdoutFromBuild(appDir)
expect(stdout).toMatch(experimentalHeader)
expect(stdout).toMatch(' · cpus: 2')
expect(stdout).toMatch(' ✓ workerThreads')
expect(stdout).toMatch(' ⨯ workerThreads')
expect(stdout).toMatch(' ✓ scrollRestoration')
expect(stdout).toMatch(' ⨯ prerenderEarlyExit')
expect(stdout).toMatch(' ✓ parallelServerCompiles')
......
module.exports = {}
module.exports = {
experimental: {
// This fixture sends SIGKILL to the worker process, which only works with
// child processes, not worker threads.
workerThreads: false,
},
}
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