The journal

Programming 12 min read

Why Rust's TcpStream read_to_end hangs on HTTP

See why a Rust web server can wait after a browser sends its request. Learn what the blank line means and use curl to spot where your code got stuck.

Two views of the same web request. On the left, both sides wait. On the right, the server reads the blank line and replies.
The blank line ends the headers. The browser can keep the connection open while it waits for a reply.

You start a tiny Rust server. You open it in your browser. The tab spins. The server says a client has joined, but your page never comes back. Where did it get stuck?

If the server called read_to_end on a TcpStream, look there first. That call waits for the end of the stream. Your browser may have sent its whole request, but it can keep the stream open while it waits for a reply. For a small HTTP GET with no body, read the headers up to the blank line. Then send the reply. You don’t have to wait for the browser to leave.

This guide is for a small HTTP/1.1 server on your own machine. Need a server to try? Start with the tiny Rust web server. It sends two words back. Here, you’ll learn how to tell when it has read enough to send them.

Two kinds of done

Think of a phone call. You ask, “Are you free at six?” Then you wait for an answer. You stop talking, but you don’t hang up. If your friend waits for you to hang up before they answer, the call gets stuck.

A browser can do much the same thing. It sends some bytes. These bytes make up its request. It then waits for your server to send bytes back. The fact that it has stopped sending for now doesn’t mean it plans to close the link. It may send more later, or it may close the link after the reply.

Rust’s read_to_end is a good fit for a file. A file has an end. When Rust gets there, the call can give you all the bytes it found. The same call on an open stream has no such fixed end. It keeps reading until the stream says there is no more to read, or the read fails. It can’t guess that the server has the whole web request.

That explains a detail that can seem odd at first. A read may have got some data even though the call has not come back yet. Rust has the bytes. It needs a rule to tell it when it can stop asking for more. That rule comes from HTTP, not from the stream.

Look for the blank line

Here is a small HTTP request. The blank line after Host is part of it. It can be hard to spot because there’s nothing to see on that line.

GET / HTTP/1.1
Host: localhost

The first line says what to do and which page to get. The next line gives the host name. A real browser may send more lines before it leaves that empty one. We call these extra lines headers. Once the server sees the blank line, it has the full set of headers. On the wire, each line ends with a pair of bytes that Rust can write as \r\n.

If you are working with a GET like the one above, there is no body to read. You can now send a reply. It doesn’t matter if the link stays open while you get that reply ready. Think back to the phone call. The blank line is like a pause you both agreed would mean, “Your turn.”

A blank line does not mark the end of every full request. A request may also carry a body. A form can send what you typed as a body, for example. In that case the headers can tell a real HTTP server how to read the rest. This small lab skips that job. Use an HTTP library for an app that people will use.

See what curl sees

Open two tabs in your terminal. Start the server in the first one. Run the next command in the other tab. It asks for the home page and waits up to three seconds. The time cap is for your test client. It does not fix the server.

curl --http1.1 --max-time 3 -v http://127.0.0.1:7878/

On Windows, if your shell treats curl as a different command, try curl.exe. If your server uses a different port, change the number at the end of the address. Keep this test on your own machine. You can stop your server with Ctrl+C after the test.

Look at the lines that start with a right angle mark. Those are lines that curl sent. Lines that start with a left angle mark came back from the server. If curl shows what it sent but prints no status line, you know it did not see a reply start. That can narrow down where to look next.

A timeout alone does not prove that read_to_end is to blame. The server could be stuck in some other part of the code. If curl says it could not connect at all, check that the server is up and that the port is right. If you see a line like HTTP/1.1 200 OK, the server has at least started its reply. The next place to look is the body.

Put a note on each side of the read

Here’s a quick way to tell where your server got stuck. In the code that deals with one client, print a note right before the read. Print a new note right after it. For that test, you want both notes to show up in the server tab, not in the web page.

Run curl again. If only the first note shows up while curl waits, the call has not come back. If the second note shows up at once but the page still spins, the wait is somewhere else. Move the notes closer to the part that sends the reply. Check the error that Rust gives you instead of throwing it away.

One thing may look strange. When curl hits its time cap and quits, the note after the read may show up at last. That does not mean the server got a reply out in time. It can mean that curl closed the link, and the read saw the end it was waiting for all along. The order of the notes can explain more than the notes on their own.

You can use the same trick the next time a server seems stuck. Start with three points: when it takes a client, when it has the headers, and when it sends the reply. That gives you a small map of what took place. Add more notes only if you need them.

Give the read a stopping rule

The Rust Book’s web server chapter shows a way to read lines from a stream. It uses a BufReader, a tool that can hold bytes for you and give them back one line at a time. Look for an empty header line instead of waiting for the whole link to close. If the link ends before that line, do not treat it as a full set of headers.

Here is what that rule looks like as a short plan. This is not a full server. It shows what each step needs to do in this one lab.

  1. Read the first line. It tells you what page the client wants.
  2. Read the next line. If it is a header, keep going.
  3. On the blank line, stop reading headers.
  4. For this GET with no body, send a reply and close the link.

A read time limit is still a good idea. If no new bytes come in, it keeps a blocking read from waiting with no end in sight. But a time limit is a guard, not a way to tell where a request ends. A good server should be able to say, “I saw the last header,” long before it runs out of time.

A small demo can also put a cap on how much it will read. That way it won’t keep every line it sees. For a real app, you need rules for the full request and limits that fit your needs. If you want to see what Rust can do at the stream level, read the TcpStream docs.

Check the reply, too

A server can get the read right and still leave the tab stuck. The reply has its own rules. Look at the lines that curl got back. A small text reply needs a status line, headers, a blank line, and the text you want to show.

If you set Content-Length, count bytes in the text that you send. Rust’s body.len() gives the byte count for a string. A letter with a mark over it can take more than one byte. If you tell the browser to wait for more bytes than you send and keep the link open, it can sit there and wait.

For a lab that sends one reply per link, you can send Connection: close and then drop the stream after you send the body. Run curl twice as two fresh calls. You should see a status line and the body each time. If the first call works but the next one waits, check that your server went back to its loop to take a new client.

Before you add more pages, save this small test. Ask for the same page each time you change the read or the reply. A known good test makes it much easier to tell which change broke the page.

What to try next

Go back to your first server and find the line where it starts to read. Can you say out loud what makes that read stop? If the answer is “when the other end closes,” ask if that is what you want. For this GET with no body, the answer you want is “when the headers end.”

Once that works, try to explain the whole trip. The client asks for a page. The server reads the request, sees the blank line, and writes a reply. The client sees the status and reads the body. If a tab spins again, you can work out which of those steps it did not reach. You have a place to look that is more useful than the tab.

If you’d like to keep building, Build It in Rust starts with small tools you can run and change as you go.

Further reading: Rust’s Read trait and HTTP/1.1 message format in RFC 9112.

Read next