Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASImageNode] Fix a threading issue which can cause a display completion block to never be executed #1148

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Source/ASImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,15 @@ - (void)displayDidFinish
[super displayDidFinish];

__instanceLock__.lock();
void (^displayCompletionBlock)(BOOL canceled) = _displayCompletionBlock;
UIImage *image = _image;
void (^displayCompletionBlock)(BOOL canceled) = _displayCompletionBlock;
BOOL shouldPerformDisplayCompletionBlock = (image && displayCompletionBlock);

// Clear the ivar now. The block is retained and will be executed shortly.
if (shouldPerformDisplayCompletionBlock) {
_displayCompletionBlock = nil;
}

BOOL hasDebugLabel = (_debugLabelNode != nil);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since many of the calls in the debug label section require the lock anyway (like self.bounds), should we just continue holding the lock until after that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a comment below stating that we don't care about locking in the debug section. I don't remember the reasoning behind that (locking violation somehow?), but let's revisit it in another PR.

__instanceLock__.unlock();

Expand All @@ -556,13 +563,8 @@ - (void)displayDidFinish
}

// If we've got a block to perform after displaying, do it.
if (image && displayCompletionBlock) {

if (shouldPerformDisplayCompletionBlock) {
displayCompletionBlock(NO);

__instanceLock__.lock();
_displayCompletionBlock = nil;
__instanceLock__.unlock();
}
}

Expand Down