diff --git a/src/main/scala/li/cil/oc/server/machine/Machine.scala b/src/main/scala/li/cil/oc/server/machine/Machine.scala index b25c18bb4..f41ca7613 100644 --- a/src/main/scala/li/cil/oc/server/machine/Machine.scala +++ b/src/main/scala/li/cil/oc/server/machine/Machine.scala @@ -117,19 +117,17 @@ class Machine(val host: MachineHost) extends prefab.ManagedEnvironment with mach } case _ => 0 })) - val oldArchitecture = architecture - architecture = null + var newArchitecture: Architecture = null components.find { case stack: ItemStack => Option(Driver.driverFor(stack, host.getClass)) match { case Some(driver: Processor) if driver.slot(stack) == Slot.CPU => Option(driver.architecture(stack)) match { case Some(clazz) => - if (oldArchitecture == null || oldArchitecture.getClass != clazz) { - architecture = clazz.getConstructor(classOf[machine.Machine]).newInstance(this) - if (node.network != null) architecture.onConnect() + if (architecture == null || architecture.getClass != clazz) { + newArchitecture = clazz.getConstructor(classOf[machine.Machine]).newInstance(this) } else { - architecture = oldArchitecture + newArchitecture = architecture } true case _ => false @@ -138,6 +136,12 @@ class Machine(val host: MachineHost) extends prefab.ManagedEnvironment with mach } case _ => false } + // This needs to operate synchronized against the worker thread, to avoid the + // architecture changing while it is currently being executed. + if (newArchitecture != architecture) this.synchronized { + architecture = newArchitecture + if (architecture != null && node.network != null) architecture.onConnect() + } hasMemory = Option(architecture).fold(false)(_.recomputeMemory(components)) }