"Collection mutated while being enumerated" error at @main

I have had this bug in my project for a long time and I just can't figure it out. It is a SpriteKit application and I'm using RxSwift to coordinate game events.

The problem is the error is thrown at the very top level of the application (@main), and the array isn't identified by name. I've used the debugging tools to examine all the threads. The few that are in "my code" (as opposed to just assembly) are always in a different spots and almost never in any sort of array operation. I've used LLDB to interrogate the application as much as possible, but since in the thread with the error there is only a memory address to go on, it hasn't helped much. (I can print out the contents of that address but as you can imagine it isn't very illuminating since I don't even know the type of the contents.)

I haven't been able to find any part that of the application that triggers this error. I've carefully examined all interactions with collections and ensured no concurrency issues or attempts to change the contents. I even migrated from using NSLocks to the new actor concurrency model in swift to protect access to all collections. It works either way, but the error is the same:

Thread 1: "*** Collection <__NSArrayM: 0x6000006a9a70> was mutated while being enumerated."

I realize since I can't provide an isolated code block where this is happening it is difficult to help, but I'm hoping for guidance on how I can narrow it down. I've probably spend 40 hours on it so far. It has forced me to learn LLDB, the internal debugging tools in Xcode, and the structured concurrency system in Swift - so nice journey but I'm still left with this issue.

One thing you can do if this is a Mac app or if you can reproduce it in the simulator is set MallocStackLogging=1 in your environment and then use the malloc_history commandline tool in address-specific mode to print a stack trace of where the logged address got allocated from

Wow thanks - that opened up a whole world of debugging improvements.

Turns out the array is the _bodies property of the PKPhysicsWorld instance in my game scene. This gives me something to focus on, however I'm a little concerned as I never directly interact with that array. I only even look a the bodies in a really simple SKPhysicsContactDelegate.