fix(anvil): finalize queued dispatch ownership safely
This commit is contained in:
1 parent
09bc15e979
commit
990028c728
8 files changed
+639
-80
No files matched your search
@@ -33,7 +33,8 @@ final class AdvancedAnvilProtocolGuard implements AutoCloseable {
|
||||
BukkitTask expiry;
|
||||
volatile boolean sealed;
|
||||
volatile boolean revoked;
|
||||
volatile boolean handedOff;
|
||||
// Transport-monitor protected, sticky raw-seen/wrapper ownership (not post acknowledgement).
|
||||
boolean wrapped;
|
||||
boolean consumed;
|
||||
final AnvilView view;
|
||||
final net.minecraft.server.network.ServerGamePacketListenerImpl nativeListener;
|
||||
@@ -121,7 +122,7 @@ final class AdvancedAnvilProtocolGuard implements AutoCloseable {
|
||||
nativePlayer.level().getServer().packetProcessor(), () -> {
|
||||
if (!closed && !plugin.getServer().isPrimaryThread()) plugin.getServer().getScheduler().runTask(plugin,
|
||||
() -> { if (!closed && !watched.contains(view)) owner.protocolHookReady(view); });
|
||||
});
|
||||
}, () -> plugin.getServer().isPrimaryThread());
|
||||
dispatches.put(player, dispatch);
|
||||
}
|
||||
if (!dispatch.ready()) return false;
|
||||
@@ -192,6 +193,8 @@ final class AdvancedAnvilProtocolGuard implements AutoCloseable {
|
||||
}
|
||||
if (!(event.getPlayer().getOpenInventory() instanceof AnvilView view) || !watched.contains(view)) return;
|
||||
invalidate(view); // A new raw intent can never reuse an earlier ticket.
|
||||
NativeAnvilDispatch acquiredDispatch = null;
|
||||
Admission acquired = null;
|
||||
try {
|
||||
Object raw = event.getPacket().getHandle();
|
||||
if (seen.put(event, Boolean.TRUE) != null || event.isCancelled()
|
||||
@@ -223,27 +226,30 @@ final class AdvancedAnvilProtocolGuard implements AutoCloseable {
|
||||
if (!admission.consumed && !closed && !admissions.containsKey(view)) owner.protocolRejected(view);
|
||||
};
|
||||
if (!dispatch.admit(admission)) { refuse(event, view); return; }
|
||||
acquiredDispatch = dispatch;
|
||||
acquired = admission;
|
||||
admissions.put(view, admission);
|
||||
var marker = com.comphenix.protocol.events.NetworkMarker.getNetworkMarker(event);
|
||||
if (marker == null) {
|
||||
marker = new com.comphenix.protocol.events.NetworkMarker(
|
||||
com.comphenix.protocol.events.ConnectionSide.CLIENT_SIDE, event.getPacketType());
|
||||
event.setNetworkMarker(marker);
|
||||
}
|
||||
marker.addPostListener(new com.comphenix.protocol.events.PacketPostListener() {
|
||||
@Override public Plugin getPlugin() { return plugin; }
|
||||
@Override public void onPostEvent(PacketEvent forwarded) {
|
||||
if (forwarded == event) admission.handedOff = true;
|
||||
}
|
||||
});
|
||||
admission.expiry = plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
|
||||
admission.revoked = true;
|
||||
// No owned post listener/marker: foreign markers remain entirely untouched.
|
||||
admission.expiry = Objects.requireNonNull(plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
|
||||
dispatch.revoke(admission);
|
||||
if (admissions.get(view) == admission) admissions.remove(view);
|
||||
// Post means forward scheduled, not native completion. Keep such tombstones
|
||||
// until the exact record reaches the hook or its queued wrapper finishes.
|
||||
if (!admission.handedOff) dispatch.complete(admission);
|
||||
}, ADMISSION_TICKS);
|
||||
} catch (RuntimeException | LinkageError unavailable) { refuse(event, view); }
|
||||
// Unresolved raw ownership waits for the two-queue fence or exact raw arrival.
|
||||
// Already-wrapped ownership is safe to release even if native skips handle.
|
||||
}, ADMISSION_TICKS));
|
||||
} catch (RuntimeException | LinkageError unavailable) {
|
||||
if (acquired != null) {
|
||||
acquiredDispatch.revoke(acquired);
|
||||
if (admissions.get(view) == acquired) admissions.remove(view);
|
||||
}
|
||||
refuse(event, view);
|
||||
} finally {
|
||||
// Also fences a partially acquired, cancelled record after timer setup throws.
|
||||
// Cancellation inside a callback is never upfront proof of no raw forward.
|
||||
if (acquired != null && !acquiredDispatch.requestFence()) {
|
||||
invalidate(view);
|
||||
refuse(event, view);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean currentFrame(AnvilView view, ServerboundContainerClickPacket packet) {
|
||||
|
||||
@@ -24,13 +24,22 @@ final class NativeAnvilDispatch extends ChannelInboundHandlerAdapter {
|
||||
private final Runnable readyCallback;
|
||||
private final Map<ServerboundContainerClickPacket, AdvancedAnvilProtocolGuard.Admission> records =
|
||||
new IdentityHashMap<>();
|
||||
private final java.util.Set<ServerboundContainerClickPacket> dispatched =
|
||||
java.util.Collections.newSetFromMap(new IdentityHashMap<>());
|
||||
private final com.comphenix.protocol.scheduler.ProtocolScheduler scheduler;
|
||||
private final java.util.function.BooleanSupplier mainThread;
|
||||
private boolean mainFencePending;
|
||||
private volatile boolean unavailable;
|
||||
private volatile boolean ready;
|
||||
private volatile boolean removing;
|
||||
private boolean retiring;
|
||||
|
||||
NativeAnvilDispatch(Connection connection, PacketProcessor processor, Runnable readyCallback) {
|
||||
NativeAnvilDispatch(Connection connection, PacketProcessor processor, Runnable readyCallback,
|
||||
java.util.function.BooleanSupplier mainThread) {
|
||||
// Link the public, library-owned main queue BEFORE enhanced output/hook installation.
|
||||
// DefaultScheduler.runTask runs after the executing library filters/posts return;
|
||||
// this is an ordering fence, not a promise to wait a strict server tick.
|
||||
scheduler = java.util.Objects.requireNonNull(com.comphenix.protocol.ProtocolLibrary.getScheduler(),
|
||||
"Missing ProtocolLib main scheduler");
|
||||
this.mainThread = java.util.Objects.requireNonNull(mainThread);
|
||||
NativeAnvilPacketLimits.link();
|
||||
this.connection = java.util.Objects.requireNonNull(connection);
|
||||
this.processor = java.util.Objects.requireNonNull(processor);
|
||||
@@ -49,7 +58,7 @@ final class NativeAnvilDispatch extends ChannelInboundHandlerAdapter {
|
||||
boolean ready() {
|
||||
var pipeline = connection.channel.pipeline();
|
||||
var context = pipeline.context(this);
|
||||
return ready && !removing && connection.channel.isActive() && context != null
|
||||
return ready && !unavailable && !removing && connection.channel.isActive() && context != null
|
||||
&& pipeline.context(connection) != null && pipeline.last() == connection
|
||||
&& pipeline.names().indexOf(context.name()) + 1
|
||||
== pipeline.names().indexOf(pipeline.context(connection).name());
|
||||
@@ -72,13 +81,76 @@ final class NativeAnvilDispatch extends ChannelInboundHandlerAdapter {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** One pending library-main callback per dispatch, never one job per admission. */
|
||||
synchronized boolean requestFence() {
|
||||
if (unavailable) return false;
|
||||
if (mainFencePending) return true;
|
||||
mainFencePending = true;
|
||||
try {
|
||||
if (scheduler.runTask(this::mainFence) == null) {
|
||||
failBridge();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (RuntimeException | LinkageError failure) {
|
||||
failBridge();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void mainFence() {
|
||||
try {
|
||||
if (!mainThread.getAsBoolean()) {
|
||||
failBridge();
|
||||
return;
|
||||
}
|
||||
final java.util.List<AdvancedAnvilProtocolGuard.Admission> snapshot;
|
||||
synchronized (this) {
|
||||
mainFencePending = false;
|
||||
if (unavailable) return;
|
||||
// Snapshot HERE, not on Netty: later unfinished receives cannot be declared
|
||||
// NO_FORWARD by a barrier queued before their actual library raw forwards.
|
||||
snapshot = java.util.List.copyOf(records.values()); // bounded by MAX_OUTSTANDING
|
||||
}
|
||||
connection.channel.eventLoop().execute(() -> terminalFence(snapshot));
|
||||
} catch (RuntimeException | LinkageError failure) {
|
||||
// Rejected submission is not evidence of NO_FORWARD.
|
||||
failBridge();
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void terminalFence(java.util.List<AdvancedAnvilProtocolGuard.Admission> snapshot) {
|
||||
for (var admission : snapshot) {
|
||||
if (records.get(admission.packet) != admission) continue;
|
||||
if (!admission.wrapped) {
|
||||
// All actual raw forwards captured before mainFence are ahead of us on
|
||||
// this same event loop. Never infer acknowledgement from mutable events/posts.
|
||||
admission.revoked = true;
|
||||
records.remove(admission.packet);
|
||||
} else if (admission.revoked) records.remove(admission.packet);
|
||||
}
|
||||
// Transport-only completion: no Bukkit/map/policy callback on the socket loop.
|
||||
removeIfDrained();
|
||||
}
|
||||
|
||||
private synchronized void failBridge() {
|
||||
unavailable = true;
|
||||
mainFencePending = false;
|
||||
revokeRecords(); // Keep unresolved exact-raw tombstones; no false cleanup claim.
|
||||
removeIfDrained();
|
||||
}
|
||||
|
||||
static AdvancedAnvilProtocolGuard.Admission current() { return CURRENT.get(); }
|
||||
|
||||
@Override public void channelRead(ChannelHandlerContext context, Object message) {
|
||||
AdvancedAnvilProtocolGuard.Admission admission;
|
||||
synchronized (this) {
|
||||
admission = records.get(message);
|
||||
if (admission != null && !dispatched.add(admission.packet)) return;
|
||||
if (admission != null) {
|
||||
if (admission.wrapped) return; // Duplicate exact raw while still owned.
|
||||
admission.wrapped = true; // Sticky, even after ledger retirement.
|
||||
if (admission.revoked) complete(admission);
|
||||
}
|
||||
}
|
||||
if (admission == null) {
|
||||
context.fireChannelRead(message);
|
||||
@@ -92,13 +164,25 @@ final class NativeAnvilDispatch extends ChannelInboundHandlerAdapter {
|
||||
|
||||
synchronized void complete(AdvancedAnvilProtocolGuard.Admission admission) {
|
||||
if (records.get(admission.packet) == admission) records.remove(admission.packet);
|
||||
dispatched.remove(admission.packet);
|
||||
removeIfDrained();
|
||||
}
|
||||
|
||||
synchronized void revoke(AdvancedAnvilProtocolGuard.Admission admission) {
|
||||
admission.revoked = true;
|
||||
// The wrapper retains irreversible denial even if native never calls its handle.
|
||||
if (admission.wrapped) complete(admission);
|
||||
}
|
||||
|
||||
private void revokeRecords() {
|
||||
records.values().removeIf(admission -> {
|
||||
admission.revoked = true;
|
||||
return admission.wrapped;
|
||||
});
|
||||
}
|
||||
|
||||
synchronized void retire() {
|
||||
retiring = true;
|
||||
records.values().forEach(admission -> admission.revoked = true);
|
||||
revokeRecords();
|
||||
removeIfDrained();
|
||||
}
|
||||
|
||||
@@ -116,7 +200,6 @@ final class NativeAnvilDispatch extends ChannelInboundHandlerAdapter {
|
||||
synchronized (this) {
|
||||
records.values().forEach(admission -> admission.revoked = true);
|
||||
records.clear();
|
||||
dispatched.clear();
|
||||
retiring = true;
|
||||
removeIfDrained();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user