首页 > 解决方案 > 即使客户端认为连接已经建立,TcpListener 也会卡在 accept() 中

问题描述

let addr: SocketAddr = self.listen_bind.parse().unwrap();
let mut listener = TcpListener::bind(&addr).await?;
info!("Nightfort listening on {}", addr);
loop {
    info!("debug1");
    match listener.accept().await {
        Ok((stream, addr)) => {
            info!("debug2");
            let watcher = self.watcher.clone();
            info!("debug3");
            tokio::spawn(async move {
                info!("debug4");
                if let Err(e) = Nightfort::process(watcher, stream, addr).await {
                    error!("Error on this ranger: {}, error: {:?}", addr, e);
                }
            });
        }
        Err(e) => error!("Socket conn error {}", e),
    }
    // let (stream, addr) = listener.accept().await?;
}

我花了两天时间解决这个奇怪的问题。rust中的进程在我本地的macos,linux,linux上的docker上可以很好的运行,但是在aws linux或者aws上的k8s上就不能运行了。我发现的主要问题是:accept()即使客户端认为它建立了与服务器的连接并开始向它发送消息,进程也会挂起。ps显示服务器进程处于S状态。代码是用 alpha 库在 nightly rust 中编写的,我认为依赖项中可能存在错误,然后我更新了我的代码并使用最新版本的依赖项将其切换为 stable rust,但问题仍然存在。

标签: linuxrust

解决方案


推荐阅读