Skip to content

Commit b6f98eb

Browse files
committed
startDelayedWrite catches and re-throws original
* startDelayedWrite now catches and re-throws original error thrown when Thread.start is called. * See code comments in this commit for more details.
1 parent 4f1c754 commit b6f98eb

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalPrefs.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static class WritePrefHandlerThread extends HandlerThread {
134134

135135
synchronized void startDelayedWrite() {
136136
if (mHandler == null) {
137-
start();
137+
startThread();
138138
mHandler = new Handler(getLooper());
139139
}
140140

@@ -149,6 +149,41 @@ synchronized void startDelayedWrite() {
149149
}
150150
}
151151

152+
/**
153+
* Attempt to start the thread used by this HandlerThread
154+
* It may fail due to the following:
155+
* - InternalError - Thread starting during runtime shutdown
156+
* - OutOfMemoryError - pthread_create (####KB stack) failed: Try again
157+
* If it does throw we want to catch then save the error and rethrow
158+
* If startThread is called a 2nd time we will rethrowing the first exception
159+
* - Otherwise Thread.start will just throw IllegalThreadStateException
160+
* Normally this catch and rethrow would not be needed however somewhere in this
161+
* SDK code base or a consumer of this SDK is catching first exception and
162+
* silently ignoring it. Resulting in the true causing of a crash being unknown.
163+
* See https://github.com/OneSignal/OneSignal-Android-SDK/issues/917#issuecomment-600472976
164+
*
165+
* Future: We may want to use this strategy for all Thread.start calls.
166+
* And limit thread usages, using mostly coroutines instead.
167+
*/
168+
private VirtualMachineError threadStartError;
169+
private void startThread() {
170+
if (threadStartError != null)
171+
throw threadStartError;
172+
173+
try {
174+
start();
175+
} catch (InternalError e) {
176+
// Thread starting during runtime shutdown
177+
threadStartError = e;
178+
throw e;
179+
}
180+
catch (OutOfMemoryError e) {
181+
// pthread_create (1040KB stack) failed: Try again
182+
threadStartError = e;
183+
throw e;
184+
}
185+
}
186+
152187
private Runnable getNewRunnable() {
153188
return new Runnable() {
154189
@Override

0 commit comments

Comments
 (0)