Repository navigation
Conversation
oldergod
commented
Apr 15, 2026
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) |
Member
Author
There was a problem hiding this comment.
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
marked this pull request as ready for review
April 15, 2026 08:18
JakeWharton
approved these changes
Apr 20, 2026
JakeWharton
left a comment
Collaborator
There was a problem hiding this comment.
I have no context one whether the changes in your comment are appropriate or not. Never used gRPC stuff in Wire.
oldergod
force-pushed
the
bquenaudon.2026-04-15.scope
branch
2 times, most recently
from
November 2, 2026 09:55
a163514 to
1ad4c83
Compare
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
force-pushed
the
bquenaudon.2026-04-15.scope
branch
from
November 9, 2026 17:35
7c9f729 to
3b897ee
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3559.
GrpcStreamingCall { ... }now launches its function on the dispatcher of the scope thatexecuteIn(scope)receives.Before, it always used
Dispatchers.IOand ignored the dispatcher of the scope.If the function does blocking work, wrap that work in
withContext(Dispatchers.IO).executeBlocking()and the deprecatedexecute()still launch the function onDispatchers.IO.Callers block threads on the returned streams, so these calls must not use
Dispatchers.Default.The deprecation hint on
execute()now suggestsexecuteIn(GlobalScope + Dispatchers.IO), which keeps this behavior.Release note:
GrpcStreamingCall { ... },GrpcClientStreamingCall { ... }, andGrpcServerStreamingCall { ... }now run their function on the dispatcher of the scope given toexecuteIn(scope), instead ofDispatchers.IO.Wrap blocking work in
withContext(Dispatchers.IO).executeBlocking()andexecute()still useDispatchers.IO.