The dictionary is protected with an NIOLock, but we’re still encountering random crashes when trying to look up values. I’m not sure why this is happening. Any advice or suggestions would be greatly appreciated.
I’m using a distributed actor as a local storage mechanism for actors. The lookup method is supposed to check if the current ID is already present in the storage or in the active tasks. If it’s not found in either, it creates a new task and returns a future result.
Both the storage and activeTasks dictionaries are wrapped in a SafeDictionary, which uses an NIOLock for thread safety. This setup is running in a Linux Docker container with Swift 5.10.1 and the latest version of Swift NIO. I’ve attached the full backtrace file for reference. Any insights would be helpful.
I'm not sure that SafeDictionary does what you want it to do, and as used here it is explicitly unsafe. The fact that you have mutating functions on this dictionary make it fairy clear that the values don't behave sensibly.
In this case, you have two options: either fix SafeDictionary, or avoid needing it. Given that you are using it from inside an actor, you may simply not need it. If you do, consider replacing SafeDictionary with NIOLockedValueBox, which should prevent these issues entirely.
I initially thought I wouldn’t need an additional lock within the actor, but I was experiencing these types of crashes more frequently before switching to SafeDictionary.
I enabled strict concurrency checking, but it didn't provide any specific errors or warnings for the lookup logic. So, I wrapped it in NIOLockedValueBox to see if that makes a difference. I typically encounter 2-3 crashes in this particular area within a 24-hour period. I’ll update you tomorrow on whether this approach helped.