@@ -134,7 +134,7 @@ public static class WritePrefHandlerThread extends HandlerThread {
134
134
135
135
synchronized void startDelayedWrite () {
136
136
if (mHandler == null ) {
137
- start ();
137
+ startThread ();
138
138
mHandler = new Handler (getLooper ());
139
139
}
140
140
@@ -149,6 +149,41 @@ synchronized void startDelayedWrite() {
149
149
}
150
150
}
151
151
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
+
152
187
private Runnable getNewRunnable () {
153
188
return new Runnable () {
154
189
@ Override
0 commit comments