Skip to content

Commit 08f48c0

Browse files
author
Matt Jacobs
committed
Add try-catch to all hook executions
1 parent 2693292 commit 08f48c0

File tree

1 file changed

+73
-25
lines changed

1 file changed

+73
-25
lines changed

hystrix-core/src/main/java/com/netflix/hystrix/AbstractCommand.java

+73-25
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,11 @@ public Observable<R> toObservable() {
362362
/* mark that we received this response from cache */
363363
metrics.markResponseFromCache();
364364
isExecutionComplete.set(true);
365-
executionHook.onCacheHit(this);
365+
try {
366+
executionHook.onCacheHit(this);
367+
} catch (Throwable hookEx) {
368+
logger.warn("Error calling HystrixCommandExecutionHook.onCacheHit", hookEx);
369+
}
366370
return new CachedObservableResponse<R>((CachedObservableOriginal<R>) fromCache, this);
367371
}
368372
}
@@ -379,7 +383,11 @@ public void call(Subscriber<? super R> observer) {
379383
metrics.incrementConcurrentExecutionCount();
380384

381385
// mark that we're starting execution on the ExecutionHook
382-
executionHook.onStart(_this);
386+
try {
387+
executionHook.onStart(_this);
388+
} catch (Throwable hookEx) {
389+
logger.warn("Error calling HystrixCommandExecutionHook.onStart", hookEx);
390+
}
383391

384392
/* determine if we're allowed to execute */
385393
if (circuitBreaker.allowRequest()) {
@@ -511,9 +519,21 @@ public void call(Subscriber<? super R> s) {
511519
s.onError(new RuntimeException("timed out before executing run()"));
512520
} else {
513521
// not timed out so execute
514-
executionHook.onThreadStart(_self);
515-
executionHook.onRunStart(_self);
516-
executionHook.onExecutionStart(_self);
522+
try {
523+
executionHook.onThreadStart(_self);
524+
} catch (Throwable hookEx) {
525+
logger.warn("Error calling HystrixCommandExecutionHook.onThreadStart", hookEx);
526+
}
527+
try {
528+
executionHook.onRunStart(_self);
529+
} catch (Throwable hookEx) {
530+
logger.warn("Error calling HystrixCommandExecutionHook.onRunStart", hookEx);
531+
}
532+
try {
533+
executionHook.onExecutionStart(_self);
534+
} catch (Throwable hookEx) {
535+
logger.warn("Error calling HystrixCommandExecutionHook.onExecutionStart", hookEx);
536+
}
517537
threadPool.markThreadExecution();
518538
// store the command that is being run
519539
endCurrentThreadExecutingCommand.set(Hystrix.startCurrentThreadExecutingCommand(getCommandKey()));
@@ -530,8 +550,16 @@ public Boolean call() {
530550
}));
531551
} else {
532552
// semaphore isolated
533-
executionHook.onRunStart(_self);
534-
executionHook.onExecutionStart(_self);
553+
try {
554+
executionHook.onRunStart(_self);
555+
} catch (Throwable hookEx) {
556+
logger.warn("Error calling HystrixCommandExecutionHook.onRunStart", hookEx);
557+
}
558+
try {
559+
executionHook.onExecutionStart(_self);
560+
} catch (Throwable hookEx) {
561+
logger.warn("Error calling HystrixCommandExecutionHook.onExecutionStart", hookEx);
562+
}
535563
// store the command that is being run
536564
endCurrentThreadExecutingCommand.set(Hystrix.startCurrentThreadExecutingCommand(getCommandKey()));
537565
run = getExecutionObservableWithLifecycle(); //the getExecutionObservableWithLifecycle method already wraps sync exceptions, so no need to catch here
@@ -603,8 +631,8 @@ public Observable<R> call(Throwable t) {
603631
} else {
604632
logger.warn("ExecutionHook.onError returned an exception that was not an instance of HystrixBadRequestException so will be ignored.", decorated);
605633
}
606-
} catch (Exception hookException) {
607-
logger.warn("Error calling ExecutionHook.onError", hookException);
634+
} catch (Exception hookEx) {
635+
logger.warn("Error calling ExecutionHook.onError", hookEx);
608636
}
609637
/*
610638
* HystrixBadRequestException is treated differently and allowed to propagate without any stats tracking or fallback logic
@@ -665,7 +693,7 @@ private Observable<R> getExecutionObservableWithLifecycle() {
665693
// so we catch it here and turn it into Observable.error
666694
userObservable = Observable.error(ex);
667695
}
668-
return userObservable .lift(new ExecutionHookApplication(_self))
696+
return userObservable.lift(new ExecutionHookApplication(_self))
669697
.lift(new DeprecatedOnRunHookApplication(_self))
670698
.doOnTerminate(new Action0() {
671699
@Override
@@ -725,7 +753,11 @@ private Observable<R> getFallbackOrThrowException(final HystrixEventType eventTy
725753
// acquire a permit
726754
if (fallbackSemaphore.tryAcquire()) {
727755
if (isFallbackUserSupplied(this)) {
728-
executionHook.onFallbackStart(this);
756+
try {
757+
executionHook.onFallbackStart(this);
758+
} catch (Throwable hookEx) {
759+
logger.warn("Error calling HystrixCommandExecutionHook.onFallbackStart", hookEx);
760+
}
729761
}
730762

731763
try {
@@ -883,7 +915,11 @@ protected void handleThreadEnd() {
883915
}
884916
if (isExecutedInThread.get()) {
885917
threadPool.markThreadCompletion();
886-
executionHook.onThreadComplete(this);
918+
try {
919+
executionHook.onThreadComplete(this);
920+
} catch (Throwable hookEx) {
921+
logger.warn("Error calling HystrixCommandExecutionHook.onThreadComplete", hookEx);
922+
}
887923
}
888924
}
889925

@@ -1297,7 +1333,11 @@ public Subscriber<? super R> call(final Subscriber<? super R> subscriber) {
12971333
return new Subscriber<R>(subscriber) {
12981334
@Override
12991335
public void onCompleted() {
1300-
executionHook.onSuccess(cmd);
1336+
try {
1337+
executionHook.onSuccess(cmd);
1338+
} catch (Throwable hookEx) {
1339+
logger.warn("Error calling HystrixCommandExecutionHook.onSuccess", hookEx);
1340+
}
13011341
subscriber.onCompleted();
13021342
}
13031343

@@ -1328,7 +1368,11 @@ public Subscriber<? super R> call(final Subscriber<? super R> subscriber) {
13281368
return new Subscriber<R>(subscriber) {
13291369
@Override
13301370
public void onCompleted() {
1331-
executionHook.onExecutionSuccess(cmd);
1371+
try {
1372+
executionHook.onExecutionSuccess(cmd);
1373+
} catch (Throwable hookEx) {
1374+
logger.warn("Error calling HystrixCommandExecutionHook.onExecutionSuccess", hookEx);
1375+
}
13321376
subscriber.onCompleted();
13331377
}
13341378

@@ -1359,7 +1403,11 @@ public Subscriber<? super R> call(final Subscriber<? super R> subscriber) {
13591403
return new Subscriber<R>(subscriber) {
13601404
@Override
13611405
public void onCompleted() {
1362-
executionHook.onFallbackSuccess(cmd);
1406+
try {
1407+
executionHook.onFallbackSuccess(cmd);
1408+
} catch (Throwable hookEx) {
1409+
logger.warn("Error calling HystrixCommandExecutionHook.onFallbackSuccess", hookEx);
1410+
}
13631411
subscriber.onCompleted();
13641412
}
13651413

@@ -1405,7 +1453,7 @@ public void onNext(R r) {
14051453
R wrappedValue = executionHook.onComplete(cmd, r);
14061454
subscriber.onNext(wrappedValue);
14071455
} catch (Throwable hookEx) {
1408-
logger.warn("Error calling ExecutionHook.onComplete", hookEx);
1456+
logger.warn("Error calling HystrixCommandExecutionHook.onComplete", hookEx);
14091457
subscriber.onNext(r);
14101458
}
14111459
}
@@ -1437,7 +1485,7 @@ public void onError(Throwable t) {
14371485
Exception wrappedEx = executionHook.onRunError(cmd, e);
14381486
subscriber.onError(wrappedEx);
14391487
} catch (Throwable hookEx) {
1440-
logger.warn("Error calling ExecutionHook.onRunError", hookEx);
1488+
logger.warn("Error calling HystrixCommandExecutionHook.onRunError", hookEx);
14411489
subscriber.onError(e);
14421490
}
14431491
}
@@ -1448,7 +1496,7 @@ public void onNext(R r) {
14481496
R wrappedValue = executionHook.onRunSuccess(cmd, r);
14491497
subscriber.onNext(wrappedValue);
14501498
} catch (Throwable hookEx) {
1451-
logger.warn("Error calling ExecutionHook.onRunSuccess", hookEx);
1499+
logger.warn("Error calling HystrixCommandExecutionHook.onRunSuccess", hookEx);
14521500
subscriber.onNext(r);
14531501
}
14541502
}
@@ -1485,7 +1533,7 @@ public void onNext(R r) {
14851533
R wrappedValue = executionHook.onFallbackSuccess(cmd, r);
14861534
subscriber.onNext(wrappedValue);
14871535
} catch (Throwable hookEx) {
1488-
logger.warn("Error calling ExecutionHook.onFallbackSuccess", hookEx);
1536+
logger.warn("Error calling HystrixCommandExecutionHook.onFallbackSuccess", hookEx);
14891537
subscriber.onNext(r);
14901538
}
14911539
}
@@ -1498,7 +1546,7 @@ private Exception wrapWithOnExecutionErrorHook(Throwable t) {
14981546
try {
14991547
return executionHook.onExecutionError(this, e);
15001548
} catch (Throwable hookEx) {
1501-
logger.warn("Error calling ExecutionHook.onExecutionError", hookEx);
1549+
logger.warn("Error calling HystrixCommandExecutionHook.onExecutionError", hookEx);
15021550
return e;
15031551
}
15041552
}
@@ -1512,7 +1560,7 @@ private Exception wrapWithOnFallbackErrorHook(Throwable t) {
15121560
return e;
15131561
}
15141562
} catch (Throwable hookEx) {
1515-
logger.warn("Error calling ExecutionHook.onFallbackError", hookEx);
1563+
logger.warn("Error calling HystrixCommandExecutionHook.onFallbackError", hookEx);
15161564
return e;
15171565
}
15181566
}
@@ -1522,7 +1570,7 @@ private Exception wrapWithOnErrorHook(FailureType failureType, Throwable t) {
15221570
try {
15231571
return executionHook.onError(this, failureType, e);
15241572
} catch (Throwable hookEx) {
1525-
logger.warn("Error calling ExecutionHook.onError", hookEx);
1573+
logger.warn("Error calling HystrixCommandExecutionHook.onError", hookEx);
15261574
return e;
15271575
}
15281576
}
@@ -1531,7 +1579,7 @@ private R wrapWithOnExecutionEmitHook(R r) {
15311579
try {
15321580
return executionHook.onExecutionEmit(this, r);
15331581
} catch (Throwable hookEx) {
1534-
logger.warn("Error calling ExecutionHook.onExecutionEmit", hookEx);
1582+
logger.warn("Error calling HystrixCommandExecutionHook.onExecutionEmit", hookEx);
15351583
return r;
15361584
}
15371585
}
@@ -1540,7 +1588,7 @@ private R wrapWithOnFallbackEmitHook(R r) {
15401588
try {
15411589
return executionHook.onFallbackEmit(this, r);
15421590
} catch (Throwable hookEx) {
1543-
logger.warn("Error calling ExecutionHook.onFallbackEmit", hookEx);
1591+
logger.warn("Error calling HystrixCommandExecutionHook.onFallbackEmit", hookEx);
15441592
return r;
15451593
}
15461594
}
@@ -1549,7 +1597,7 @@ private R wrapWithOnEmitHook(R r) {
15491597
try {
15501598
return executionHook.onEmit(this, r);
15511599
} catch (Throwable hookEx) {
1552-
logger.warn("Error calling ExecutionHook.onEmit", hookEx);
1600+
logger.warn("Error calling HystrixCommandExecutionHook.onEmit", hookEx);
15531601
return r;
15541602
}
15551603
}

0 commit comments

Comments
 (0)