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

BugFix: Race condition between run() and timeout #189

Merged
merged 2 commits into from
Oct 2, 2013
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
40 changes: 25 additions & 15 deletions hystrix-core/src/main/java/com/netflix/hystrix/HystrixCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ public abstract class HystrixCommand<R> implements HystrixExecutable<R> {
private volatile ExecutionResult executionResult = ExecutionResult.EMPTY;

/* If this command executed and timed-out */
private final AtomicBoolean isCommandTimedOut = new AtomicBoolean(false);
private final AtomicReference<TimedOutStatus> isCommandTimedOut = new AtomicReference<TimedOutStatus>(TimedOutStatus.NOT_EXECUTED);
private final AtomicBoolean isExecutionComplete = new AtomicBoolean(false);
private final AtomicBoolean isExecutedInThread = new AtomicBoolean(false);

private static enum TimedOutStatus {NOT_EXECUTED, COMPLETED, TIMED_OUT};

private final HystrixCommandKey commandKey;
private final HystrixCommandGroupKey commandGroup;

Expand Down Expand Up @@ -1008,7 +1010,9 @@ public Subscription onSubscribe(final Observer<? super R> observer) {

@Override
public void tick() {
if (originalCommand.isCommandTimedOut.compareAndSet(false, true)) {
// if we can go from NOT_EXECUTED to TIMED_OUT then we do the timeout codepath
// otherwise it means we lost a race and the run() execution completed
if (originalCommand.isCommandTimedOut.compareAndSet(TimedOutStatus.NOT_EXECUTED, TimedOutStatus.TIMED_OUT)) {
// do fallback logic

// report timeout failure
Expand Down Expand Up @@ -1163,28 +1167,34 @@ public R call() throws Exception {

// execute the command
R r = executeCommand();
if (isCommandTimedOut.get()) {
// if we can go from NOT_EXECUTED to COMPLETED then we did not timeout
if (isCommandTimedOut.compareAndSet(TimedOutStatus.NOT_EXECUTED, TimedOutStatus.COMPLETED)) {
// give the hook an opportunity to modify it
r = executionHook.onComplete(_this, r);
// pass to the observer
observer.onNext(r);
// state changes before termination
preTerminationWork();
/* now complete which releases the consumer */
observer.onCompleted();
return r;
} else {
// this means we lost the race and the timeout logic has or is being executed
// state changes before termination
preTerminationWork();
return null;
}
// give the hook an opportunity to modify it
r = executionHook.onComplete(_this, r);
// pass to the observer
observer.onNext(r);
// state changes before termination
preTerminationWork();
/* now complete which releases the consumer */
observer.onCompleted();
return r;
} finally {
// pop this off the thread now that it's done
Hystrix.endCurrentThreadExecutingCommand();
}
} catch (Exception e) {
// state changes before termination
preTerminationWork();
observer.onError(e);
// if we can go from NOT_EXECUTED to COMPLETED then we did not timeout
if (isCommandTimedOut.compareAndSet(TimedOutStatus.NOT_EXECUTED, TimedOutStatus.COMPLETED)) {
observer.onError(e);
}
throw e;
}
}
Expand Down Expand Up @@ -1254,7 +1264,7 @@ private R executeCommand() {
long duration = System.currentTimeMillis() - startTime;
metrics.addCommandExecutionTime(duration);

if (isCommandTimedOut.get()) {
if (isCommandTimedOut.get() == TimedOutStatus.TIMED_OUT) {
// the command timed out in the wrapping thread so we will return immediately
// and not increment any of the counters below or other such logic
return null;
Expand Down Expand Up @@ -1296,7 +1306,7 @@ private R executeCommand() {
logger.warn("Error calling ExecutionHook.endRunFailure", hookException);
}

if (isCommandTimedOut.get()) {
if (isCommandTimedOut.get() == TimedOutStatus.TIMED_OUT) {
// http://jira/browse/API-4905 HystrixCommand: Error/Timeout Double-count if both occur
// this means we have already timed out then we don't count this error stat and we just return
// as this means the user-thread has already returned, we've already done fallback logic
Expand Down