[Coroutines Interop] Dispose the handle returned by Job.invokeOnCompletion when Rx subscription is disposed.#470
Merged
Merged
Conversation
…letion` when Rx subscription is disposed. The `Completable` `emitter` is captured by the `Job.invokeOnCompletion` closure (`CompletionHandler`), so if we don't dispose the handler when Rx subscription is disposed, we are holding onto a reference to `CompletableEmitter` until the job is completed (and the handler is called and disposed). Calling the handler after the subscription is disposed is not an issue, because `CompletableEmitter.onError` and `CompletableEmitter.onComplete` checks for disposal before taking any action -- but it stills presents a potential memory leak.
Job.invokeOnCompletion when Rx subscription is disposed.
ZacSweers
approved these changes
Jul 22, 2023
ZacSweers
pushed a commit
that referenced
this pull request
Jul 22, 2023
…letion` when Rx subscription is disposed. (#470) The `Completable` `emitter` is captured by the `Job.invokeOnCompletion` closure (`CompletionHandler`), so if we don't dispose the handler when Rx subscription is disposed, we are holding onto a reference to `CompletableEmitter` until the job is completed (and the handler is called and disposed). Calling the handler after the subscription is disposed is not an issue, because `CompletableEmitter.onError` and `CompletableEmitter.onComplete` checks for disposal before taking any action -- but it stills presents a potential memory leak.
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.
The
Completableemitteris captured by theJob.invokeOnCompletionclosure,CompletionHandler.In the current implementation, we do not dispose the DisposableHandle to free the
CompletionHandlerwhen the Rx subscription is disposed. Thus, we are holding onto a reference toCompletableEmitteruntil the coroutine job is completed (and theCompletionHandleris called and disposed).Calling the handler after the subscription is disposed is not an issue, because
CompletableEmitter.onErrorandCompletableEmitter.onCompletechecks for disposal before taking any action.But holding onto the Rx
emitteruntil coroutineJobis cancelled will imply in memory leak whenever theJoboutlives the Rx subscription.