timgit / pg-boss

Queueing jobs in Postgres from Node.js like a boss
MIT License
2.13k stars 158 forks source link

pg-boss calls executeSql after `stop` finalizes #223

Closed gavacho closed 3 years ago

gavacho commented 3 years ago

pg-boss is throwing an error when our app shuts down:

 "UnexpectedStateError: Connection pool shutdown has been already initiated. Cannot create a new connection.
    at Object.createConnection (/app/node_modules/slonik/src/factories/createConnection.ts:61:11)
    at Object.query (/app/node_modules/slonik/src/binders/bindPool.ts:30:14)
    at Object.executeSql (/app/src/App.ts:109:45)
    at Timekeeper.setCronTime (/app/node_modules/pg-boss/src/timekeeper.js:193:19)
    at Timekeeper.onCron (/app/node_modules/pg-boss/src/timekeeper.js:133:18)"

Based on our logs, we know that await boss.stop() has already returned when this exception is thrown. It looks like Timekeeper.onCron is executing after await boss.stop()

timgit commented 3 years ago

How many schedules do you have in your pgboss.schedule table? I see how there could be a race condition here during onCron() as it's running. The only check for isStopped is at the very beginning and I don't have it checking line by line.

gavacho commented 3 years ago

select count(1) from pgboss.schedule returned 7