Skip to content

Commit

Permalink
Synchronize packet encoding
Browse files Browse the repository at this point in the history
Resolves concurrency issues which may happen when sending the same packet wrapper to different players, concurrently

This is not a perfect solution, but the alternative would be refactoring the packetwrapper to be based on buffers passed during encoding as parameters, which would be too much effort and would require breaking changes
  • Loading branch information
booky10 committed Sep 17, 2024
1 parent 99acbe4 commit a3b4cc2
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 @@ -128,6 +128,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 a3b4cc2

Please sign in to comment.