Skip to content

Commit 9767295

Browse files
committed
Extract channel close listener into static CloseChannelFutureListener
The close listener now captures only the NettyStream, so the one-shot connection-open handler can no longer be pinned for the channel lifetime. Replaces the local-variable capture idiom with a structural guarantee. JAVA-6250
1 parent 2745450 commit 9767295

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

driver-core/src/main/com/mongodb/internal/connection/netty/NettyStream.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,9 @@ public void operationComplete(final ChannelFuture future) {
530530
channelFuture.channel().close();
531531
} else {
532532
channel = channelFuture.channel();
533-
// capture only the enclosing stream in the close listener: capturing OpenChannelFutureListener.this
534-
// would pin the open handler (and everything it references) for the lifetime of the pooled channel
535-
NettyStream stream = NettyStream.this;
536-
channel.closeFuture().addListener((ChannelFutureListener) future1 ->
537-
stream.handleReadResponse(null, new IOException("The connection to the server was closed")));
533+
// CloseChannelFutureListener captures only the NettyStream, never OpenChannelFutureListener.this,
534+
// so the one-shot connection-open handler is not pinned for the lifetime of the pooled channel (JAVA-6250)
535+
channel.closeFuture().addListener(new CloseChannelFutureListener(NettyStream.this));
538536
}
539537
handler.completed(null);
540538
} else {
@@ -550,6 +548,19 @@ public void operationComplete(final ChannelFuture future) {
550548
}
551549
}
552550

551+
private static final class CloseChannelFutureListener implements ChannelFutureListener {
552+
private final NettyStream stream;
553+
554+
CloseChannelFutureListener(final NettyStream stream) {
555+
this.stream = stream;
556+
}
557+
558+
@Override
559+
public void operationComplete(final ChannelFuture future) {
560+
stream.handleReadResponse(null, new IOException("The connection to the server was closed"));
561+
}
562+
}
563+
553564
private static void cancel(@Nullable final Future<?> f) {
554565
if (f != null) {
555566
f.cancel(false);

0 commit comments

Comments
 (0)