in tchannel-core/src/main/java/com/uber/tchannel/api/TFuture.java [126:153]
public void addListener(final Runnable listener, Executor exec) {
listenerCount.incrementAndGet();
// this is the current span of whomever is adding the listener - preserve it for the invocation of the latter
final Span span = tracingContext != null && tracingContext.hasSpan() ? tracingContext.currentSpan() : null;
super.addListener(new Runnable() {
@Override
public void run() {
try {
try {
pushSpan(span);
listener.run();
} finally {
popSpan(span, listener);
}
} finally {
// if ALL listeners were given a CHANCE to run, then release response regardless:
// a) whether listener actually ran or not;
// b) whether tracing was pushed/popped or not;
int remainingListeners = listenerCount.decrementAndGet();
if (remainingListeners <= 0) {
if (response != null) {
response.release();
}
}
}
}
}, exec);
}