MacBook Pro late 2013.
macOS Catalina 10.15.6
I found this really strange bug in which the print function causes a deadlock when called within a dispatch queue.
The deadlock occurs at the line
in AuthorizationManager.description, which is indirectly called by
Intriguingly, changing the above line to any of the following prevents the deadlock:
Furthermore, removing the line
also prevents the deadlock.
I suspect that this bug is related to the fact that the print function is synchronized.
I also tested out other locking mechanisms instead of DispatchQueue, including NSLock, NSRecursiveLock and even this fancy implementation from swift-log, and I experienced the exact same bug.
I posted this issue on the swift forums, and a commenter summarized the cause of the bug as follows:
So I found that for a dead lock to happen, you need to repeatedly and concurrently do the following:
- You run a block in dispatchQueue,
- The block prints anything [In this case it is the line `print("Refreshing")`],
- The block runs print(X) on a separated concurrent queue, where
- X is CustomStringConvertible that uses dispatchQueue inside description.
Here is the code that causes the issue: