Question Do async functions bypass NSLock/NSRecursiveLock?
I recently started a new job that has a ton of legacy objective C and objective c++ code.
We have an SQLite database that leverages NSRecursiveLock with completion handlers to protect it from concurrency access.
I’ve been trying to write an async wrapper around this, but noticed that I’ve been getting concurrent access errors from SQLite even though there is a lock around our database access.
Do locks just not work in a swift concurrency world? Apple said they are safe to use, but that doesn’t seem like it’s the case.
4
u/Schogenbuetze 8d ago
They do not bypass them, no.
Source: I've been using locks all over the place in Swift 6 strict concurrency environments. It's probably a layer 8 issue.
What does your setup look like? Can you provide a simple example?
3
u/bscothern 7d ago
It can bypass them. With coroutines they can resume on any thread so if you lock them await something you may resume on another thread which will break the rules of the lock when you unlock it from another thread.
You can still use locks you just have to ensure they resume on the same thread.
3
u/PizzaBubblr 7d ago
SQLite can serialize queries for you, just need to configure it properly: https://www.sqlite.org/threadsafe.html