Skip to content

Commit

Permalink
Merge pull request #1237 from bugsnag/PLAT-6493/fix-thread-capture-npe
Browse files Browse the repository at this point in the history
Prevent rare NPE when capturing thread traces
  • Loading branch information
fractalwrench authored May 10, 2021
2 parents 20b449e + 5908551 commit 20df8b1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
* Fix bug that terminated the app when multiple ANRs occur
[#1235](https://github.com/bugsnag/bugsnag-android/pull/1235)

* Prevent rare NPE when capturing thread traces
[#1237](https://github.com/bugsnag/bugsnag-android/pull/1237)

## 5.9.1 (2021-04-22)

### Bug fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ internal class ThreadState @JvmOverloads constructor(
val currentThreadId = currentThread.id
return stackTraces.keys
.sortedBy { it.id }
.map {
val stacktrace = Stacktrace.stacktraceFromJavaTrace(stackTraces[it]!!, projectPackages, logger)
val errorThread = it.id == currentThreadId
Thread(it.id, it.name, ThreadType.ANDROID, errorThread, stacktrace, logger)
.mapNotNull { thread ->
val trace = stackTraces[thread]

if (trace != null) {
val stacktrace = Stacktrace.stacktraceFromJavaTrace(trace, projectPackages, logger)
val errorThread = thread.id == currentThreadId
Thread(thread.id, thread.name, ThreadType.ANDROID, errorThread, stacktrace, logger)
} else {
null
}
}.toMutableList()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.bugsnag.android

import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test
import java.lang.RuntimeException
import java.lang.Thread
import java.util.Collections

class ThreadStateMissingTraceTest {

@Test
fun handleNullThreadTraces() {
val currentThread = Thread.currentThread()
val traces = Thread.getAllStackTraces()

// make all stacktraces null
traces.keys.forEach {
traces[it] = null
}

val state = ThreadState(
RuntimeException(),
false,
ThreadSendPolicy.ALWAYS,
Collections.emptyList(),
NoopLogger,
currentThread,
traces
)
assertNotNull(state)
assertTrue(state.threads.isEmpty())
}
}

0 comments on commit 20df8b1

Please sign in to comment.