Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gyszalai opened this issue Apr 28, 2021 · 2 comments · Fixed by #143
Closed

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

gyszalai opened this issue Apr 28, 2021 · 2 comments · Fixed by #143

Comments

@gyszalai
Copy link
Contributor

🐛 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
@mcollina
Copy link
Member

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

gyszalai pushed a commit to gyszalai/avvio that referenced this issue May 4, 2021
@gyszalai
Copy link
Contributor Author

gyszalai commented May 4, 2021

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

mcollina pushed a commit that referenced this issue May 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants