Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit f7a0ac9

Browse files
nguyenhuyHannah Troisi
authored andcommitted
[ASThread][ASDisplayNode] Detect and avoid deadlocks caused by upward lock gathering in didEnter/Exit states (#2764)
* Implement mutex ownership and use it to check potential upward lock gathering * Don't hold instance lock and call willEnterHierarchy/didExitHierarchy of ASDisplayNode - This can cause deadlocks (e.g #2605) if subsequent methods, that are implemented by developers, walk up the node tree. - This is a way to keep the optimizations introduced in 9e87813 while making sure the locking situation is a bit safer. * More lock ownership assertions in ASDisplayNode * Document main thread contract of -clearContents * ASNetworkImageNode shoud not call setNeedsPreload while holding instance lock - This helps to avoid potentially deadlocks caused if the node (esp in case it's a subclass of ASNetworkImageNode) walks up the tree in didEnterPreloadState, for example to build logging context. * ASVideoNode should not call setNeedsPreload while holding instance lock - This helps to avoid potentially deadlocks caused if the node (esp. if it's a subclass of ASVideoNode) walks up the tree in didEnterPreloadState, for example to build logging context. * Don't hold instance lock for the entire insert subnode operation - The recursive lock should not be held throughout `_insertSubnode:atSubnodeIndex:sublayerIndex:andRemoveSubnode:`. The method instead should manage the lock itself and acquire it as shortly as possible. The reason is that this method calls many methods outside the scope of `self`. `[subnode __setSupernode:self]` is especially troublesome because it causes the subnode to migrate to new hierarchy and interface states, which triggers `didEnter/Exit(.*)State` methods. These methods are meant to be overriden by subclasses. Thus they might walk up the node tree and potentially cause deadlocks, or they perform expensive tasks and cause the lock to be held for too long. - Other methods that call this method should release the lock before doing so. * Lock getter and setter of `synchronous` flag * Address comment in ASVideoNode * Add main thread assertions to methods that change asset and assetURL of ASVideoNode * Explain CHECK_LOCKING_SAFETY flag * More thread and locking assertions in ASVideNode - It's not safe to call `-[subnode __setSupernode:self]` while holding instance lock of soon-to-be supernode (e.g `self`). - It's also not safe to call `[subnode __setSupernode:nil]` while holding the instance lock of the old supernode (e.g `self`). - did(Enter|Exit)(.*)State methods are always called on main. Add main thread assertions to indicate that. * Minor change in explanation of CHECK_LOCKING_SAFETY flag
1 parent 546925b commit f7a0ac9

5 files changed

Lines changed: 318 additions & 154 deletions

File tree

AsyncDisplayKit/ASDisplayNode+Subclasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ NS_ASSUME_NONNULL_BEGIN
327327
* Provides an opportunity to clear backing store and other memory-intensive intermediates, such as text layout managers
328328
* on the current node.
329329
*
330-
* @discussion Called by -recursivelyClearContents. Base class implements self.contents = nil, clearing any backing
330+
* @discussion Called by -recursivelyClearContents. Always called on main thread. Base class implements self.contents = nil, clearing any backing
331331
* store, for asynchronous regeneration when needed.
332332
*/
333333
- (void)clearContents ASDISPLAYNODE_REQUIRES_SUPER;

0 commit comments

Comments
 (0)