Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

fix: destroy sockets on close #204

Merged
merged 3 commits into from
Aug 31, 2022
Merged

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Aug 31, 2022

When closing a MultiaddrConnection We call .end on the underlying TCP socket and wait for the close event, but calling .end only closes the writable end of the socket, not the whole thing.

To close both ends we either need to wait for the remote to close their writable end or we need to .destroy our socket. If we call .destroy all data is lost and no more I/O occurs.

The change here is to call .end then check to see if we have any outgoing writes, if we do, wait for the drain event which means the outgoing data has been sent, then call .destroy, otherwise call .destroy immediately.

At the same time use a timer to call .destroy if the drain event never arrives.

It also set up the timeout event for the socket to allow closing the socket after a period of inactivity.

Three new constructor options are added to control the behaviour:

  • inboundSocketInactivityTimeout inbound sockets will be closed after this many ms of not sending/receiving data (default 30s)
  • outboundSocketInactivityTimeout outbound sockets will be closed after this many ms of not sending/receiving data (default 30s)
  • socketCloseTimeout how long to wait for the drain event (default 2s)

Fixes #201

We call `.end` on a socket and wait for the `close` event, but calling `.end` only closes the writable end of the socket.

To close both ends we either need to wait for the remote to close their writable end or we need to `.destroy` our socket.  If we call `.destroy` all data is lost and no more I/O occurs.

The change here is to call `.end` then check to see if we have any outgoing writes, if we do, wait for the `drain` event which means the outgoing data has been sent, then call `.destroy`, otherwise call `.destroy` immediately.

At the same time use a timer to call `.destroy` if the `drain` event never arrives.

It also set up the `timeout` event for the socket to allow closing the socket after a period of inactivity.

Three new constructor options are added to control the behvaiour:

- `inboundSocketInactivityTimeout` the socket will be closed after this many ms of not sending/recieving data (default 30s)
- `outboundSocketInactivityTimeout` the socket will be closed after this many ms of not sending/recieving data (default 30s)
- `socketCloseTimeout` how long to wait for the `drain` event (default 2s)

Fixes #201
Copy link
Member

@wemeetagain wemeetagain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wemeetagain wemeetagain merged commit e8b8f2e into master Aug 31, 2022
@wemeetagain wemeetagain deleted the fix/destroy-sockets-after-timeout branch August 31, 2022 14:37
github-actions bot pushed a commit that referenced this pull request Aug 31, 2022
## [3.0.5](v3.0.4...v3.0.5) (2022-08-31)

### Bug Fixes

* destroy sockets on close ([#204](#204)) ([e8b8f2e](e8b8f2e)), closes [#201](#201)
@github-actions
Copy link

🎉 This PR is included in version 3.0.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

Sockets leak
3 participants