Avoid deadlock consuming from blocking Iterator #2204
Merged
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.
Motivation:
The blocking streaming APIs allow for specifying a Iterator
as input which is consumed by ServiceTalk. ServiceTalk assumes
it can consume in batches from the same thread in order to make
progress. However if the Iterator
next()
method is dependenton external input we may deadlock:
PublisherAsBlockingIterable.onSubscribe(..)
does arequest(16)
FromIterablePublisher.request(..)
loops callinghasNext()
andnext()
until demand is exhausted. Each loop iteration callssubscriber.onNext(next())
.FromIterablePublisher.request(..)
isalso responsible for draining the queue in
PublisherAsBlockingIterable
.This means that no data will be sent downstream until
FromIterablePublisher
exits the loop. This maybe perceived as“dead lock” because no data is sent even if the
Iterable
provides anelement.
Modifications:
FromIterablePublisher
andFromBlockingIterablePublisher
check tounwrap from
PublisherAsBlockingIterable
if possible to directlyaccess the underlying
Publisher
. This skips thee consumptionvia thee
Iterable
API and avoids the dead lock loop.Result:
No more dead lock when using HTTP / gRPC API conversions and providing
Iterable
that is dependent upon external events to make progress.