Skip to content

Commit e38ca48

Browse files
Nikita Kovalqwwdfsad
Nikita Koval
authored andcommitted
Recover stack-trace in complete()
1 parent aecc03f commit e38ca48

File tree

1 file changed

+14
-6
lines changed
  • kotlinx-coroutines-core/common/src/selects

1 file changed

+14
-6
lines changed

kotlinx-coroutines-core/common/src/selects/Select.kt

+14-6
Original file line numberDiff line numberDiff line change
@@ -632,16 +632,24 @@ internal open class SelectImplementation<R> constructor(
632632
// is invoked at the very end.
633633
selectedClause.invokeBlock(blockArgument)
634634
} else {
635+
// TAIL-CALL OPTIMIZATION: the `suspend`
636+
// function is invoked at the very end.
637+
// However, internally this `suspend` function
638+
// constructs a state machine to recover a
639+
// possible stack-trace.
640+
selectedClause.invokeBlockRecoveringException(blockArgument)
641+
}
642+
}
643+
644+
private suspend fun ClauseData<R>.invokeBlockRecoveringException(blockArgument: Any?): R =
645+
try {
635646
// In the debug mode, we need to recover
636647
// the stack-trace of possible exception.
637648
// Thus, the tail-call optimization cannot be applied.
638-
try {
639-
selectedClause.invokeBlock(blockArgument)
640-
} catch (e: Throwable) {
641-
throw recoverStackTrace(e)
642-
}
649+
invokeBlock(blockArgument)
650+
} catch (e: Throwable) {
651+
throw recoverStackTrace(e)
643652
}
644-
}
645653

646654
/**
647655
* Invokes all [DisposableHandle]-s provided via

0 commit comments

Comments
 (0)