Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Coroutines Interop] Dispose the handle returned by Job.invokeOnCompletion when Rx subscription is disposed. #470

Merged
merged 1 commit into from
Jul 22, 2023

Conversation

psteiger
Copy link
Contributor

@psteiger psteiger commented Jul 22, 2023

The Completable emitter is captured by the Job.invokeOnCompletion closure, CompletionHandler.

In the current implementation, we do not dispose the DisposableHandle to free the CompletionHandler when the Rx subscription is disposed. Thus, we are holding onto a reference to CompletableEmitter until the coroutine job is completed (and the CompletionHandler 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 holding onto the Rx emitter until coroutine Job is cancelled will imply in memory leak whenever the Job outlives the Rx subscription.

…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.
@ZacSweers ZacSweers changed the title [Coroutines Interop] Dispose the handle returned by `Job.invokeOnComp… [Coroutines Interop] Dispose the handle returned by Job.invokeOnCompletion when Rx subscription is disposed. Jul 22, 2023
@ZacSweers ZacSweers merged commit 08a8fc3 into uber:main 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.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants