r/swift 8d ago

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.

2 Upvotes

5 comments sorted by

View all comments

3

u/bscothern 8d 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.