Fix intermittent NPE race in EmittingSubscription when cancel() nulls subscriber during emit - #7354
Merged
Merged
Conversation
joviegas
force-pushed
the
joviegas/emit-subscriber-fix
branch
from
September 4, 2026 23:18
0909157 to
52ccd12
Compare
joviegas
force-pushed
the
joviegas/emit-subscriber-fix
branch
from
September 4, 2026 23:36
52ccd12 to
e8d7872
Compare
zoewangg
reviewed
Sep 8, 2026
| * {@link Subscription#request(long)}. It tracks the outstandingDemand that has not yet been fulfilled and used a Supplier | ||
| * passed to it to create the object it needs to emit. | ||
| * <p> | ||
| * Thread safe: {@link #request(long)} and {@link #cancel()} may run concurrently. The subscriber reference is |
Contributor
There was a problem hiding this comment.
request and cancel shouldnt run concurrently though. The subscriber should make sure they run synchronously
Contributor
Author
There was a problem hiding this comment.
Good call out. This java doc comment can be misleading. So removing it
| private final Path path; | ||
| private final FileTransformerConfiguration initialConfig; | ||
| private Subscriber<?> subscriber; | ||
| private volatile Subscriber<?> subscriber; |
Contributor
There was a problem hiding this comment.
- Should we handle additional onNext signals after cancel in
ParallelMultipartDownloaderSubscriberper Reactive Streams rule 2.8? - It seems we should also add lock when we invoke subscription.cancel(); in
ParallelMultipartDownloaderSubscriber
Contributor
Author
There was a problem hiding this comment.
Should we handle additional onNext
Extra onNext signals are already no-op'd here:
Each onNext gets a unique increasing part number from nextPart(),and for a single-part object totalParts is 1, so anything past part 1 falls outside
currentPartNum <= totalParts and is dropped. Do you think we need any additional handling beyond this check?
Contributor
Author
There was a problem hiding this comment.
It seems we should also add lock
Added lock to cancel and request now
12 tasks
zoewangg
approved these changes
Sep 9, 2026
|
This pull request has been closed and the conversation has been locked. Comments on closed PRs are hard for our team to see. If you need more assistance, please open a new issue that references this one. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #7351
Issue
A customer downloading a small object with a multipart-enabled async S3 client (
getObject/downloadFilewithmultipartEnabled(true)) intermittently gets aNullPointerExceptioninstead of their object. The same call succeeds on retry, and the object is small enough to need no multipart at all. It shows up more on fast endpoints and busy hosts, so it hits local testing and CI hardest.Root cause
EmittingSubscription.downstreamSubscriberis a non-volatile field shared by two threads. For a single-part object, the caller thread runs the emit loop while the SDK response-completion thread, seeingpartsCount == null, callscancel(), which sets the field to null. The loop checksisCancelledbeforeonNext, but the cancel can land in the gap between that check and the dereference, soonNextis called on a null reference and throws.Fix
EmittingSubscription: makedownstreamSubscriberfinaland stop nulling it incancel(). Cancellation is already signaled by the existingisCancelledflag, so the reference no longer needs to be cleared. A final reference cannot be observed as null.FileAsyncResponseTransformerPublisher(its only user, same defect one frame away): this class is thePublisher, so Reactive Streams rule 3.13 requires it to drop the subscriber on cancel. It keeps nulling, but the field is nowvolatileand every read snapshots it into a local and null-checks before use.ParallelMultipartDownloaderSubscriber: the subscriber calledrequest()(line 201) andcancel()(line 355) from two threads without synchronizing them, which Reactive Streams rule 2.7 requires. Both calls now hold the existingsubscriptionLock.Testing
EmittingSubscriptionTest: demand/emit, non-positive demand, cancel, and a concurrency test racingcancel()against the emit loop. The concurrency test fails on the old code and passes on the fix.FileAsyncResponseTransformerPublisherunit tests and its Reactive Streams TCK suite pass, including the rule 3.13 test that verifies the publisher drops the subscriber on cancel (the behavior the publisher change touches).ParallelMultipartDownloaderSubscriberTCK pass with the lock change.Testing
EmittingSubscriptionTest: demand/emit, non-positive demand, cancel, and a concurrency test racingcancel()against the emit loop. The concurrency test fails on the old code and passes on the fix.FileAsyncResponseTransformerPublisherunit tests and its Reactive Streams TCK suite pass, including the rule 3.13 test that verifies the publisher drops the subscriber on cancel (the behavior the publisher change touches).Screenshots (if appropriate)
Types of changes
License