Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -42,14 +42,19 @@ public actual class ConcurrentMap<Key, Value> public actual constructor(

actual override fun isEmpty(): Boolean = delegate.isEmpty()

// Native returns detached snapshots instead of live views to avoid reintroducing
// ConcurrentModificationException when callers iterate outside the map lock.
// See https://github.com/ktorio/ktor/issues/5480.
actual override val entries: MutableSet<MutableMap.MutableEntry<Key, Value>>
get() = synchronized(lock) { delegate.entries }
get() = synchronized(lock) {
LinkedHashSet(delegate.entries.map { DetachedMutableEntry(it.key, it.value) })
}

actual override val keys: MutableSet<Key>
get() = synchronized(lock) { delegate.keys }
get() = synchronized(lock) { LinkedHashSet(delegate.keys) }

actual override val values: MutableCollection<Value>
get() = synchronized(lock) { delegate.values }
get() = synchronized(lock) { ArrayList(delegate.values) }
Comment thread
dapzthelegend marked this conversation as resolved.

actual override fun clear() {
synchronized(lock) {
Expand Down Expand Up @@ -82,3 +87,26 @@ public actual class ConcurrentMap<Key, Value> public actual constructor(

override fun toString(): String = "ConcurrentMapNative by $delegate"
}

private class DetachedMutableEntry<K, V>(
override val key: K,
value: V
) : MutableMap.MutableEntry<K, V> {
private var currentValue: V = value

override val value: V
get() = currentValue

override fun setValue(newValue: V): V {
val oldValue = currentValue
currentValue = newValue
return oldValue
}

override fun equals(other: Any?): Boolean {
if (other !is Map.Entry<*, *>) return false
return key == other.key && value == other.value
}

override fun hashCode(): Int = key.hashCode() xor value.hashCode()
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.util.collections

import kotlin.test.*

class ConcurrentMapNativeTest {

@Test
fun `entries returns snapshot`() {
val map = ConcurrentMap<Int, String>(initialCapacity = 4)
map[1] = "one"
map[2] = "two"

val entries = map.entries
map.remove(1)
map[3] = "three"

assertEquals(listOf(1 to "one", 2 to "two"), entries.map { it.key to it.value }.sortedBy { it.first })
}

@Test
fun `keys returns snapshot`() {
val map = ConcurrentMap<Int, String>(initialCapacity = 4)
map[1] = "one"
map[2] = "two"

val keys = map.keys
map.remove(1)
map[3] = "three"

assertEquals(setOf(1, 2), keys.toSet())
}

@Test
fun `values returns snapshot`() {
val map = ConcurrentMap<Int, String>(initialCapacity = 4)
map[1] = "one"
map[2] = "two"

val values = map.values
map.remove(1)
map[3] = "three"

assertEquals(listOf("one", "two"), values.sorted())
}

@Test
fun `entries snapshot supports value-based contains and remove`() {
val map = ConcurrentMap<Int, String>(initialCapacity = 4)
map[1] = "one"
map[2] = "two"

val entries = map.entries
assertTrue(entries.contains(TestMutableEntry(1, "one")))
assertTrue(entries.remove(TestMutableEntry(1, "one")))
assertFalse(entries.contains(TestMutableEntry(1, "one")))
}
}

private class TestMutableEntry<K, V>(
override val key: K,
override val value: V
) : MutableMap.MutableEntry<K, V> {
override fun setValue(newValue: V): V = value

override fun equals(other: Any?): Boolean {
if (other !is Map.Entry<*, *>) return false
return key == other.key && value == other.value
}

override fun hashCode(): Int = key.hashCode() xor value.hashCode()
}
Loading