Marcus Leon
Marcus Leon

Reputation: 56699

Swift: Understanding NSLock deadlock

Seeing this message in our logs using NSLock:

*** -[NSLock lock]: deadlock (<NSLock: 0x6100000cbec0> '(null)')
*** Break on _NSLockError() to debug.

Does this mean that the application has encountered a fatal error and will stop working? Or is this handled in some kind of 'graceful' fashion?

Upvotes: 2

Views: 2943

Answers (2)

Rob
Rob

Reputation: 438102

A deadlock, by definition, means that the thread in question cannot proceed. Swift doesn't "handle" the deadlock, but is merely informing you that this occurred.

How this deadlock manifests itself in your app depends upon what the code associated with that thread was doing. But, obviously, whatever it was, it will never complete and the resources for that thread will never be recovered. And if this deadlock took place on the main thread, the app will freeze.

Bottom line, the purpose of this message is not to tell you that the deadlock was handled, but to the contrary, to tell you that it can't be handled, and, so therefore, that it's incumbent upon you to fix the code to eliminate this problem.

Upvotes: 6

L&#233;o Natan
L&#233;o Natan

Reputation: 57060

Swift has nothing to do with the behavior here. This is not a crash. Notice that locks are not a reentrant lock, so calling lock while you already have the lock will cause a deadlock. The app will appear stuck if main thread is deadlocked, or one or more threads will be deadlocked in the background, causing undefined behavior, such as data not arriving, tasks not handled, etc.

Upvotes: 2

Related Questions