Skip to content
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

Ensure we only add OpenSslEngine to the OpenSslEngineMap when handsha… #4653

Closed
wants to merge 1 commit into from

Conversation

normanmaurer
Copy link
Member

…ke is started

Motivation:

We need to ensure we only add the OpenSslEngine to the OpenSslEngineMap when the handshake is started as otherwise we may produce a memory leak when the OpenSslEngine is created but not actually used. This can for example happen if we encounter a connection refused from the remote peer. In this case we will never remove the OpenSslEngine from the OpenSslEngineMap and so it will never be collected (as we hold a reference). This has as affect that the finalizer will never be run as well.

Modifications:

  • Lazy add the OpenSslEngine to the OpenSslEngineMap to elimate possible leak.
  • Call OpenSslEngine.shutdown() when SslHandler is removed from the ChannelPipeline to free memory asap in all cases.

Result:

No more memory leak with OpenSslEngine if connection is refused.

…ke is started

Motivation:

We need to ensure we only add the OpenSslEngine to the OpenSslEngineMap when the handshake is started as otherwise we may produce a memory leak when the OpenSslEngine is created but not actually used. This can for example happen if we encounter a connection refused from the remote peer. In this case we will never remove the OpenSslEngine from the OpenSslEngineMap and so it will never be collected (as we hold a reference). This has as affect that the finalizer will never be run as well.

Modifications:

- Lazy add the OpenSslEngine to the OpenSslEngineMap to elimate possible leak.
- Call OpenSslEngine.shutdown() when SslHandler is removed from the ChannelPipeline to free memory asap in all cases.

Result:

No more memory leak with OpenSslEngine if connection is refused.
@normanmaurer normanmaurer self-assigned this Jan 4, 2016
@normanmaurer normanmaurer added this to the 4.0.34.Final milestone Jan 4, 2016
@normanmaurer
Copy link
Member Author

@nmittler @Scottmitch @trustin PTAL... This is a serious bug when you use OpenSslEngine on the client-side and result in a memory leak.

@@ -429,6 +429,10 @@ public void handlerRemoved0(ChannelHandlerContext ctx) throws Exception {
// Check if queue is not empty first because create a new ChannelException is expensive
pendingUnencryptedWrites.removeAndFailAll(new ChannelException("Pending write on removal of SslHandler"));
}
if (engine instanceof OpenSslEngine) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this a second leak? Or is it related?

Copy link
Member Author

Choose a reason for hiding this comment

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

I wouldn't say its a leak as shutdown() will be called in the finalize() as well. It just release memory as fast as possible as you never know when the finalize() method will be run at the end.

Copy link
Member Author

Choose a reason for hiding this comment

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

@nmittler so it's more an improvement to free up memory asap

Copy link
Member Author

Choose a reason for hiding this comment

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

@nmittler also it's in the modification section but I can also adjust the commit message to make it more explicit if you want too.

Copy link
Member

Choose a reason for hiding this comment

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

@normanmaurer Yeah I noticed it after .... the commit message is fine as-is. SGTM, thanks!

@nmittler
Copy link
Member

nmittler commented Jan 4, 2016

LGTM

1 similar comment
@trustin
Copy link
Member

trustin commented Jan 5, 2016

LGTM

@normanmaurer
Copy link
Member Author

Cherry-picked into 4.0 (c864c41) and 4.1 (a157528)

@normanmaurer normanmaurer deleted the ssl_leak_connection_refused branch January 5, 2016 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants