Skip to content

Execute GrpcCalls in the provided scope - #3569

Open
oldergod wants to merge 3 commits into
masterfrom
bquenaudon.2026-04-15.scope
Open

oldergod wants to merge 3 commits into
masterfrom
bquenaudon.2026-04-15.scope

Conversation

@oldergod

@oldergod oldergod commented Apr 15, 2026 •

Copy link
Copy Markdown
Member

Fixes #3559.

GrpcStreamingCall { ... } now launches its function on the dispatcher of the scope that executeIn(scope) receives.
Before, it always used Dispatchers.IO and ignored the dispatcher of the scope.
If the function does blocking work, wrap that work in withContext(Dispatchers.IO).

executeBlocking() and the deprecated execute() still launch the function on Dispatchers.IO.
Callers block threads on the returned streams, so these calls must not use Dispatchers.Default.
The deprecation hint on execute() now suggests executeIn(GlobalScope + Dispatchers.IO), which keeps this behavior.

Release note: GrpcStreamingCall { ... }, GrpcClientStreamingCall { ... }, and GrpcServerStreamingCall { ... } now run their function on the dispatcher of the scope given to executeIn(scope), instead of Dispatchers.IO.
Wrap blocking work in withContext(Dispatchers.IO).
executeBlocking() and execute() still use Dispatchers.IO.

@oldergod
oldergod requested a review from swankjesse April 15, 2026 08:16
Comment on lines 21 to 194
import com.squareup.wire.internal.asGrpcServerStreamingCall
import java.util.concurrent.atomic.AtomicBoolean
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.onClosed
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import okio.IOException
import okio.Timeout

/**
* Returns a new instance of [GrpcCall] that can be used for a single call to
* [execute][GrpcCall.execute], [executeBlocking][GrpcCall.executeBlocking], or
* [enqueue][GrpcCall.enqueue].
*
* The returned instance executes [function] synchronously on the calling thread, regardless of
* which blocking mode is used. If [function] throws, the thrown exception will be wrapped in an
* [IOException].
*
* This method is useful when implementing the interfaces that are generated by Wire:
*
* ```
* override fun GetFeature(): GrpcCall<Point, Feature> {
* return GrpcCall<Point, Feature> { request ->
* return@GrpcCall lookupNearestFeature(request.latitude, request.longitude)
* }
* }
* ```
*
* It is succinct when used in an expression function:
*
* ```
* override fun GetFeature() = GrpcCall<Point, Feature> { request ->
* return@GrpcCall lookupNearestFeature(request.latitude, request.longitude)
* }
* ```
*/
@JvmName("grpcCall")
fun <S : Any, R : Any> GrpcCall(function: (S) -> R): GrpcCall<S, R> {
return object : GrpcCall<S, R> {
private var canceled = AtomicBoolean()
private var executed = AtomicBoolean()
override var requestMetadata: Map<String, String> = mapOf()
override val responseMetadata: Map<String, String>? = null

@Suppress("UNCHECKED_CAST")
override val method: GrpcMethod<S, R>
get() = GrpcMethod(
path = "/wire/AnonymousEndpoint",
requestAdapter = ProtoAdapter.BYTES,
responseAdapter = ProtoAdapter.BYTES,
) as GrpcMethod<S, R>

override val timeout: Timeout = Timeout.NONE

override fun cancel() {
canceled.set(true)
}

override fun isCanceled() = canceled.get()

override fun isExecuted() = executed.get()

override fun enqueue(request: S, callback: GrpcCall.Callback<S, R>) {
val response = try {
executeBlocking(request)
} catch (exception: IOException) {
callback.onFailure(this, exception)
return
}
callback.onSuccess(this, response)
}

override suspend fun execute(request: S): R = executeBlocking(request)

override fun executeBlocking(request: S): R {
check(executed.compareAndSet(false, true)) { "already executed" }
if (canceled.get()) throw IOException("canceled")
try {
return function(request)
} catch (e: IOException) {
throw e
} catch (e: Exception) {
throw IOException("call failed: $e", e)
}
}

override fun clone() = GrpcCall(function).also { it.requestMetadata += requestMetadata }
}
}

/**
* Returns a new instance of [GrpcStreamingCall] that can be used for a single call to
* [executeIn][GrpcStreamingCall.executeIn] or [executeBlocking][GrpcStreamingCall.executeBlocking].
*
* The returned instance launches [function] on [Dispatchers.IO]. The function must close the
* The returned instance launches [function] in the provided scope. The function must close the
* [SendChannel] when it has no more messages to transmit. If [function] throws, both channels will
* be closed using the thrown exception as a cause.
*
* This method is useful when implementing the interfaces that are generated by Wire:
*
* ```
* override fun RouteChat(): GrpcStreamingCall<RouteNote, RouteNote> {
* return GrpcStreamingCall { requests, responses ->
* requests.consumeEach { note ->
* responses.send(translateNote(note))
* }
* responses.close()
* }
* }
* ```
*
* It is succinct when used in an expression function:
*
* ```
* override fun RouteChat() = GrpcStreamingCall<RouteNote, RouteNote> { requests, responses ->
* requests.consumeEach { note ->
* responses.send(translateNote(note))
* }
* responses.close()
* }
* ```
*/
@JvmName("grpcStreamingCall")
fun <S : Any, R : Any> GrpcStreamingCall(
function: suspend (ReceiveChannel<S>, SendChannel<R>) -> Unit,
): GrpcStreamingCall<S, R> {
return object : GrpcStreamingCall<S, R> {
@Suppress("UNCHECKED_CAST")
override val method: GrpcMethod<S, R>
get() = GrpcMethod(
path = "/wire/AnonymousEndpoint",
requestAdapter = ProtoAdapter.BYTES,
responseAdapter = ProtoAdapter.BYTES,
) as GrpcMethod<S, R>

private var canceled = AtomicBoolean()
private var executed = AtomicBoolean()
override var requestMetadata: Map<String, String> = mapOf()
override val responseMetadata: Map<String, String>? = null
private val requestChannel = Channel<S>(1)
private val responseChannel = Channel<R>(1)

override val timeout: Timeout = Timeout.NONE

override fun cancel() {
if (canceled.compareAndSet(false, true)) {
requestChannel.cancel()
responseChannel.cancel()
}
}

override fun isCanceled() = canceled.get()

override fun isExecuted() = executed.get()

@Deprecated(
"Provide a scope, preferably not GlobalScope",
replaceWith = ReplaceWith("executeIn(GlobalScope)", "kotlinx.coroutines.GlobalScope"),
level = DeprecationLevel.WARNING,
)
@Suppress("OPT_IN_USAGE")
override fun execute(): Pair<SendChannel<S>, ReceiveChannel<R>> = executeIn(GlobalScope)

override fun executeIn(scope: CoroutineScope): Pair<SendChannel<S>, ReceiveChannel<R>> {
check(executed.compareAndSet(false, true)) { "already executed" }

val job = scope.launch(Dispatchers.IO) {
val job = scope.launch {
try {
function(requestChannel, responseChannel)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we add a parameter to GrpcStreamingCall to not break existing callers?

 fun <S : Any, R : Any> GrpcStreamingCall(
+  dispatcher: CoroutineContext = Dispatchers.IO,
   function: suspend (ReceiveChannel<S>, SendChannel<R>) -> Unit,
 ): GrpcStreamingCall<S, R> {

and

-      val job = scope.launch(Dispatchers.IO) {
+      val job = scope.launch(dispatcher) {

@oldergod
oldergod marked this pull request as ready for review April 15, 2026 08:18
@oldergod
oldergod requested a review from JakeWharton April 20, 2026 12:28

@JakeWharton JakeWharton left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no context one whether the changes in your comment are appropriate or not. Never used gRPC stuff in Wire.

@oldergod
oldergod force-pushed the bquenaudon.2026-04-15.scope branch 2 times, most recently from a163514 to 1ad4c83 Compare November 2, 2026 09:55
GlobalScope has no dispatcher, so executeBlocking() and execute() ran the function on Dispatchers.Default.
Blocking functions could starve Default threads and deadlock callers.
Only executeIn() now uses the dispatcher of the provided scope.
executeIn(GlobalScope) now runs the helper's function on Dispatchers.Default.
The suggested replacement keeps the Dispatchers.IO behavior of execute().
@oldergod
oldergod force-pushed the bquenaudon.2026-04-15.scope branch from 7c9f729 to 3b897ee Compare November 9, 2026 17:35

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GrpcStreamingCall() factory's executeIn() hardcodes Dispatchers.IO instead of using an incoming scope

2 participants