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

Re:#926, Fix #925 Memory Leak while WebSocketServerHandshakeException… #987

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading