census: fix NPE in calling recordFinishedAttempt() [backport v1.42.x] #8731
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport of #8706
Fix the NPE as shown in the following stacktrace:
The NPE can happen when
ClientCall.Listener.onClose()
andStatsTraceContext.streamClosed()
(orClientStreamListener.closed()
) are invoked concurrently in different threads. Note thatCensusStatsModule$CallAttemptsTracerFactory.attemptEnded()
in the above stack trace would observecallEnded==true
in such a race condition.The following are the possible scenarios that the race between
ClientCall.Listener.onClose()
andClientStreamListener.closed()
can happen:ClientCall.Listener
may be closed earlier than the stream listener. (This is the case of the above stack trace, in which the inbound end-of-stream is received observingcallEnded==true
. Even if nothing inbound is received, there is still a chance that the NPE can happen.)setStream(realStream)
is not called yet, when deadline exceeded. (This has little chance to happen, only for the very first RPC on the channel.)In deadline-exceeded cases, the shorter the deadline is, the more likely the race can happen.