Skip to content

Commit 2d235ef

Browse files
committed
Added catch for Throwable to Thread.start
* Catching InternalError and OutOfMemoryError didn't cover all possible first time calls to Thread.start based on the following reports - #917 (comment) - #917 (comment) * Added catching for Error, IllegalThreadStateException (should never fire on first start call but just in case), RuntimeException, and Throwable. * These are new catches and re-throws with the same logic as PR #975
1 parent bda77aa commit 2d235ef

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,36 @@ private synchronized void startDelayedWrite() {
167167
* Future: We may want to use this strategy for all Thread.start calls.
168168
* And limit thread usages, using mostly coroutines instead.
169169
*/
170-
private VirtualMachineError threadStartError;
170+
171+
private Error threadStartError;
172+
private RuntimeException threadStartRuntimeException;
173+
private Throwable threadStartThrowable;
174+
171175
private void startThread() {
172176
if (threadStartError != null)
173177
throw threadStartError;
174178

179+
if (threadStartRuntimeException != null)
180+
throw threadStartRuntimeException;
181+
182+
// Ideally we would just throw threadStartThrowable here,
183+
// however we can't without adding throws to this method's signature.
184+
// If this is done we would have to add throws all the way up the stack to
185+
// to public SDK methods which can't be done at this time nor would
186+
// "throws Throwable" be a good public signature.
187+
if (threadStartThrowable != null) {
188+
// The following lines turn a Throwable into a RuntimeException
189+
// to workaround the the throwable signature noted above.
190+
RuntimeException exception = new RuntimeException(
191+
threadStartThrowable.getClass().getName() +
192+
": " +
193+
threadStartThrowable.getMessage(),
194+
threadStartThrowable
195+
);
196+
exception.setStackTrace(threadStartThrowable.getStackTrace());
197+
throw exception;
198+
}
199+
175200
try {
176201
start();
177202
} catch (InternalError e) {
@@ -184,6 +209,27 @@ private void startThread() {
184209
threadStartError = e;
185210
throw e;
186211
}
212+
catch (Error t) {
213+
// Possibly some other error we didn't expect Thread.start() to throw
214+
threadStartError = t;
215+
throw t;
216+
}
217+
catch (IllegalThreadStateException e) {
218+
// Adds the state of the thread to IllegalThreadStateException to provide more details
219+
IllegalThreadStateException exception =
220+
new IllegalThreadStateException("Thread has state: " + this.getState());
221+
exception.setStackTrace(e.getStackTrace());
222+
threadStartRuntimeException = exception;
223+
throw exception;
224+
}
225+
catch (RuntimeException e) {
226+
threadStartRuntimeException = e;
227+
throw e;
228+
}
229+
catch (Throwable t) {
230+
threadStartThrowable = t;
231+
throw t;
232+
}
187233
}
188234

189235
private Runnable getNewRunnable() {

0 commit comments

Comments
 (0)