Skip to content

Commit

Permalink
Merge pull request #995 from booky10/fix/packet-sending-race-condition
Browse files Browse the repository at this point in the history
Synchronize packet encoding
  • Loading branch information
retrooper committed Sep 20, 2024
2 parents 02a565b + a3b4cc2 commit cb277bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ default Object[] transformWrappers(PacketWrapper<?> wrapper, Object channel, boo
PacketWrapper<?>[] wrappers = PacketTransformationUtil.transform(wrapper);
Object[] buffers = new Object[wrappers.length];
for (int i = 0; i < wrappers.length; i++) {
wrappers[i].prepareForSend(channel, outgoing);
buffers[i] = wrappers[i].buffer;
// Fix race condition when sending packets to multiple people (due to when the buffer is freed)
wrappers[i].buffer = null;
PacketWrapper<?> wrappper = wrappers[i];
synchronized (wrappper.bufferLock) {
wrappper.prepareForSend(channel, outgoing);
buffers[i] = wrappper.buffer;
// Fix race condition when sending packets to multiple people (due to when the buffer is freed)
wrappper.buffer = null;
}
}
return buffers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public class PacketWrapper<T extends PacketWrapper<T>> {
@Nullable
public Object buffer;

@ApiStatus.Internal
public final Object bufferLock = new Object();

protected ClientVersion clientVersion;
protected ServerVersion serverVersion;
private PacketTypeData packetTypeData;
Expand Down

0 comments on commit cb277bf

Please sign in to comment.