Skip to content

Commit e0e30bf

Browse files
committed
netty: fix leak + disconnected websocket clients
- WebSocket onClose not called for Netty fix #3452 - Memory leak in jooby-netty fix #3453
1 parent 9283105 commit e0e30bf

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyHandler.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,17 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
9999
}
100100
}
101101
} else if (isLastHttpContent(msg)) {
102-
// when decoder == null, chunk is always a LastHttpContent.EMPTY, ignore it
103-
if (context.decoder != null) {
104-
offer(context, (HttpContent) msg);
105-
Router.Match route = router.match(context);
106-
resetDecoderState(context, !route.matches());
107-
route.execute(context);
102+
var chunk = (HttpContent) msg;
103+
try {
104+
// when decoder == null, chunk is always a LastHttpContent.EMPTY, ignore it
105+
if (context.decoder != null) {
106+
offer(context, chunk);
107+
Router.Match route = router.match(context);
108+
resetDecoderState(context, !route.matches());
109+
route.execute(context);
110+
}
111+
} finally {
112+
release(chunk);
108113
}
109114
} else if (isHttpContent(msg)) {
110115
var chunk = (HttpContent) msg;

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyWebSocket.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ public NettyWebSocket(NettyContext ctx) {
103103
this.netty = ctx;
104104
this.key = ctx.getRoute().getPattern();
105105
this.dispatch = !ctx.isInIoThread();
106-
this.netty.ctx.channel().attr(WS).set(this);
106+
var channel = this.netty.ctx.channel();
107+
channel.attr(WS).set(this);
108+
channel.closeFuture().addListener(future -> handleClose(WebSocketCloseStatus.GOING_AWAY));
107109
}
108110

109111
@NonNull @Override

0 commit comments

Comments
 (0)