mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-12-02 12:28:05 +00:00
Skip main-thread tasks if peripheral is detached
Due to the asynchronous nature of main-thread tasks, it's possible for them to be executed on peripherals which have been detached. This has been known for a long time (#893 was opened back in 2021), but finding a good solution here is tricky. Most of the time the method will silently succeed, but if we try to interact with an IComputerAccess (such as in inventory methods, as seen in #1750), we throw a NotAttachedException exception and spam the logs! This is an initial step towards fixing this - when calling a peripheral method via peripheral.call/modem.callRemote, we now wrap any enqueued main-thread tasks and silently skip them if the peripheral has been detached since. This means that peripheral methods may start to return nil when they didn't before. I think this is *fine* (though not ideal for sure!) - we return nil if the peripheral has been detached, so it's largely equivalent to that.
This commit is contained in:
@@ -11,6 +11,7 @@ import dan200.computercraft.api.lua.MethodResult;
|
||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||
import dan200.computercraft.api.peripheral.IDynamicPeripheral;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.core.computer.GuardedLuaContext;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
|
||||
@@ -26,12 +27,16 @@ public final class GenericPeripheral implements IDynamicPeripheral {
|
||||
private final Set<String> additionalTypes;
|
||||
private final List<SaturatedMethod> methods;
|
||||
|
||||
private @Nullable GuardedLuaContext contextWrapper;
|
||||
private final GuardedLuaContext.Guard guard;
|
||||
|
||||
GenericPeripheral(BlockEntity tile, Direction side, String type, Set<String> additionalTypes, List<SaturatedMethod> methods) {
|
||||
this.side = side;
|
||||
this.tile = tile;
|
||||
this.type = type;
|
||||
this.additionalTypes = additionalTypes;
|
||||
this.methods = methods;
|
||||
this.guard = () -> !tile.isRemoved() && tile.getLevel() != null && tile.getLevel().isLoaded(tile.getBlockPos());
|
||||
}
|
||||
|
||||
public Direction side() {
|
||||
@@ -47,7 +52,12 @@ public final class GenericPeripheral implements IDynamicPeripheral {
|
||||
|
||||
@Override
|
||||
public MethodResult callMethod(IComputerAccess computer, ILuaContext context, int method, IArguments arguments) throws LuaException {
|
||||
return methods.get(method).apply(context, computer, arguments);
|
||||
var contextWrapper = this.contextWrapper;
|
||||
if (contextWrapper == null || !contextWrapper.wraps(context)) {
|
||||
contextWrapper = this.contextWrapper = new GuardedLuaContext(context, guard);
|
||||
}
|
||||
|
||||
return methods.get(method).apply(contextWrapper, computer, arguments);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,6 +15,7 @@ import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.peripheral.NotAttachedException;
|
||||
import dan200.computercraft.api.peripheral.WorkMonitor;
|
||||
import dan200.computercraft.core.apis.PeripheralAPI;
|
||||
import dan200.computercraft.core.computer.GuardedLuaContext;
|
||||
import dan200.computercraft.core.methods.PeripheralMethod;
|
||||
import dan200.computercraft.core.util.LuaUtil;
|
||||
import dan200.computercraft.shared.computer.core.ServerContext;
|
||||
@@ -304,7 +305,7 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements Wi
|
||||
return wrappers == null ? null : wrappers.get(remoteName);
|
||||
}
|
||||
|
||||
private static class RemotePeripheralWrapper implements IComputerAccess {
|
||||
private static class RemotePeripheralWrapper implements IComputerAccess, GuardedLuaContext.Guard {
|
||||
private final WiredModemElement element;
|
||||
private final IPeripheral peripheral;
|
||||
private final IComputerAccess computer;
|
||||
@@ -317,6 +318,8 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements Wi
|
||||
private volatile boolean attached;
|
||||
private final Set<String> mounts = new HashSet<>();
|
||||
|
||||
private @Nullable GuardedLuaContext contextWrapper;
|
||||
|
||||
RemotePeripheralWrapper(WiredModemElement element, IPeripheral peripheral, IComputerAccess computer, String name, Map<String, PeripheralMethod> methods) {
|
||||
this.element = element;
|
||||
this.peripheral = peripheral;
|
||||
@@ -364,7 +367,19 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements Wi
|
||||
public MethodResult callMethod(ILuaContext context, String methodName, IArguments arguments) throws LuaException {
|
||||
var method = methodMap.get(methodName);
|
||||
if (method == null) throw new LuaException("No such method " + methodName);
|
||||
return method.apply(peripheral, context, this, arguments);
|
||||
|
||||
// Wrap the ILuaContext. We try to reuse the previous context where possible to avoid allocations.
|
||||
var contextWrapper = this.contextWrapper;
|
||||
if (contextWrapper == null || !contextWrapper.wraps(context)) {
|
||||
contextWrapper = this.contextWrapper = new GuardedLuaContext(context, this);
|
||||
}
|
||||
|
||||
return method.apply(peripheral, contextWrapper, this, arguments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkValid() {
|
||||
return attached;
|
||||
}
|
||||
|
||||
// IComputerAccess implementation
|
||||
|
||||
Reference in New Issue
Block a user