title: Scala Grpc failed with error io.grpc.StatusRuntimeException: CANCELLED: Failed to read message
body: <p>I am trying to write a streaming service in Scala using GRPC. Towards this I wrote this proto file</p>
  
  <pre><code>syntax = "proto3";
  package com.abhi.grpc;
  
  message TimeRequest{}
  message TimeResponse {
      int64 currentTime = 1;
  }
  
  service Clock {
      rpc StreamTime(TimeRequest) returns (stream TimeResponse);
  }
  </code></pre>
  
  <p>This is my server side code</p>
  
  <pre><code>import com.abhi.grpc.clock.{ClockGrpc, TimeRequest, TimeResponse}
  import io.grpc.stub.StreamObserver
  import monix.execution.Scheduler
  import monix.execution.Scheduler.{global =&gt; scheduler}
  import scala.concurrent.duration._
  
  object ClockGrpcServer extends GrpcServer with App {
     val ssd = ClockGrpc.bindService(new ClockGRPC(), Scheduler.global)
     runServer(ssd, "Clock")
  }
  
  class ClockGRPC extends ClockGrpc.Clock {
     override def streamTime(request: TimeRequest, responseObserver: StreamObserver[TimeResponse]): Unit = {
        scheduler.scheduleWithFixedDelay(0.seconds, 3.seconds) {
           responseObserver.onNext(TimeResponse(System.currentTimeMillis))
        }
     }
  }
  </code></pre>
  
  <p>and this is my client</p>
  
  <pre><code>object ClockGrpcClient extends App {
     val channel = ManagedChannelBuilder.forAddress("localhost", 50051).usePlaintext(true).build()
     val stub = ClockGrpc.stub(channel)
     val observer = new StreamObserver[TimeResponse] {
        override def onError(t: Throwable): Unit = println(s"failed with error ${t}")
        override def onCompleted(): Unit = println("closing observer")
        override def onNext(value: TimeResponse): Unit = println(s"received time ${new DateTime(value)}")
     }
     stub.streamTime(TimeRequest(), observer)
     StdIn.readLine()
  }
  </code></pre>
  
  <p>When I run the server and the client. The server throws the following error as soon as it receives any message from the client</p>
  
  <pre><code>io.grpc.StatusRuntimeException: CANCELLED
          at io.grpc.Status.asRuntimeException(Status.java:534)
          at io.grpc.stub.ServerCalls$ServerCallStreamObserverImpl.onNext(ServerCalls.java:279)
          at com.abhi.ClockGRPC.$anonfun$streamTime$1(ClockGRPC.scala:22)
          at monix.execution.internal.RunnableAction.run(RunnableAction.scala:25)
          at monix.execution.schedulers.ReferenceScheduler$$anon$1.run(ReferenceScheduler.scala:45)
          at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:140)
          at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
          at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
          at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
          at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
  </code></pre>
  
  <p>I googled a little and found this article</p>
  
  <p><a href="https://blog.codecentric.de/en/2017/01/hello-grpc-scalapb/" rel="nofollow noreferrer">https://blog.codecentric.de/en/2017/01/hello-grpc-scalapb/</a></p>
  
  <p>based on this I changed my server to use the java.util scheduler</p>
  
  <pre><code>class ClockGRPC extends ClockGrpc.Clock {
     val scheduler = Executors.newSingleThreadScheduledExecutor()
     override def streamTime(request: TimeRequest, responseObserver: StreamObserver[TimeResponse]): Unit = {
        val tick = new Runnable {
           val counter = new AtomicInteger(10)
           def run() =
              if (counter.getAndDecrement() &gt;= 0) {
                 val currentTime = System.currentTimeMillis()
                 responseObserver.onNext(TimeResponse(currentTime))
              } else {
                 scheduler.shutdown()
                 responseObserver.onCompleted()
              }
        }
        scheduler.scheduleAtFixedRate(tick, 0l, 3000l, TimeUnit.SECONDS)
     }
  }
  </code></pre>
  
  <p>But I still get the CANCELLED error. So i cannot get the streaming example to work.</p>