core icon indicating copy to clipboard operation
core copied to clipboard

onPreShutdown lifecycle hook no longer waits for async code to run

Open chrisdrackett opened this issue 1 year ago • 3 comments

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

chrisdrackett avatar Aug 08 '24 18:08 chrisdrackett

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

image

webbertakken avatar Aug 11 '24 16:08 webbertakken

@webbertakken what version of core are you on?

chrisdrackett avatar Aug 13 '24 00:08 chrisdrackett

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

webbertakken avatar Aug 13 '24 01:08 webbertakken

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

zoe-codez avatar Sep 29 '24 01:09 zoe-codez