Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Ensure LevelDB Iterator is closed #5458

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ class DuckDuckGoWebLocalStorageManager @Inject constructor(
Timber.d("WebLocalStorageManager: Matching regex: $matchingRegex")

databaseProvider.get().use { db ->
joshliebe marked this conversation as resolved.
Show resolved Hide resolved
val iterator = db.iterator()
iterator.seekToFirst()
db.iterator().use { iterator ->
iterator.seekToFirst()

while (iterator.hasNext()) {
val entry = iterator.next()
val key = String(entry.key, StandardCharsets.UTF_8)
while (iterator.hasNext()) {
val entry = iterator.next()
val key = String(entry.key, StandardCharsets.UTF_8)

if (!isAllowedKey(key)) {
db.delete(entry.key)
Timber.d("WebLocalStorageManager: Deleted key: $key")
if (!isAllowedKey(key)) {
db.delete(entry.key)
Timber.d("WebLocalStorageManager: Deleted key: $key")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,14 @@ class DuckDuckGoWebLocalStorageManagerTest {
}

@Test
fun whenClearWebLocalStorageThenDBIsClosed() {
fun whenClearWebLocalStorageThenDBAndIteratorIsClosed() {
whenever(mockDB.iterator()).thenReturn(mockIterator)
whenever(mockIterator.hasNext()).thenReturn(false)

testee.clearWebLocalStorage()

verify(mockDB).close()
verify(mockIterator).close()
}

private fun createMockDBEntry(key: ByteArray): MutableMap.MutableEntry<ByteArray, ByteArray> {
Expand Down
Loading