@@ -188,10 +188,10 @@ internal class DispatchedContinuation<in T>(
188
188
override fun resumeWith (result : Result <T >) {
189
189
val context = continuation.context
190
190
val state = result.toState()
191
- if (dispatcher.isDispatchNeeded (context)) {
191
+ if (dispatcher.safeIsDispatchNeeded (context)) {
192
192
_state = state
193
193
resumeMode = MODE_ATOMIC
194
- dispatchWithExceptionHandling (context)
194
+ dispatcher.safeDispatch (context, this )
195
195
} else {
196
196
executeUnconfined(state, MODE_ATOMIC ) {
197
197
withCoroutineContext(this .context, countOrElement) {
@@ -209,10 +209,10 @@ internal class DispatchedContinuation<in T>(
209
209
noinline onCancellation : ((cause: Throwable ) -> Unit )?
210
210
) {
211
211
val state = result.toState(onCancellation)
212
- if (dispatcher.isDispatchNeeded (context)) {
212
+ if (dispatcher.safeIsDispatchNeeded (context)) {
213
213
_state = state
214
214
resumeMode = MODE_CANCELLABLE
215
- dispatchWithExceptionHandling (context)
215
+ dispatcher.safeDispatch (context, this )
216
216
} else {
217
217
executeUnconfined(state, MODE_CANCELLABLE ) {
218
218
if (! resumeCancelled(state)) {
@@ -260,13 +260,21 @@ internal class DispatchedContinuation<in T>(
260
260
261
261
override fun toString (): String =
262
262
" DispatchedContinuation[$dispatcher , ${continuation.toDebugString()} ]"
263
+ }
263
264
264
- private fun dispatchWithExceptionHandling (context : CoroutineContext ) {
265
- try {
266
- dispatcher.dispatch(context, this )
267
- } catch (e: Throwable ) {
268
- throw DispatchException (e, dispatcher, context)
269
- }
265
+ internal fun CoroutineDispatcher.safeDispatch (context : CoroutineContext , runnable : Runnable ) {
266
+ try {
267
+ dispatch(context, runnable)
268
+ } catch (e: Throwable ) {
269
+ throw DispatchException (e, this , context)
270
+ }
271
+ }
272
+
273
+ internal fun CoroutineDispatcher.safeIsDispatchNeeded (context : CoroutineContext ): Boolean {
274
+ try {
275
+ return isDispatchNeeded(context)
276
+ } catch (e: Throwable ) {
277
+ throw DispatchException (e, this , context)
270
278
}
271
279
}
272
280
0 commit comments