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

The client keeps node alive if end() is called before the connection is established #1033

Closed
kkeri opened this issue Jan 17, 2020 · 5 comments
Labels

Comments

@kkeri
Copy link

kkeri commented Jan 17, 2020

If I set up an MQTT connection and for some reason, I want to exit the program during startup, calling client.end() is not enough to quit the event loop. The process hangs up for some 90 seconds before exiting. Although if I wait a few seconds to allow the connection to be established and only then I call client.end(), node exits as expected. Here is a sample that produces the issue.

const mqtt = require('mqtt')

const client = mqtt.connect('tcp://localhost')

client.on('connect', () => {
  console.log(`connected to broker`)
})

client.on('close', () => {
  console.log(`connection closed`)
})

const delay = 0 // but try 5000 instead

setTimeout(() => client.end(), delay)
@kkeri kkeri changed the title The client keeps node alive if end() is called before the connection established The client keeps node alive if end() is called before the connection is established Jan 17, 2020
@jdiamond
Copy link
Contributor

Thanks for the script to reproduce.

I changed delay to 5000, saw it log the connect message, then the closed message 5 seconds later, and
then the process immediately exited.

I'm running Node.js v12.14.0 on macOS. What about you? Also, what broker are you running on localhost? I tested with test.mosquitto.org.

@jdiamond
Copy link
Contributor

Based on your edits, can confirm that setting delay to 0 does hang for 90 seconds.

@kkeri
Copy link
Author

kkeri commented Jan 17, 2020

Thanks for the confirmation. Btw. I'm on Windows 10, using node v10.15.1 and mosquitto version 1.5.8.

@github-actions
Copy link

This is an automated message to let you know that this issue has
gone 365 days without any activity. In order to ensure that we work
on issues that still matter, this issue will be closed in 14 days.

If this issue is still important, you can simply comment with a
"bump" to keep it open.

Thank you for your contribution.

@github-actions github-actions bot added the stale label Oct 15, 2022
@github-actions
Copy link

This issue was automatically closed due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants