Fix race condition in PacketListener

The ChannelHandlerContext was only set on the next received packet,
however `save` could be called before that happened.
This commit is contained in:
Jonas Herzig
2024-05-19 14:27:15 +02:00
parent 046a50f326
commit 4df627029c
2 changed files with 8 additions and 18 deletions

View File

@@ -153,8 +153,9 @@ public class ConnectionEventHandler {
metaData.setGenerator("ReplayMod v" + ReplayMod.instance.getVersion());
metaData.setDate(System.currentTimeMillis());
metaData.setMcVersion(ReplayMod.instance.getMinecraftVersion());
packetListener = new PacketListener(core, outputPath, replayFile, metaData);
Channel channel = ((NetworkManagerAccessor) networkManager).getChannel();
packetListener = new PacketListener(core, channel, outputPath, replayFile, metaData);
if (channel.pipeline().get(PacketListener.DECODER_KEY) != null) {
// Regular channel, we'll inject our recorder directly before the decoder
channel.pipeline().addBefore(PacketListener.DECODER_KEY, PacketListener.RAW_RECORDER_KEY, packetListener);

View File

@@ -22,6 +22,7 @@ import com.replaymod.replaystudio.replay.ReplayMetaData;
import de.johni0702.minecraft.gui.container.VanillaGuiScreen;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
@@ -102,7 +103,7 @@ public class PacketListener extends ChannelInboundHandlerAdapter {
private ReplayMetaData metaData;
private ChannelHandlerContext context = null;
private final Channel channel;
private Packet currentRawPacket;
private final long startTime;
@@ -116,8 +117,9 @@ public class PacketListener extends ChannelInboundHandlerAdapter {
*/
private final AtomicInteger lastSaveMetaDataId = new AtomicInteger();
public PacketListener(ReplayMod core, Path outputPath, ReplayFile replayFile, ReplayMetaData metaData) throws IOException {
public PacketListener(ReplayMod core, Channel channel, Path outputPath, ReplayFile replayFile, ReplayMetaData metaData) throws IOException {
this.core = core;
this.channel = channel;
this.outputPath = outputPath;
this.replayFile = replayFile;
this.metaData = metaData;
@@ -309,15 +311,6 @@ public class PacketListener extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if(ctx == null) {
if(context == null) {
return;
} else {
ctx = context;
}
}
this.context = ctx;
NetworkState connectionState = getConnectionState();
Packet packet = null;
@@ -367,16 +360,12 @@ public class PacketListener extends ChannelInboundHandlerAdapter {
}
private NetworkState getConnectionState() {
ChannelHandlerContext ctx = context;
if (ctx == null) {
return NetworkState.LOGIN;
}
//#if MC>=12002
//$$ AttributeKey<NetworkState.PacketHandler<?>> key = ClientConnection.CLIENTBOUND_PROTOCOL_KEY;
//$$ return ctx.channel().attr(key).get().getState();
//$$ return channel.attr(key).get().getState();
//#else
AttributeKey<NetworkState> key = ClientConnection.ATTR_KEY_PROTOCOL;
return ctx.channel().attr(key).get();
return channel.attr(key).get();
//#endif
}