RimWorld

RimWorld

Simple sidearms
Race Condition when entering job tick for melee combat
Technical Analysis & Suggested Fix
The error strongly suggests a race condition. The game's core code is likely iterating through a pawn's inventory or equipment collection to execute the JobDriver_AttackMelee, while Simple Sidearms simultaneously attempts to modify that same collection to switch weapons.

This results in the InvalidOperationException, as the collection is being modified during enumeration.

Suggested Code Edit:

The issue could likely be resolved by ensuring that any modifications to a pawn's inventory/equipment collections are performed on a copy, rather than the live collection being enumerated.

Example of Problematic Pattern:

C#
// This pattern can cause the crash if another process is reading the collection. foreach (Thing item in pawn.inventory.innerContainer) { if (ShouldBeRemoved(item)) { // Modifies the collection while it's being looped over. pawn.inventory.innerContainer.Remove(item); } }
Example of Suggested Solution:

C#
// Create a copy of the list to iterate over safely. List<Thing> inventorySnapshot = new List<Thing>(pawn.inventory.innerContainer); foreach (Thing item in inventorySnapshot) { if (ShouldBeRemoved(item)) { // Modifies the original list, which is now safe. pawn.inventory.innerContainer.Remove(item); } }
Thank you for your hard work on this excellent mod! I hope this detailed report is helpful for debugging.
Last edited by Dreadarm; 2 hours ago