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

Prevent ConcurrentModificationException thrown from Metadata class #935

Merged
merged 1 commit into from
Sep 17, 2020
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## TBD

### Bug fixes

* Prevent ConcurrentModificationException thrown from Metadata class
[#935](https://github.com/bugsnag/bugsnag-android/pull/935)

## 5.1.0 (2020-09-08)

### Enhancements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package com.bugsnag.android

import java.io.IOException
import java.util.HashMap
import java.util.HashSet
import java.util.concurrent.ConcurrentHashMap

Expand All @@ -14,7 +13,7 @@ import java.util.concurrent.ConcurrentHashMap
* Diagnostic information is presented on your Bugsnag dashboard in tabs.
*/
internal data class Metadata @JvmOverloads constructor(
internal val store: MutableMap<String, Any> = ConcurrentHashMap(),
internal val store: ConcurrentHashMap<String, Any> = ConcurrentHashMap(),
val jsonStreamer: ObjectJsonStreamer = ObjectJsonStreamer(),
val redactedKeys: Set<String> = jsonStreamer.redactedKeys
) : JsonStream.Streamable, MetadataAware {
Expand Down Expand Up @@ -79,8 +78,8 @@ internal data class Metadata @JvmOverloads constructor(
}
}

fun toMap(): Map<String, Any> {
val hashMap = HashMap(store)
fun toMap(): ConcurrentHashMap<String, Any> {
val hashMap = ConcurrentHashMap(store)

// deep copy each section
store.entries.forEach {
Expand All @@ -106,7 +105,7 @@ internal data class Metadata @JvmOverloads constructor(
return newMeta
}

internal fun mergeMaps(data: List<Map<String, Any>>): MutableMap<String, Any> {
internal fun mergeMaps(data: List<Map<String, Any>>): ConcurrentHashMap<String, Any> {
val keys = data.flatMap { it.keys }.toSet()
val result = ConcurrentHashMap<String, Any>()

Expand Down Expand Up @@ -144,7 +143,7 @@ internal data class Metadata @JvmOverloads constructor(
}

fun copy() = this.copy(
store = toMap().toMutableMap(),
store = toMap(),
jsonStreamer = jsonStreamer,
redactedKeys = redactedKeys
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.bugsnag.android

import org.junit.Assert.assertNotNull
import org.junit.Test
import java.util.concurrent.Executors

internal class MetadataConcurrentModificationTest {

@Test()
fun testHandlesConcurrentModification() {
val metadata = Metadata().copy()
val executor = Executors.newSingleThreadExecutor()

repeat(100) { count ->
assertNotNull(metadata.toMap())
executor.execute {
metadata.store["$count"] = count
}
}
}
}
6 changes: 3 additions & 3 deletions bugsnag-android-core/src/test/resources/event_redaction.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"app": {
"password": "[REDACTED]"
},
"device": {
"baz": {
"password": "[REDACTED]"
},
"baz": {
"device": {
"password": "[REDACTED]"
}
},
Expand Down Expand Up @@ -48,4 +48,4 @@
}
],
"threads": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"app": {
"foo": 55
},
"device": {
"bar": true
},
"wham": {
"some_key": "Avalue"
},
"device": {
"bar": true
}
},
"severity": "warning",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.bugsnag.android;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

class MetadataDeserializer implements MapDeserializer<Metadata> {
@Override
public Metadata deserialize(Map<String, Object> map) {
return new Metadata(map);
ConcurrentHashMap<String, Object> store = new ConcurrentHashMap<>(map);
return new Metadata(store);
}
}