hyperium / tonic

A native gRPC client & server implementation with async/await support.
https://docs.rs/tonic
MIT License
9.82k stars 999 forks source link

serve_with_incoming_shutdown doesn't work properly when TcpListener::from_std is used #1424

Open ik1ne opened 1 year ago

ik1ne commented 1 year ago

Bug Report

Version

v0.9.2, 0d86e360ab45779770ca150c8487fe7940c299a9

Platform

macOS 13.4.1(22F82) Darwin (hostname) 22.5.0 Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000 arm64

Description

Using TcpListener::from_std prevents tonic::transport::server::serve_with_incoming_shutdown from working properly.

Full code(based on examples/src/streaming/server.rs): https://pastebin.com/4CPZ0QZs

Code snippet:

Server::builder()
    ...
    .serve_with_incoming_shutdown(TcpListenerStream::new(TcpListener::from_std(listener)?), (async ctrl_c handler))

This code works properly when there's no client connection. (i.e. the server shuts down when I press ctrl_c) However, after a client tries to connect, the async signal handler doesn't get the executor, preventing the server from shutting down when I press ctrl_c. Replacing the std TcpListener with tokio version, or changing the async ctrl_c into busy wait solves the issue.

I'm opening this because I'm not sure if it's intended(i.e. I'm supposed to not use std TcpListener) or not.

magaya-marksinclair commented 2 months ago

Not a tonic dev, but I don't think that's a bug. std TcpListener isn't async so can't have any integration with tokio. Generally you shouldn't be using the networking code from std with tokio.