Fix residual leak in downloadDirectory by skipping file downloads after cancellation.#6983
Merged
RanVaknin merged 4 commits intoJun 2, 2026
Merged
Conversation
downloadDirectory by skipping file downloads after cancellation.
zoewangg
reviewed
May 22, 2026
zoewangg
approved these changes
Jun 1, 2026
… cancelled to prevent directory recreation
…andler already ran
b7c3ca9 to
a1aad27
Compare
|
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.
In #6875, we addressed canceling in flight transfers when a directory download is cancelled. That fix reduced the leaked files per orphaned directory from thousands to single digits in stress test. However there was a residual leak for downloads that were already dispatched to
doDownloadSingleFile(the method that creates the destination directory and initiates the file download) before the cancellation signal propagated.When cancellation occurs, thread A calls
subscription.cancel()to stop new items from being delivered. Meanwhile, thread B has already picked up an item fromonNextand is insidedoDownloadSingleFile. Since it entered before thread A's cancellation took effect, it creates the destination directory and starts a download into it.This PR adds an
isCompletedExceptionally()guard at the top ofdoDownloadSingleFileso that any thread that entered the method before cancellation took effect will return early before touching the filesystem.The PR also adds a
isCompletedExceptionally()check inAsyncBufferingSubscriber.onNext, immediately after adding the future torequestsInFlight. WhenreturnFuturecompletes exceptionally, the cancel handler iteratesrequestsInFlightand cancels every future in it. That iteration only happens once. If it already ran before the future was added to the set, the future was missed. The additional check ensures we cancel it in that case.The tests:
Updated
downloadDirectory_cancel_shouldCancelAllFutures- now waits for both downloads to start before cancelling. The original test calledcancel()immediately afterdownloadDirectory(), implicitly assuming all downloads would always start regardless of cancellation state. With the newisCompletedExceptionally()guard, that assumption is not always true since cancel can now prevent downloads from starting. The test's intent is unchanged (in flight futures get cancelled), we just make sure both futures are actually in flight before cancelling. (Sanity check tested this with a repeated test suite 1000 times to make sure its deterministic)Added
downloadDirectory_cancelledFuture_shouldNotCreateDirectories- The test cancels the directory download future, then delivers an S3 object through the publisher. Asserts that the destination subdirectory was never created on the filesystem. Without the fix,doDownloadSingleFilewould callcreateParentDirectoriesIfNeeded()and create the directory despite the cancellation.Added
returnFutureCancelledDuringOnNext_shouldCancelInFlightFutureinAsyncBufferingSubscriberTest- Forces the race outcome by triggering cancellation insideconsumer.apply(), guaranteeing the cancel handler iterates an empty set. Asserts the future gets cancelled via the post add check.