Skip to content
Basic TCP server does not give response
awesome-e
OP

a month ago

I was looking into setting up a tcp server and tried to get a basic example working:

// Build info: edition = "2024"
// rust  │  1.85.1  │  Cargo.toml (1.85.1)

fn main() {

    let listener = TcpListener::bind("0.0.0.0:2610").unwrap();

    for stream in listener.incoming() {

        println!("got stream");

        let Ok(mut stream) = stream else {

            eprintln!("bad stream");

            continue;

        };

        let wr = stream.write_all(format!("hello\r\n").as_bytes());

        dbg!(&wr);

        stream.flush().expect("flush failed");

        // additionally, if i add a std::thread::sleep here, shutdown fails with an error `Transport endpoint is not connected (os error 107)` on railway only

        let sd = stream.shutdown(Shutdown::Both);

        dbg!(&sd);

        println!("done with stream");

    }

}

Then, I tried to send a basic message and get a response.


% printf "hi\r\n" | nc idofmy.proxy.rlwy.net 18437

% printf "hi\r\n" | nc idofmy.proxy.rlwy.net 18437

% printf "hi\r\n" | nc idofmy.proxy.rlwy.net 18437

hello

% printf "hi\r\n" | nc idofmy.proxy.rlwy.net 18437

hello

% printf "hi\r\n" | nc idofmy.proxy.rlwy.net 18437

hello

% printf "hi\r\n" | nc idofmy.proxy.rlwy.net 18437

% printf "hi\r\n" | nc idofmy.proxy.rlwy.net 18437

% printf "hi\r\n" | nc idofmy.proxy.rlwy.net 18437

hello

The service deploy logs show 'got stream', wr and sd both being Ok(()), and 'done with stream' for requests that do have a response and for requests that don't get a response.

If I run the same code locally, it works just fine:


% printf "hi\r\n" | nc localhost 2610

hello

% printf "hi\r\n" | nc localhost 2610

hello

% printf "hi\r\n" | nc localhost 2610

hello

% printf "hi\r\n" | nc localhost 2610

hello

% printf "hi\r\n" | nc localhost 2610

hello

Is this an issue with my code, or there something shutting down my tcp stream prematurely on Railway? From the rust application's side, everything gets fine, but I never get a response when I try to send the request remotely.

$10 Bounty

5 Replies

Railway
BOT

a month ago

This thread has been opened as a bounty so the community can help solve it.

Status changed to Open Railway • about 1 month ago


awesome-e
OP

a month ago

the service's network settings:

image.png


iiviavs

a month ago

Your TCP Proxy mapping looks correct: public :18437 -> container :2610, and binding 0.0.0.0:2610 is right. This points to a TCP half-close / client test issue, though the code alone does not prove it.

Piping into nc makes nc send EOF (FIN) on its write side when stdin ends. TCP is full duplex, so the server can still send a response after receiving that FIN. But this server never reads the request, and immediately closes both directions after writing a banner. First try removing the explicit shutdown (dropping TcpStream closes it) and use a client that remains open briefly, e.g. (printf 'hi\r\n'; sleep 1) | nc -v idofmy.proxy.rlwy.net 18437, or nc -N (OpenBSD netcat; -q 1 on some implementations) so the client half-closes stdin but continues reading.

For an actual request/response server, read the input before replying, then let the stream drop after the write:

Rust sketch:

let mut stream = stream?;

let mut line = String::new();

BufReader::new(stream.try_clone()?).read_line(&mut line)?;

stream.write_all(b"hello\r\n")?;

stream.flush()?;

// Return and let drop close the connection; no Shutdown::Both needed.

One caveat: Rust write_all followed by shutdown(Both) should ordinarily send queued bytes before FIN on a single TCP connection. So if a client that continues reading still gets intermittent empty responses, that would be evidence worth escalating as a proxy issue. Compare client output and server logs for the same connection, and test with nc -N / nc -q 1 to remove netcat-variant ambiguity.


iiviavs

Your TCP Proxy mapping looks correct: public :18437 -> container :2610, and binding 0.0.0.0:2610 is right. This points to a TCP half-close / client test issue, though the code alone does not prove it. Piping into nc makes nc send EOF (FIN) on its write side when stdin ends. TCP is full duplex, so the server can still send a response after receiving that FIN. But this server never reads the request, and immediately closes both directions after writing a banner. First try removing the explicit shutdown (dropping TcpStream closes it) and use a client that remains open briefly, e.g. (printf 'hi\r\n'; sleep 1) | nc -v idofmy.proxy.rlwy.net 18437, or nc -N (OpenBSD netcat; -q 1 on some implementations) so the client half-closes stdin but continues reading. For an actual request/response server, read the input before replying, then let the stream drop after the write: Rust sketch: let mut stream = stream?; let mut line = String::new(); BufReader::new(stream.try_clone()?).read_line(&mut line)?; stream.write_all(b"hello\r\n")?; stream.flush()?; // Return and let drop close the connection; no Shutdown::Both needed. One caveat: Rust write_all followed by shutdown(Both) should ordinarily send queued bytes before FIN on a single TCP connection. So if a client that continues reading still gets intermittent empty responses, that would be evidence worth escalating as a proxy issue. Compare client output and server logs for the same connection, and test with nc -N / nc -q 1 to remove netcat-variant ambiguity.

awesome-e
OP

a month ago

You were correct about nc closing before it finished reading the response, though I think there's more to it.

For my case, having the server read an end sequence line is sufficient, but I do think there is also still a limitation with Railway here.

I created a short client program to have more precise control of when the writing end shuts down, so this is the minimal example that demonstrates a difference now:

// server
fn main() -> Result<(), Box<dyn Error>> {
    let listener = TcpListener::bind("0.0.0.0:2610")?;
    for stream in listener.incoming() {
        println!("got stream");
        let Ok(mut stream) = stream else {
            eprintln!("bad stream");
            continue;
        };

        let buf_reader = BufReader::new(&stream);
        let res = match buf_reader
            .lines()
            .enumerate()
            .map(|line| line.1.and_then(|l| {
                match l.trim() {
                    "TEST 1 2 3" => Ok(()),
                    _ => Err(io::Error::new(ErrorKind::Other, "bad")),
                }
            }))
            .collect::<Result<Vec<()>, _>>() // reads TEST 1 2 3 until EOF or first Err
        {
            Ok(line) => stream.write_all(b"ok")?,
            Err(e) => stream.write_all(e.to_string().as_bytes())?
        };

        // no explicit shutdown either
        dbg!(res); // Always `()` since the write_all succeeds; both locally and remotely
    }

    Ok(())
}

// client
fn main() -> Result<(), Box<dyn Error>> {
    // Case 1: any line is bad before EOF
    let mut stream = TcpStream::connect("localhost:2610")?;
    stream.write_all("TEST 1 2 3\r\nbad\r\n".as_bytes())?;
    let mut buf = Vec::new();
    let size = stream.read_to_end(&mut buf)?;
    println!("recv case 1: {}, `{}`", size, String::from_utf8_lossy(&buf));

    // Case 2: shut down write end
    let mut stream = TcpStream::connect("localhost:2610")?;
    stream.write_all("TEST 1 2 3\r\n".as_bytes())?;
    stream.shutdown(Shutdown::Write)?;
    let mut buf = Vec::new();
    let size = stream.read_to_end(&mut buf)?;
    println!("recv case 2: {}, `{}`", size, String::from_utf8_lossy(&buf));

    // Case 3: correct info but no shutdown
    let mut stream = TcpStream::connect("localhost:2610")?;
    stream.write_all("TEST 1 2 3\r\n".as_bytes())?;
    stream.set_read_timeout(Some(Duration::from_millis(500)))?;
    let mut buf = Vec::new();
    let size = stream.read_to_end(&mut buf)
        .map_err(|_| "Read timeout")?;
    println!("recv case 3: {}, `{}`", size, String::from_utf8_lossy(&buf));

    Ok(())

    // Railway
    // recv case 1: 3, `bad`
    // recv case 2: 0, ``
    // Error: "Read timeout"

    // Local
    // recv case 1: 3, `bad`
    // recv case 2: 2, `ok`
    // Error: "Read timeout"
}

In this case, the entire input is read by the server and never shutdown. Since it waits until EOF before writing its first byte, I'm inclined to believe that an empty response is likely to be caused by Railway terminating the TCP connection when the client sends EOF/FIN rather than after it closes the connection on its own.

I would not be too surprised if this is intentional for some reason, but if so, it would be nice to mention this in the railway docs.


janinecoakley44-tech

a month ago

use std::io::{BufRead, BufReader, Write};

use std::net::TcpListener;

fn main() -> std::io::Result<()> {

let listener = TcpListener::bind("0.0.0.0:2610")?;

for stream in listener.incoming() {

    let mut stream = match stream {

        Ok(s) => s,

        Err(e) => {

            eprintln!("Connection failed: {e}");

            continue;

        }

    };

    // Clone the stream handle: one for buffered reading, one for writing

    let mut reader = BufReader::new(stream.try_clone()?);

    let mut line = String::new();

    // Read a single delimited command/line without waiting for EOF

    if reader.read_line(&mut line)? > 0 {

        let trimmed = line.trim();

        if trimmed == "TEST 1 2 3" {

            stream.write_all(b"ok\r\n")?;

        } else {

            stream.write_all(format!("bad: {trimmed}\r\n").as_bytes())?;

        }

        stream.flush()?;

    }

    // No stream.shutdown(Shutdown::Both) — dropping `stream` closes it cleanly

}

Ok(())

}


iiviavs

a month ago

Thanks, the reproducer helps. Reading a newline and replying before EOF seems like a good workaround for the app. But the second test looks like the proxy drops the server's reply after the client half-closes its write side; TCP should still allow the server to write then. Railway, is that expected? If so, it would be useful to document. Otherwise, this seems like a clear proxy bug to investigate.


Welcome!

Sign in to your Railway account to join the conversation.

Loading...