2

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?

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
  • What it means is you should post the code where you're calling `[NSLock lock]` (Whatever threads are involved in this issue would never return, this is a bad thing) – Tibrogargan Oct 15 '16 at 21:56
  • 1
    I understand why the deadlock is happening (so I didn't post the code). My question is about how Swift/NSLock handles the deadlock when it happens. – Marcus Leon Oct 15 '16 at 22:05

2 Answers2

6

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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
2

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.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195