Skip to content

Commit 864cbc4

Browse files
authored
fix: ignore illegal state exception on addShutdownHook w/c happens when shutdown sequence has begun (#248)
1 parent 598a421 commit 864cbc4

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

core/src/main/java/com/amplitude/core/platform/EventPipeline.kt

+13-7
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,19 @@ class EventPipeline(
171171

172172
private fun registerShutdownHook() {
173173
// close the stream if the app shuts down
174-
Runtime.getRuntime().addShutdownHook(
175-
object : Thread() {
176-
override fun run() {
177-
this@EventPipeline.stop()
178-
}
179-
},
180-
)
174+
try {
175+
Runtime.getRuntime().addShutdownHook(
176+
object : Thread() {
177+
override fun run() {
178+
this@EventPipeline.stop()
179+
}
180+
},
181+
)
182+
} catch (e: IllegalStateException) {
183+
// Once the shutdown sequence has begun it is impossible to register a shutdown hook,
184+
// so we just ignore the IllegalStateException that's thrown.
185+
// https://developer.android.com/reference/java/lang/Runtime#addShutdownHook(java.lang.Thread)
186+
}
181187
}
182188
}
183189

0 commit comments

Comments
 (0)