fix(server): fix a potential deadlock when requesting a tarball worker (#7041)

This commit is contained in:
Brandon Cheng
2023-09-05 10:32:47 -04:00
committed by GitHub
parent 4a1a9431dc
commit 548768e092
3 changed files with 18 additions and 2 deletions

View File

@@ -0,0 +1,7 @@
---
"@pnpm/plugin-commands-server": patch
"@pnpm/server": patch
pnpm: patch
---
Fix a bug causing the pnpm server to hang if a tarball worker was requested while another worker was exiting.

View File

@@ -64,7 +64,7 @@ export async function start (
}
throw new PnpmError('SERVER_MANIFEST_LOCKED', `Canceling startup of server (pid ${process.pid}) because another process got exclusive access to server.json`)
}
let server: null | { close: () => Promise<void> } = null
let server: null | ReturnType<typeof createServer> = null
onExit(() => {
if (server !== null) {
// Note that server.close returns a Promise, but we cannot wait for it because we may be
@@ -118,6 +118,11 @@ export async function start (
// Set fd to null so we only attempt to close it once.
fd = null
await close(fdForClose)
// Intentionally avoid returning control back to the caller until the server
// exits. This defers cleanup operations that should not run before the server
// finishes.
await server.waitForClose
}
async function getServerOptions (

View File

@@ -166,7 +166,11 @@ export function createServer (
listener = server.listen(opts.port, opts.hostname)
}
return { close }
const waitForClose = new Promise<void>((resolve) => listener.once('close', () => {
resolve()
}))
return { close, waitForClose }
async function close () {
listener.close()