Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"bugs": "https://github.com/prisma/prisma/issues",
"devDependencies": {
"@types/jest": "29.4.0",
"@types/node": "12.20.55",
"@types/node": "14.18.42",
"esbuild": "0.15.13",
"jest": "29.4.3",
"jest-junit": "15.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/engine-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "16.18.23",
"@types/node": "14.18.42",
"esbuild": "0.15.13",
"jest": "29.4.3",
"jest-junit": "15.0.0",
Expand Down
47 changes: 31 additions & 16 deletions packages/engine-core/src/library/ExitHooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Debug from '@prisma/debug'
import os from 'os'

export type BeforeExitListener = () => Promise<void> | void

Expand All @@ -15,11 +16,11 @@ export class ExitHooks {
return
}

this.installHook('beforeExit')
this.installHook('exit')
this.installHook('SIGINT', true)
this.installHook('SIGUSR2', true)
this.installHook('SIGTERM', true)
this.installExitEventHook('beforeExit')
this.installExitEventHook('exit')
this.installExitSignalHook('SIGINT')
this.installExitSignalHook('SIGUSR2')
this.installExitSignalHook('SIGTERM')
this.areHooksInstalled = true
}

Expand Down Expand Up @@ -48,20 +49,34 @@ export class ExitHooks {
return this.idToListenerMap.get(id)
}

private installHook(event: string, shouldExit = false) {
process.once(event, async (code) => {
debug(`exit event received: ${event}`)
for (const listener of this.idToListenerMap.values()) {
await listener()
}
private installExitEventHook(event: 'beforeExit' | 'exit') {
// Note: TypeScript isn't able to narrow type arguments on overloaded functions.
process.once(event as any, this.exitLikeHook)
}

this.idToListenerMap.clear()
private installExitSignalHook(signal: NodeJS.Signals) {
process.once(signal, async (signal) => {
await this.exitLikeHook(signal)
const isSomeoneStillListening = process.listenerCount(signal) > 0

// only exit, if only we are listening
// if there is another listener, that other listener is responsible
if (shouldExit && process.listenerCount(event) === 0) {
process.exit(code)
// Only exit when there are no listeners left for this signal.
// If there is another listener, that other listener is responsible for exiting.
if (isSomeoneStillListening) {
return
}

// the usual way to exit with a signal is to add 128 to the signal number
const exitCode = os.constants.signals[signal] + 128
Comment on lines +68 to +69

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

People might rely on the old exitCode, not sure if we want to do this change in a standard release?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering this is a direct consequence of Node.js changing its behavior, that's not up to us.
Afaik, as long as we don't turn a 0 exit code into another number, everything's fine

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am with Alberto on this, right now, you applications exit code changes to 0 if you set our beforeExit hook, this change would restore them to what they are by default.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this since it was always 0 until this PR.
Also, we would at least need a test for this? It's an important bit that is still untested based on the CI run here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not always 0. We set our signal handlers only when you add beforeExit event listener. If you don't do it, exit code will be default one, 128 + signal.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which means it was always 0 for people using the beforeExit event listener 😄

process.exit(exitCode)
})
}

private exitLikeHook = async (event: 'beforeExit' | 'exit' | NodeJS.Signals) => {
debug(`exit event received: ${event}`)
for (const listener of this.idToListenerMap.values()) {
await listener()
}

this.idToListenerMap.clear()
}
}
2 changes: 1 addition & 1 deletion packages/engines/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "16.18.23",
"@types/node": "14.18.42",
"execa": "5.1.1",
"jest": "29.4.3",
"typescript": "4.9.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/fetch-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "16.18.23",
"@types/node": "14.18.42",
"@types/node-fetch": "2.6.3",
"@types/progress": "2.0.5",
"del": "6.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/generator-helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "12.20.55",
"@types/node": "14.18.42",
"esbuild": "0.15.13",
"jest": "29.4.3",
"jest-junit": "15.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/get-platform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "16.18.23",
"@types/node": "14.18.42",
"jest": "29.4.3",
"jest-junit": "15.0.0",
"typescript": "4.9.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/instrumentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"bugs": "https://github.com/prisma/prisma/issues",
"devDependencies": {
"@swc/core": "1.3.32",
"@types/node": "16.18.23",
"@types/node": "14.18.42",
"typescript": "4.9.5",
"jest": "29.4.3",
"jest-junit": "15.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/mssql": "8.1.2",
"@types/node": "12.20.55",
"@types/node": "14.18.42",
"@types/pg": "8.6.6",
"@types/sqlite3": "3.1.8",
"decimal.js": "10.4.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "12.20.55",
"@types/node": "14.18.42",
"@types/pg": "8.6.6",
"@types/prompts": "2.4.4",
"@types/sqlite3": "3.1.8",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
},
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
},
"prisma": {
"seed": "node prisma/seed.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
},
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
},
"prisma": {
"seed": "./prisma/seed.sh"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"type": "module",
"devDependencies": {
"@types/node": "14.14.21",
"@types/node": "14.18.42",
"ts-node": "10",
"typescript": "4.7.4"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
},
"prisma": {
"seed": "ts-node prisma/seed.ts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
}
}
Loading