-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix possibly huge memory leak caused by long lived CancellableManager…
…Provider (#213) * Fix forever growing cancellable Manager A long lived CancellableManagerProvider could easily end up with very many “cancelled” Cancellable in it’s internal CancellableManager because this is only cleared when the Provider itself is cancelled. This can cause huge memory leaks in some cases, for example in the DebounceProcessor Since there is only ever one “not cancelled” CancellableManager provided by the CancellableManagerProvider, there is actually no need to keep the previously provided, now cancelled, CancellableManager. We now simply use the internalCancellableManagerRef. Also took the oportunity to make cancelPreviousAndCreate hopefully atomic be exposing and using getAndSet on AtomicReference * Add test for “already cancelled” contract and implement * remove another hole * Update public API declaration
- Loading branch information
1 parent
3a98901
commit 9b298e5
Showing
5 changed files
with
32 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
21 changes: 15 additions & 6 deletions
21
...src/commonMain/kotlin/com/mirego/trikot/streams/cancellable/CancellableManagerProvider.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
package com.mirego.trikot.streams.cancellable | ||
|
||
import com.mirego.trikot.foundation.concurrent.AtomicReference | ||
import com.mirego.trikot.foundation.concurrent.dispatchQueue.SynchronousSerialQueue | ||
|
||
class CancellableManagerProvider : Cancellable { | ||
private val cancellableManager = CancellableManager() | ||
private val serialQueue = SynchronousSerialQueue() | ||
private val internalCancellableManagerRef = AtomicReference(CancellableManager()) | ||
private val isCancelled = AtomicReference(false) | ||
|
||
fun cancelPreviousAndCreate(): CancellableManager { | ||
internalCancellableManagerRef.value.cancel() | ||
return CancellableManager().also { | ||
internalCancellableManagerRef.setOrThrow(internalCancellableManagerRef.value, it) | ||
cancellableManager.add(it) | ||
return CancellableManager().also { cancellableManager -> | ||
internalCancellableManagerRef.getAndSet(cancellableManager).cancel() | ||
serialQueue.dispatch { | ||
if (isCancelled.value) { | ||
cancellableManager.cancel() | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun cancel() { | ||
cancellableManager.cancel() | ||
serialQueue.dispatch { | ||
if (isCancelled.compareAndSet(isCancelled.value, true)) { | ||
internalCancellableManagerRef.value.cancel() | ||
} | ||
} | ||
} | ||
} |
This file contains 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