Skip to content

Commit

Permalink
fix(engine-core): MaxListenersExceededWarning and adds too many engin…
Browse files Browse the repository at this point in the history
…es warning (#8712)

* fix(engine-core): MaxListenersExceededWarning and adds warning for too many engines

* test too many engines with node-api
  • Loading branch information
williamluke4 committed Aug 12, 2021
1 parent 7282911 commit a674c38
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 88 deletions.
@@ -1,10 +1,6 @@
import {
ClientEngineType,
getClientEngineType,
} from '../../../../runtime/utils/getClientEngineType'
import { getTestClient } from '../../../../utils/getTestClient'

test('raw-transaction: queryRaw', async () => {
test('too-many-engines warning', async () => {
const PrismaClient = await getTestClient()
const oldConsoleWarn = console.warn
const warnings: any[] = []
Expand All @@ -22,16 +18,12 @@ test('raw-transaction: queryRaw', async () => {
for (const client of clients) {
client.$disconnect()
}
if (getClientEngineType() === ClientEngineType.Library) {
// TODO Should this really not be implemented? https://github.com/prisma/prisma/issues/7814
expect(warnings).toMatchInlineSnapshot(`Array []`)
} else {
expect(warnings).toMatchInlineSnapshot(`
Array [
warn(prisma-client) Already 10 Prisma Clients are actively running.,
]
`)
}

expect(warnings).toMatchInlineSnapshot(`
Array [
warn(prisma-client) Already 10 Prisma Clients are actively running.,
]
`)

console.warn = oldConsoleWarn
})
46 changes: 34 additions & 12 deletions packages/engine-core/src/library/LibraryEngine.ts
Expand Up @@ -52,6 +52,8 @@ function isPanicEvent(event: QueryEngineEvent): event is QueryEnginePanicEvent {
}

const knownPlatforms: Platform[] = [...platforms, 'native']
const engines: LibraryEngine[] = []

export class LibraryEngine extends Engine {
private engine?: QueryEngineInstance
private libraryInstantiationPromise?: Promise<void>
Expand Down Expand Up @@ -98,9 +100,23 @@ export class LibraryEngine extends Engine {
// Debug.enable('*')
}
this.libraryInstantiationPromise = this.instantiateLibrary()
initHooks(this)
}

initHooks()
engines.push(this)
this.checkForTooManyEngines()
}
private checkForTooManyEngines() {
if (engines.length >= 10) {
const runningEngines = engines.filter((e) => e.engine)
if (runningEngines.length === 10) {
console.warn(
`${chalk.yellow(
'warn(prisma-client)',
)} Already 10 Prisma Clients are actively running.`,
)
}
}
}
async transaction(action: 'start', options?: Tx.Options): Promise<Tx.Info>
async transaction(action: 'commit', info: Tx.Info): Promise<undefined>
async transaction(action: 'rollback', info: Tx.Info): Promise<undefined>
Expand Down Expand Up @@ -679,23 +695,29 @@ Read more about deploying Prisma Client: https://pris.ly/d/client-generator`
}
}

function hookProcess(engine: LibraryEngine, handler: string, exit = false) {
function hookProcess(handler: string, exit = false) {
process.once(handler as any, async () => {
debug(`hookProcess received: ${handler}`)
await engine.runBeforeExit()
for (const engine of engines) {
await engine.runBeforeExit()
}
engines.splice(0, engines.length)
// only exit, if only we are listening
// if there is another listener, that other listener is responsible
if (exit && process.listenerCount(handler) === 0) {
process.exit()
}
})
}

function initHooks(engine: LibraryEngine) {
hookProcess(engine, 'beforeExit')
hookProcess(engine, 'exit')
hookProcess(engine, 'SIGINT', true)
hookProcess(engine, 'SIGUSR1', true)
hookProcess(engine, 'SIGUSR2', true)
hookProcess(engine, 'SIGTERM', true)
let hooksInitialized = false
function initHooks() {
if (!hooksInitialized) {
hookProcess('beforeExit')
hookProcess('exit')
hookProcess('SIGINT', true)
hookProcess('SIGUSR1', true)
hookProcess('SIGUSR2', true)
hookProcess('SIGTERM', true)
hooksInitialized = true
}
}
84 changes: 23 additions & 61 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a674c38

Please sign in to comment.