Skip to content

Commit fab2948

Browse files
committed
Guard refresh memory leak fix against concurrency issues
1 parent 5979db9 commit fab2948

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

plugin/src/main/java/net/elytrium/limboapi/server/LimboImpl.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ public LimboImpl(LimboAPI plugin, VirtualWorld world) {
184184
}
185185

186186
protected void refresh() {
187-
this.localDispose();
188-
189187
this.built = true;
190188
JoinGamePacket legacyJoinGame = this.createLegacyJoinGamePacket();
191189
JoinGamePacket joinGame = this.createJoinGamePacket(ProtocolVersion.MINECRAFT_1_16);
@@ -351,7 +349,17 @@ private void addPostJoin(PreparedPacket packet) {
351349
@Override
352350
public void spawnPlayer(Player apiPlayer, LimboSessionHandler handler) {
353351
if (!this.built) {
354-
this.refresh();
352+
List<PreparedPacket> packets = this.takeSnapshot();
353+
try {
354+
this.refresh();
355+
} finally {
356+
List<PreparedPacket> changed = this.takeSnapshot();
357+
for (PreparedPacket packet : packets) {
358+
if (packet != null && !changed.contains(packet)) {
359+
packet.release();
360+
}
361+
}
362+
}
355363
}
356364

357365
ConnectedPlayer player = (ConnectedPlayer) apiPlayer;
@@ -730,19 +738,29 @@ public void dispose() {
730738
}
731739
}
732740

733-
private void localDispose() {
734-
if (this.joinPackets == null) {
735-
return;
741+
private List<PreparedPacket> takeSnapshot() {
742+
List<PreparedPacket> packets = new ArrayList<>();
743+
744+
packets.add(this.joinPackets);
745+
packets.add(this.fastRejoinPackets);
746+
packets.add(this.safeRejoinPackets);
747+
packets.add(this.respawnPackets);
748+
packets.add(this.firstChunks);
749+
if (this.delayedChunks != null) {
750+
packets.addAll(this.delayedChunks);
736751
}
752+
packets.add(this.configTransitionPackets);
753+
packets.add(this.configPackets);
737754

738-
this.joinPackets.release();
739-
this.fastRejoinPackets.release();
740-
this.safeRejoinPackets.release();
741-
this.respawnPackets.release();
742-
this.firstChunks.release();
743-
this.delayedChunks.forEach(PreparedPacket::release);
744-
this.configTransitionPackets.release();
745-
this.configPackets.release();
755+
return packets;
756+
}
757+
758+
private void localDispose() {
759+
this.takeSnapshot().forEach(packet -> {
760+
if (packet != null) {
761+
packet.release();
762+
}
763+
});
746764
}
747765

748766
// From Velocity.

0 commit comments

Comments
 (0)