Skip to content

Commit

Permalink
Merge pull request #987 from h3mant-1/memory_leak_patch
Browse files Browse the repository at this point in the history
Re:#926, Fix #925 Memory Leak while WebSocketServerHandshakeException…
  • Loading branch information
mrniko authored Jul 24, 2024
2 parents 8243315 + fb46707 commit bebdad6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void addClient(ClientHead clientHead) {
uuid2clients.put(clientHead.getSessionId(), clientHead);
}

public void removeClient(UUID sessionId) {
uuid2clients.remove(sessionId);
public ClientHead removeClient(UUID sessionId) {
return uuid2clients.remove(sessionId);
}

public ClientHead get(UUID sessionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,31 +153,47 @@ private void handshake(ChannelHandlerContext ctx, final UUID sessionId, String p
new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true, configuration.getMaxFramePayloadLength());
WebSocketServerHandshaker handshaker = factory.newHandshaker(req);
if (handshaker != null) {
ChannelFuture f = handshaker.handshake(channel, req);
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
log.error("Can't handshake " + sessionId, future.cause());
return;
try {
ChannelFuture f = handshaker.handshake(channel, req);
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
log.error("Can't handshake {}", sessionId, future.cause());
closeClient(sessionId, channel);
return;
}
channel.pipeline().addBefore(SocketIOChannelInitializer.WEB_SOCKET_TRANSPORT, SocketIOChannelInitializer.WEB_SOCKET_AGGREGATOR,
new WebSocketFrameAggregator(configuration.getMaxFramePayloadLength()));
connectClient(channel, sessionId);
}

channel.pipeline().addBefore(SocketIOChannelInitializer.WEB_SOCKET_TRANSPORT, SocketIOChannelInitializer.WEB_SOCKET_AGGREGATOR,
new WebSocketFrameAggregator(configuration.getMaxFramePayloadLength()));
connectClient(channel, sessionId);
}
});
});
} catch (Throwable e) {
log.warn("Can't handshake {}, {}", sessionId, e.getMessage(), e);
closeClient(sessionId, channel);
}
} else {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
}
}

private void closeClient(UUID sessionId, Channel channel) {
try {
channel.close();
} catch (Throwable t) {
log.warn("Can't close channel for sessionId: {}", sessionId, t);
}
ClientHead clientHead = clientsBox.removeClient(sessionId);
clientHead.disconnect();
log.info("Client with sessionId: {} was disconnected", sessionId);
}

private void connectClient(final Channel channel, final UUID sessionId) {
ClientHead client = clientsBox.get(sessionId);
if (client == null) {
log.warn("Unauthorized client with sessionId: {} with ip: {}. Channel closed!",
sessionId, channel.remoteAddress());
channel.close();
closeClient(sessionId, channel);
return;
}

Expand Down

0 comments on commit bebdad6

Please sign in to comment.