holepunchto / hyperdht

The DHT powering Hyperswarm
https://docs.holepunch.to
MIT License
323 stars 46 forks source link

Bugfix: don't create an announcer if closing #153

Closed HDegroote closed 10 months ago

HDegroote commented 10 months ago

The race condition triggered when server.listen() created the announcer https://github.com/holepunchto/hyperdht/blob/75059f561f633ab7f7cde214a6f7ed309f503bcb/lib/server.js#L111-L116

after server.close() started executing but before await this.dht.bind()resolved, because then this._listening is not yet true, andclose() early returns if no listener is found: https://github.com/holepunchto/hyperdht/blob/75059f561f633ab7f7cde214a6f7ed309f503bcb/lib/server.js#L83-L86

It's a catastrophic bug when it triggers, because an endless loop floods the CPU in Announcer._background(): https://github.com/holepunchto/hyperdht/blob/75059f561f633ab7f7cde214a6f7ed309f503bcb/lib/announcer.js#L96-L97 It will continuously throw NODE_DESTROYED errors which are caught and ignored