Ab – Apache Bench hanging with -k Keep-Alive switch

November 25th, 2009 | by Sean |

This problem was making me go blind! I’m testing Spinneret with ab at the moment, trying to make sure there’s no disastrous behaviour under load. I found that ab just stopped after the first request if I used the -k switch. I’m not sure the problem is my fault, exactly, but I have ‘solved’ it. HTTP 1.0 didn’t have persistent connections. In HTTP 1.1, the default behaviour is persistence, so only a mechanism for explicitly closing a connection is provided.

My Spider

My Spider

Apache bench offers the ‘-k’ switch to allow you to test your webserver with persistent connections, but sends a HTTP/1.0 request. The -k switch causes ab to send the header “Connection: Keep-Alive” to the server. Spinneret’s debugging log confirmed a response was sent back, and the ‘-v’ switch (verbose) in ab would even show that it received the response, but it would just ‘hang’ at that point, until it timed out:

LOG: Response code = 200
apr_poll: The timeout specified has expired (70007)

I wasted a lot of time imagining what could be wrong with it before I decided to just duplicate the response from an Apache server that worked with the -k switch in ab. I spotted that Apache was returning the ‘Connection: Keep-Alive’ header. Adding that header field to a Spinneret response to a HTTP/1.0 request that contains it solves the problem with ab.

This behaviour isn’t specified anywhere, but it is suggested in an Internet Draft for HTTP/1.1 at w3.org:

The Connection header field with a keep-alive keyword must be sent on all requests and responses that wish to continue the persistence.

Perhaps the nice people at Apache might consider some less baffling behaviour for when amateur webserver programmers attempt to pick up their tools!

  1. 4 Responses to “Ab – Apache Bench hanging with -k Keep-Alive switch”

  2. By Brian on Jul 28, 2011 | Reply

    Really helpful and informative post — thanks!

  3. By knib on Nov 15, 2011 | Reply

    Very useful Tip!
    Thanks for sharing!

  4. By alain on Dec 5, 2011 | Reply

    I spent hours on this problem… Unfortunately I found your post after I solved the problem. But thanks for sharing.

  1. 1 Trackback(s)

  2. Mar 28, 2012: Apache Benchmark (ab) hanging and timeout with -k Keep-Alive switch | Veeb's Brain Dump

Post a Comment