Fix NPE when EmittingSubscription is cancelled while emitting - #7352
Open
cthiebault wants to merge 1 commit into
Open
Fix NPE when EmittingSubscription is cancelled while emitting#7352cthiebault wants to merge 1 commit into
cthiebault wants to merge 1 commit into
Conversation
cancel() nulls downstreamSubscriber from another thread while doEmit() sits between its isCancelled check and the downstream signal, so the emitting thread throws a bare NullPointerException. The S3 parallel multipart download path drives that interleaving on every single-part object: ParallelMultipartDownloaderSubscriber.onSubscribe requests maxInFlightParts synchronously on the calling thread, and isMultipartObject() cancels from an SDK response thread as soon as part 1 comes back with a null partsCount. Make the field volatile and read it into a local per iteration, so a concurrent cancel cannot null it mid-emit. Signalling a subscriber that cancelled during supplier.get() is permitted by Reactive Streams rule 2.8. Fixes aws#7351
jencymaryjoseph
approved these changes
Sep 5, 2026
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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 and Context
Fixes #7351.
EmittingSubscriptionis annotated@ThreadSafe, butcancel()anddoEmit()race on the plain, non-volatiledownstreamSubscriberfield.cancel()nulls the field;doEmit()checksisCancelledat the top of its loop anddereferences
downstreamSubscriberseveral statements later, aftersupplier.get(). Acancel()landing in thatwindow makes the emitting thread throw a bare
NullPointerExceptionout ofSubscription.request(long):The S3 parallel multipart download path drives exactly that interleaving, on every single-part object:
ParallelMultipartDownloaderSubscriber.onSubscribecallssubscription.request(maxInFlightParts)synchronously on thecalling thread, and
isMultipartObject()callssubscription.cancel()from an SDK response thread as soon as part 1comes back with
partsCount == null. Whether the download survives is decided purely by whether the emitting threadfinished its iterations first. We hit it as an intermittent
S3TransferManager.downloadFilefailure that no caller canclassify, since the NPE arrives unwrapped.
request(long n)has the same hazard on itsn <= 0branch.Modifications
downstreamSubscriberis nowvolatile.doEmit()reads it into a local once per iteration and returns when it is null or the subscription is cancelled, thensignals through that local, so a concurrent
cancel()cannot null it between the check and the signal.n <= 0branch ofrequest(long)gets the same read-into-local guard.This can still deliver one
onNextto a subscriber that cancelled duringsupplier.get(), which Reactive Streams rule2.8 explicitly permits ("a Subscriber MUST be prepared to receive one or more onNext signals after having called
Subscription.cancel()"). The field is still nulled in
cancel(), so the reference is released for GC as before.Not changed here, but worth a look separately:
FileAsyncResponseTransformerPublisher.subscriberis also anon-volatile field nulled in
onCancel(), and two of its dereferences are unguarded — the "Content length header ismissing" branch of
IndividualFileTransformer.onResponse, andhandleError. Same defect class, same feature. Happy toadd it to this PR if you would rather have it in one change.
Testing
New
EmittingSubscriptionTestwith two cases, both deterministic (latch-driven, no sleeps):request_cancelledWhileEmitting_doesNotThrow— holds the emitting thread insidesupplier.get()while another threadcancels, i.e. the exact interleaving above. Asserts
request(2)does not throw and that at most oneonNextisdelivered.
request_negativeDemandAfterCancel_doesNotThrow— covers then <= 0branch after a cancel.Both fail on unmodified
masterwith the NPE above and pass with this change.Local runs on JDK 17 (Temurin 17.0.18):
Types of changes
Checklist
mvn installsucceedsscripts/new-changescript and following the instructions. Commit the new file created by the script in.changes/next-releasewith your changes.License