Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tasks returned by grpc stub methods are not idempotent #41

Open
Jasper-M opened this issue Sep 2, 2022 · 0 comments · May be fixed by #42
Open

Tasks returned by grpc stub methods are not idempotent #41

Jasper-M opened this issue Sep 2, 2022 · 0 comments · May be fixed by #42

Comments

@Jasper-M
Copy link

Jasper-M commented Sep 2, 2022

We have an interceptor that works like this:

case class CredentialsSetter(credentials: CallCredentials) extends ClientInterceptor() {
  override def interceptCall[ReqT, RespT](
      method: MethodDescriptor[ReqT, RespT],
      callOptions: CallOptions,
      next: Channel
    ): ClientCall[ReqT, RespT] = {
    next.newCall(method, callOptions.withCallCredentials(credentials))
  }
}

case class StaticTokenCredentials(token: String) extends CallCredentials {
  override def applyRequestMetadata(
      requestInfo: CallCredentials.RequestInfo,
      appExecutor: Executor,
      applier: CallCredentials.MetadataApplier
    ): Unit = {
    val requestHeaders = new Metadata().withToken(token)
    applier.apply(requestHeaders)
  }
  override def thisUsesUnstableApi(): Unit = {}
}

Now when we have a stub service with this interceptor applied to its channel:

(service.someGrpcCall(Empty()) <* Task.sleep(1.minute))
  .flatMap(response => Task(println(s"received $response")))
  .restartUntil(_ => false)
  .runToFuture

Every subsequent call to someGrpcCall will contain one extra token header until the header grows so big that you get an exception Header size exceeded max allowed size (8192).

The reason is that here in grpc-java the origHeaders is a mutable object and our requestHeaders above are getting merged into it. And in the stub code we have ClientCall(channel, MyServiceApi.METHOD_SOME_GRPC_CALL, opts).unaryToUnaryCall(request, metadata). The metadata here gets reused on every subsequent call, and every time the MetadataApplier mutates it.

I think the stub implementation should be something like this to make it safe:

Task.defer{
  val metadataCopy = new Metadata().merge(metadata)
  ClientCall(channel, MyServiceApi.METHOD_SOME_GRPC_CALL, opts).unaryToUnaryCall(request, metadataCopy)
}
@Jasper-M Jasper-M changed the title Grpc stub methods are not idempotent Tasks returned by grpc stub methods are not idempotent Sep 2, 2022
Jasper-M added a commit to Jasper-M/monix-grpc that referenced this issue Sep 5, 2022
Jasper-M added a commit to Jasper-M/monix-grpc that referenced this issue Sep 5, 2022
@Jasper-M Jasper-M linked a pull request Sep 5, 2022 that will close this issue
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 a pull request may close this issue.

1 participant