zio-grpc
zio-grpc copied to clipboard
Python client receives out-of-order stream
Hi all, I have a server implemented in Scala using zio-grpc, and a client in Python.
I found that sometimes the stream received by the client is out of order. For example, the sequence of data sent by the server is [1, 2, 3, 4, 5]
, but those received by the client are [1, 2, 4, 3, 5]
. Though I'm new to grpc, I don't think this is an expected behavior.
Here is a minimal example: https://drive.google.com/file/d/1Eew2sOhjSt2tCBEupE1glo6PALYkB0t1/view
import io.grpc.StatusException
import scalapb.zio_grpc.ServerMain
import scalapb.zio_grpc.ServiceList
import zio.{ZIO, stream}
import zio.stream.ZStream
import example.demo._
class DemoService extends ZioDemo.Demo {
def foo(request: FooRequest): stream.Stream[StatusException, FooResponse] = {
ZStream.fromIterable(getStream)
}
private def getStream: List[FooResponse] = List.range(0, 100).map(i => FooResponse(i.toString))
}
object Main extends ServerMain {
override def port: Int = 8981
override def services: ServiceList[Any] =
ServiceList.add(new DemoService)
}
import grpc
import demo_pb2
import demo_pb2_grpc
def get_streaming_response(stub):
responses = list(stub.Foo(demo_pb2.FooRequest(data="hello")))
if any(r.data != str(i) for i, r in enumerate(responses)):
print("Expected:", list(range(len(responses))))
print("Got: ", [int(r.data) for r in responses])
print("Diff: ", [i for i, r in enumerate(responses) if r.data != str(i)])
raise ValueError("Out of order response")
if __name__ == "__main__":
with grpc.insecure_channel("localhost:8981") as channel:
demo_stub = demo_pb2_grpc.DemoStub(channel)
n = 10000
for _ in range(n):
get_streaming_response(demo_stub)
#### output looks like:
# Expected: [0, 1, 2, ...]
# Got: [0, 1, 2, ...]
# Diff: [x, y, ...]
According to my testing results, this happens rarely (with n=10000
this will almost always happen), and usually only one or two pairs of adjacent items are swapped.
I'm not sure if this problem is with zio-grpc or Pyhton's grpcio, below is some relevant information: OS: Ubuntu 22.04 Python version: 3.10.13 grpcio version: 1.51.1 (couldn't find 1.50.1) grpcio-tools version: 1.51.1 grpc version: 4.25.3 Scala version: 2.13.13 grpc-netty version: 1.50.1