Skip to content

Commit

Permalink
Sync LayoutStateFuture refCount
Browse files Browse the repository at this point in the history
Summary: increasing/decreasing refCount on LayoutStateFuture without proper sync was leading to crashes for cancelable LayoutStateFutures

Reviewed By: muraziz

Differential Revision: D14870132

fbshipit-source-id: 60b52db00f6a6224de5b46f2436408a48f018cd1
  • Loading branch information
mihaelao authored and facebook-github-bot committed Apr 11, 2019
1 parent a92018a commit 550381d
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions litho-core/src/main/java/com/facebook/litho/ComponentTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

import static android.os.Process.THREAD_PRIORITY_DEFAULT;
import static com.facebook.litho.ComponentLifecycle.StateUpdate;
import static com.facebook.litho.HandlerInstrumenter.instrumentLithoHandler;
import static com.facebook.litho.FrameworkLogEvents.EVENT_LAYOUT_CALCULATE;
import static com.facebook.litho.FrameworkLogEvents.EVENT_PRE_ALLOCATE_MOUNT_CONTENT;
import static com.facebook.litho.FrameworkLogEvents.PARAM_ATTRIBUTION;
import static com.facebook.litho.FrameworkLogEvents.PARAM_IS_BACKGROUND_LAYOUT;
import static com.facebook.litho.FrameworkLogEvents.PARAM_ROOT_COMPONENT;
import static com.facebook.litho.FrameworkLogEvents.PARAM_TREE_DIFF_ENABLED;
import static com.facebook.litho.HandlerInstrumenter.instrumentLithoHandler;
import static com.facebook.litho.LayoutState.CalculateLayoutSource;
import static com.facebook.litho.ThreadUtils.assertHoldsLock;
import static com.facebook.litho.ThreadUtils.assertMainThread;
Expand Down Expand Up @@ -2208,7 +2208,7 @@ class LayoutStateFuture {
@Nullable private final LayoutState previousLayoutState;
@Nullable private final TreeProps treeProps;
private final FutureTask<LayoutState> futureTask;
private volatile int refCount;
private final AtomicInteger refCount = new AtomicInteger(0);
private final boolean isCancelable;

@GuardedBy("LayoutStateFuture.this")
Expand Down Expand Up @@ -2299,9 +2299,9 @@ boolean isReleased() {
}

void unregisterForResponse(boolean waitingOnMainThread) {
refCount--;
final int newRefCount = refCount.decrementAndGet();

if (refCount < 0) {
if (newRefCount < 0) {
throw new IllegalStateException("LayoutStateFuture ref count is below 0");
}

Expand All @@ -2311,14 +2311,14 @@ void unregisterForResponse(boolean waitingOnMainThread) {
}

void registerForResponse(boolean waitingOnMainThread) {
refCount++;
refCount.incrementAndGet();
if (waitingOnMainThread) {
this.isBlockingMainThread = true;
}
}

public int getWaitingCount() {
return refCount;
return refCount.get();
}

boolean canBeCancelled() {
Expand Down

0 comments on commit 550381d

Please sign in to comment.