core
core copied to clipboard
onPreShutdown lifecycle hook no longer waits for async code to run
Bug description
given the following code:
lifecycle.onPreShutdown(async () => {
console.log('before await')
await new Promise((resolve) => setTimeout(resolve, 1000))
console.log('after await')
})
The second console.log will not print during shutdown.
- Expected behaviour
App won't shutdown before finishing the async parts of the shutdown hooks
I think this works as expected for me
Code:
// Give the go ahead for production to take over again when shutting down
lifecycle.onPreShutdown(async () => {
if (isDevelop) isDevelopmentActive.state = 'off'
const resolved = await Promise.race([
new Promise((resolve) => {
isDevelopmentActive.onUpdate((newValue) => resolve(newValue))
}),
new Promise((resolve) => setTimeout(() => resolve('timeout'), 10_000)),
])
if (resolved !== 'off') {
logger.error(`Unable to verify that production runtime has taken over. Reason: ${resolved}`)
}
})
waits 10 seconds for the timeout
@webbertakken what version of core are you on?
Latest. And I ended up with this script in the end.
https://github.com/Digital-Alchemy-TS/docker-standalone/blob/main/src/core/runtime-precedence.ts
Versions can be seen in package.json in that project
Added test that covers async shutdown specifically -
- Async lifecycle hooks seem to be working as expected
- If an error is thrown during a shutdown hook, then all the events after that will not get run
That 2nd item has an explicit error attached to it going right to console/stderr