In this TCP server code, a single client disconnecting with an error will shut down the entire server. How can this bug be fixed?

Go & Rust interview question for Advanced practice.

Answer

Both B and C are valid and common strategies to fix this.

Explanation

The correct answer is D. Both B and C are valid ways to solve this problem. Wrapping the call in a match or if let Err(e) block allows the loop to continue while handling the specific error. However, the most idiomatic and scalable solution in Tokio is C: spawning each connection into its own concurrent task with tokio::spawn. This isolates connection failures completely and allows the server to handle many connections concurrently, not just one at a time. Because both are correct, D is the best answer.

Related Questions