@@ -151,6 +151,15 @@ private synchronized void startDelayedWrite() {
151
151
scheduleFlushToDiskJob ();
152
152
}
153
153
154
+ private boolean threadStartCalled ;
155
+ private void startThread () {
156
+ if (threadStartCalled )
157
+ return ;
158
+
159
+ start ();
160
+ threadStartCalled = true ;
161
+ }
162
+
154
163
private synchronized void scheduleFlushToDiskJob () {
155
164
// Could be null if looper thread just started
156
165
if (mHandler == null )
@@ -171,92 +180,6 @@ public void run() {
171
180
mHandler .postDelayed (runnable , delay );
172
181
}
173
182
174
- /**
175
- * Attempt to start the thread used by this HandlerThread
176
- * It may fail due to the following:
177
- * - InternalError - Thread starting during runtime shutdown
178
- * - OutOfMemoryError - pthread_create (####KB stack) failed: Try again
179
- * If it does throw we want to catch then save the error and rethrow
180
- * If startThread is called a 2nd time we will rethrowing the first exception
181
- * - Otherwise Thread.start will just throw IllegalThreadStateException
182
- * Normally this catch and rethrow would not be needed however somewhere in this
183
- * SDK code base or a consumer of this SDK is catching first exception and
184
- * silently ignoring it. Resulting in the true causing of a crash being unknown.
185
- * See https://github.com/OneSignal/OneSignal-Android-SDK/issues/917#issuecomment-600472976
186
- *
187
- * Future: We may want to use this strategy for all Thread.start calls.
188
- * And limit thread usages, using mostly coroutines instead.
189
- */
190
-
191
- private Error threadStartError ;
192
- private RuntimeException threadStartRuntimeException ;
193
- private Throwable threadStartThrowable ;
194
-
195
- private boolean threadStartCalled ;
196
- private void startThread () {
197
- if (threadStartCalled )
198
- return ;
199
-
200
- if (threadStartError != null )
201
- throw threadStartError ;
202
-
203
- if (threadStartRuntimeException != null )
204
- throw threadStartRuntimeException ;
205
-
206
- // Ideally we would just throw threadStartThrowable here,
207
- // however we can't without adding throws to this method's signature.
208
- // If this is done we would have to add throws all the way up the stack to
209
- // to public SDK methods which can't be done at this time nor would
210
- // "throws Throwable" be a good public signature.
211
- if (threadStartThrowable != null ) {
212
- // The following lines turn a Throwable into a RuntimeException
213
- // to workaround the the throwable signature noted above.
214
- RuntimeException exception = new RuntimeException (
215
- threadStartThrowable .getClass ().getName () +
216
- ": " +
217
- threadStartThrowable .getMessage (),
218
- threadStartThrowable
219
- );
220
- exception .setStackTrace (threadStartThrowable .getStackTrace ());
221
- throw exception ;
222
- }
223
-
224
- try {
225
- start ();
226
- threadStartCalled = true ;
227
- } catch (InternalError e ) {
228
- // Thread starting during runtime shutdown
229
- threadStartError = e ;
230
- throw e ;
231
- }
232
- catch (OutOfMemoryError e ) {
233
- // pthread_create (1040KB stack) failed: Try again
234
- threadStartError = e ;
235
- throw e ;
236
- }
237
- catch (Error t ) {
238
- // Possibly some other error we didn't expect Thread.start() to throw
239
- threadStartError = t ;
240
- throw t ;
241
- }
242
- catch (IllegalThreadStateException e ) {
243
- // Adds the state of the thread to IllegalThreadStateException to provide more details
244
- IllegalThreadStateException exception =
245
- new IllegalThreadStateException ("Thread has state: " + this .getState ());
246
- exception .setStackTrace (e .getStackTrace ());
247
- threadStartRuntimeException = exception ;
248
- throw exception ;
249
- }
250
- catch (RuntimeException e ) {
251
- threadStartRuntimeException = e ;
252
- throw e ;
253
- }
254
- catch (Throwable t ) {
255
- threadStartThrowable = t ;
256
- throw t ;
257
- }
258
- }
259
-
260
183
private void flushBufferToDisk () {
261
184
for (String pref : prefsToApply .keySet ()) {
262
185
SharedPreferences prefsToWrite = getSharedPrefsByName (pref );
0 commit comments