-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Open
Description
This code (the way sendPacket handles writeToStream return values) is built on a number of false assumptions:
- It assumes that
writeToStreamwill only returnfalseif the packet was queued and is waiting to send. This is wrong.writeToStreamcan also fail if packet validation fails (inmqtt-packet/writeToStream.js) If this happens,sendPacketwill wait for the stream to drain, then call the callback with no error. - If the stream is full, and
writeToStreamreturns false, it waits for all packets to send before calling any of their callbacks. Once all packets are sent, thedrainevent fires, and all callbacks get called at once. If the sending pattern prevents the stream from draining completely, then the callbacks will never be called (or they might be called long after the packet was actually sent).
3, This assumes that there can only be 1000 packets waiting to send. After that, the maxListeners on thedrainevent overflows. I'm not sure how well the library handles this error (Does it raise an exception? If so, who catches it?)
A better fix would be to pass the callback into the stream's write method so it gets called when the packet is actually sent. At least this is how TLSSocket.write behaves. I don't know if this applies to all of the stream implementations that mqtt.js supports. This would require an update to the mqtt-packet library to support an additional callback parameter to the writeToStream function.
Plus, packetsend is emitted before the packet is actually sent (if the packet is ever actually sent). Is that what we want?
Originally posted by @BertKleewein in #1401 (comment)
Metadata
Metadata
Assignees
Labels
No labels