fastify/avvio

Awaiting on app.close() resolves before registered onClose functions resolved

gyszalai opened this issue ยท 2 comments

๐Ÿ› Bug Report

Awaiting on app.close() resolves before async onClose handlers are resolved

To Reproduce

Steps to reproduce the behavior:

const avvio = require('avvio')
const server = {}
const app = avvio(server, { autostart: false })
async function start() {
  await server.use(async instance => {
    console.log('plugin 1 starting')
    await new Promise(resolve => setTimeout(resolve, 1000))
    instance.onClose(async () => {
      console.log('plugin 1 stopping')
      await new Promise(resolve => setTimeout(resolve, 2000))
      console.log('plugin 1 stopped')
    })
    console.log('plugin 1 started')
  }, {})

  await server.ready()
  console.log('app started')
}

start()

setTimeout(async () => {
  console.log('app closing')
  await app.close()
  console.log('app closed')
}, 5000)

Expected behavior

The await app.close() call should not resolve until the registered plugin's onClose function is resolved

The output is:

plugin 1 starting
plugin 1 started
app started
app closing
plugin 1 stopping
app closed
plugin 1 stopped

whereas the expected output is:

plugin 1 starting
plugin 1 started
app started
app closing
plugin 1 stopping
plugin 1 stopped
app closed

Your Environment

  • node version: 14.16.0
  • os: Mac

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@mcollina sorry for the late reply, I think I fixed this issue. Please review my PR. thanks!