Skip to content

Add defensive connection closure. #328

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

Merged
merged 1 commit into from
Jan 19, 2021

Conversation

Lukasa
Copy link
Collaborator

@Lukasa Lukasa commented Jan 19, 2021

Motivation:

Currently when either we or the server send Connection: close, we
correctly do not return that connection to the pool. However, we rely on
the server actually performing the connection closure: we never call
close() ourselves. This is unnecessarily optimistic: a server may
absolutely fail to close this connection. To protect our own file
descriptors, we should make sure that any connection we do not return
the pool is closed.

Modifications:

If we think a connection is closing when we release it, we now call
close() on it defensively.

Result:

We no longer leak connections when the server fails to close them.

Fixes #324.

@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Jan 19, 2021
@Lukasa Lukasa requested review from artemredkin and weissi January 19, 2021 11:28
@Lukasa Lukasa force-pushed the cb-leaking-connections-is-bad branch 3 times, most recently from 07b6889 to 459efe1 Compare January 19, 2021 11:48
@Lukasa Lukasa force-pushed the cb-leaking-connections-is-bad branch 2 times, most recently from 63f6858 to 13d82d1 Compare January 19, 2021 12:27
Copy link
Contributor

@weissi weissi left a comment

Choose a reason for hiding this comment

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

Looks good to me, thank you! Left one random comment :).

// we close now to cover our backs. We don't care about the result: if the channel is
// _already_ closed, that's fine by us.
if closing {
logger.debug("closing connection")
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm totally fine with this but the information here is redundant (because we log it above (metadata: ["ahc-closing": "\(closing)"]).

Copy link
Contributor

Choose a reason for hiding this comment

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

@Lukasa whilst we're here, do you recall if there is metadata attached to the logger still here? Ie. can this message be correlated with the request that made the close happen? I think that would be good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it does: we never remove the metadata key from this logger. We create a new logger when a new request is minted.

Motivation:

Currently when either we or the server send Connection: close, we
correctly do not return that connection to the pool. However, we rely on
the server actually performing the connection closure: we never call
close() ourselves. This is unnecessarily optimistic: a server may
absolutely fail to close this connection. To protect our own file
descriptors, we should make sure that any connection we do not return
the pool is closed.

Modifications:

If we think a connection is closing when we release it, we now call
close() on it defensively.

Result:

We no longer leak connections when the server fails to close them.

Fixes swift-server#324.
@Lukasa Lukasa force-pushed the cb-leaking-connections-is-bad branch from 13d82d1 to 52a44ba Compare January 19, 2021 17:20
@Lukasa Lukasa merged commit 1aec5d7 into swift-server:main Jan 19, 2021
@Lukasa Lukasa deleted the cb-leaking-connections-is-bad branch January 19, 2021 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Connections not being closed, even when server sets the "Connection: close" header
3 participants