chore: when dev server restarts, ensure exiting the parent process kills child process (#8703)

This commit is contained in:
Alessio Gravili
2024-10-14 14:02:26 -06:00
committed by GitHub
parent e3957d746b
commit f1ebf56691
2 changed files with 25 additions and 10 deletions

View File

@@ -9,7 +9,7 @@ import { loadEnv } from 'payload/node'
import { getNextRootDir } from './helpers/getNextRootDir.js' import { getNextRootDir } from './helpers/getNextRootDir.js'
import { runInit } from './runInit.js' import { runInit } from './runInit.js'
import { safelyRunScriptFunction } from './safelyRunScript.js' import { child, safelyRunScriptFunction } from './safelyRunScript.js'
import { createTestHooks } from './testHooks.js' import { createTestHooks } from './testHooks.js'
const prod = process.argv.includes('--prod') const prod = process.argv.includes('--prod')
@@ -59,6 +59,15 @@ await nextDev({ port }, 'default', rootDir)
void fetch(`http://localhost:${port}${adminRoute}`) void fetch(`http://localhost:${port}${adminRoute}`)
// This ensures that the next-server process is killed when this process is killed and doesn't linger around. // This ensures that the next-server process is killed when this process is killed and doesn't linger around.
process.on('SIGINT', function () { process.on('SIGINT', () => {
if (child) {
child.kill('SIGINT')
}
process.exit(0) process.exit(0)
}) })
process.on('SIGTERM', () => {
if (child) {
child.kill('SIGINT')
}
process.exit(0) // Exit the parent process
})

View File

@@ -1,6 +1,8 @@
import { spawn } from 'child_process' import { spawn } from 'child_process'
import path from 'path' import path from 'path'
export let child
/** /**
* Sometimes, running certain functions in certain scripts from the command line will cause the script to be terminated * Sometimes, running certain functions in certain scripts from the command line will cause the script to be terminated
* with a "Detected unsettled top-level await" error. This often happens if that function imports the payload config. * with a "Detected unsettled top-level await" error. This often happens if that function imports the payload config.
@@ -29,7 +31,7 @@ export async function safelyRunScriptFunction(
}) })
} }
function restartProcess(reason: string): never { function restartProcess(reason: string) {
console.warn(`Restarting process: ${reason}`) console.warn(`Restarting process: ${reason}`)
// Get the path to the current script // Get the path to the current script
@@ -37,14 +39,18 @@ function restartProcess(reason: string): never {
const absoluteScriptPath = path.resolve(scriptPath) const absoluteScriptPath = path.resolve(scriptPath)
// Spawn a new process // Spawn a new process
const child = spawn('tsx', [absoluteScriptPath, ...process.argv.slice(2)], { child = spawn('tsx', [absoluteScriptPath, ...process.argv.slice(2)], {
stdio: 'inherit', stdio: 'inherit',
detached: true, // detached: true,
}) })
// Unref the child process so the parent can exit // Setup signal handlers to ensure child process exits correctly
child.unref() process.on('SIGINT', () => {
child.kill('SIGINT') // Forward SIGINT to child process
// Exit the current process process.exit(0) // Exit the parent process
process.exit(0) })
process.on('SIGTERM', () => {
child.kill('SIGTERM') // Forward SIGTERM to child process
process.exit(0) // Exit the parent process
})
} }