News

Cloudflare's Images Team Found a Race Condition in the hyper HTTP Library

A deep dive into how Cloudflare's Images team tracked down a race condition in the hyper HTTP library that caused truncated responses for larger images, fixed with just four lines of code.

July 2, 2026· 2 min read· Source: The Cloudflare Blog
Cloudflare's Images Team Found a Race Condition in the hyper HTTP Library

Cloudflare's Images service, built in Rust on Workers, uses the hyper HTTP library to handle client connections. When the team rearchitected the Images binding in December 2025 to use a local Unix socket instead of routing through an intermediary service, they started seeing intermittent failures for larger image transformations. The response would come back with a 200 status and a Content-Length header promising megabytes, but the body would be cut short — sometimes only ~200 KB of a 3.3 MB response arrived.

The bug was a race condition in hyper's response flushing logic. When the reader on the other end of the socket was slower than the writer (even by milliseconds), hyper's internal buffer would fill up, and it would need to wait for the socket's outbound buffer to drain. Under the right concurrency conditions, hyper would issue a shutdown on the socket before all data had been flushed, truncating the response.

The team spent six weeks debugging. They built a reproduction that could trigger the bug on demand, ruled out timeouts, tested across hyper versions 0.14 through 1.8 (the bug appeared in all of them), and confirmed the Workers runtime wasn't at fault. Distributed tracing narrowed the problem to the binding path through the Images service.

The fix was remarkably small: four lines of code in hyper to ensure that shutdown is only called after all buffered data has been flushed to the socket. Cloudflare contributed the fix upstream.

This is a great example of how even battle-tested libraries like hyper can have subtle concurrency bugs that only surface under specific production conditions — and how careful, methodical debugging can isolate them.