Skip to content

Commit

Permalink
Destroy rakPipeline() properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Alemiz112 committed Apr 14, 2024
1 parent 4d37bf1 commit 04d4c83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.cloudburstmc.netty.handler.codec.raknet.server.RakServerRateLimiter;
import org.cloudburstmc.netty.handler.codec.raknet.server.RakServerRouteHandler;
import org.cloudburstmc.netty.handler.codec.raknet.server.RakServerTailHandler;
import org.cloudburstmc.netty.util.RakUtils;

import java.net.InetAddress;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -91,13 +92,18 @@ public RakChildChannel getChildChannel(SocketAddress address) {

private void onChildClosed(ChannelFuture channelFuture) {
RakChildChannel channel = (RakChildChannel) channelFuture.channel();
channel.rakPipeline().fireChannelInactive();
channel.rakPipeline().fireChannelUnregistered();
this.childChannelMap.remove(channel.remoteAddress());

if (this.config().getMetrics() != null) {
this.config().getMetrics().channelClose(channel.remoteAddress());
}

channel.rakPipeline().fireChannelInactive();
channel.rakPipeline().fireChannelUnregistered();
// Need to use reflection to destroy pipeline because
// DefaultChannelPipeline.destroy() is only called when channel.isOpen() is false,
// but the method is called on parent channel, and there is no other way to destroy pipeline.
RakUtils.destroyChannelPipeline(channel.rakPipeline());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
if (this.future != null) {
this.future.cancel(false);
this.future = null;
}

EncapsulatedPacket message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.DefaultChannelPipeline;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.*;
import java.util.Queue;

public class RakUtils {

private static final Constructor<DefaultChannelPipeline> DEFAULT_CHANNEL_PIPELINE_CONSTRUCTOR;
private static final Method PIPELINE_DESTROY_METHOD;

static {
try {
Expand All @@ -37,6 +40,14 @@ public class RakUtils {
} catch (NoSuchMethodException e) {
throw new AssertionError("Unable to find DefaultChannelPipeline(Channel) constructor", e);
}

try {
Method method = DefaultChannelPipeline.class.getDeclaredMethod("destroy");
method.setAccessible(true);
PIPELINE_DESTROY_METHOD = method;
} catch (NoSuchMethodException e) {
throw new AssertionError("Unable to find DefaultChannelPipeline.destroy() method", e);
}
}

public static DefaultChannelPipeline newChannelPipeline(Channel channel) {
Expand All @@ -47,6 +58,14 @@ public static DefaultChannelPipeline newChannelPipeline(Channel channel) {
}
}

public static void destroyChannelPipeline(ChannelPipeline pipeline) {
try {
PIPELINE_DESTROY_METHOD.invoke(pipeline);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException("Unable to destroy DefaultChannelPipeline", e);
}
}

private static final int AF_INET6 = 23;

public static InetSocketAddress readAddress(ByteBuf buffer) {
Expand Down

0 comments on commit 04d4c83

Please sign in to comment.