tonic icon indicating copy to clipboard operation
tonic copied to clipboard

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

Open ik1ne opened this issue 2 years ago • 2 comments

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.

ik1ne avatar Jun 27 '23 06:06 ik1ne

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.

magaya-marksinclair avatar Jul 08 '24 21:07 magaya-marksinclair

This might be happening with from_std initialized UnixListener too. Wondering the difference because UnixListener::bind is doing the similar thing with from_std.

sorah avatar Jul 08 '25 15:07 sorah