@@ -121,9 +121,17 @@ private class LazyDeferredCoroutine<T>(
121121 * Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns
122122 * the result.
123123 *
124- * This function immediately applies dispatcher from the new context, shifting execution of the block into the
125- * different thread inside the block, and back when it completes.
126- * The specified [context] is added onto the current coroutine context for the execution of the block.
124+ * The resulting context for the [block] is derived by merging the current [coroutineContext] with the
125+ * specified [context] using `coroutineContext + context` (see [CoroutineContext.plus]).
126+ * This suspending function is cancellable. It immediately checks for cancellation of
127+ * the resulting context and throws [CancellationException] if it is not [active][CoroutineContext.isActive].
128+ *
129+ * This function uses dispatcher from the new context, shifting execution of the [block] into the
130+ * different thread if a new dispatcher is specified, and back to the original dispatcher
131+ * when it completes. Note, that the result of `withContext` invocation is
132+ * dispatched into the original context in a cancellable way, which means that if the original [coroutineContext],
133+ * in which `withContext` was invoked, is cancelled by the time its dispatcher starts to execute the code,
134+ * it discards the result of `withContext` and throws [CancellationException].
127135 */
128136public suspend fun <T > withContext (
129137 context : CoroutineContext ,
@@ -132,6 +140,8 @@ public suspend fun <T> withContext(
132140 // compute new context
133141 val oldContext = uCont.context
134142 val newContext = oldContext + context
143+ // always check for cancellation of new context
144+ newContext.checkCompletion()
135145 // FAST PATH #1 -- new context is the same as the old one
136146 if (newContext == = oldContext) {
137147 val coroutine = ScopeCoroutine (newContext, uCont) // MODE_DIRECT
0 commit comments