Closed dpatti closed 6 years ago
Thank you for the report! I don't believe there are any tests for destroying sockets, so it might be worthwhile indeed to look into this. I wonder how this will behave in Node v4 and others these days.
I'm glad to report this issue has resolved itself over the past years. I did make a change some time ago to fire the connection event in the next tick. Perhaps that fixed it. I finally added a destroy-test to ensure it stays that way: https://github.com/moll/node-mitm/commit/cee7fb2419c417f41a73fc864f1b8ca46d0caba7. :)
I don't know how many people actually run into this, but this was an issue for us when we were upgrading from io.js v2 to v3. Here's the minimal repro:
We were using this pattern in our tests to simulate a network error. The results of the script on my machine:
I did some tracing, and it seems to happen because
socket.destroy()
setssocket._handle
tonull
(and always has), butsocket._handle._externalStream
is used inHttp._connectionListener
as of this commit: https://github.com/nodejs/node/commit/1bc446863f9b44dfd40e8c49f489180d53cce80c, which did not land until io.js v3.3.0.An easy workaround for us is to just destroy the socket on the next tick. I don't know if this would qualify as something you'd want to fix.
As a bonus, this does not crash:
because
Http._connectionListener
fires on theconnection
event, which the above handler is registered after.