Symptoms
I have a site that has some static files and some images retrieved from a database table. If I have a number of large dynamic images, WEBrick freaks out, indicating the MySQL database connection is lost. Also, from the development log, it looks like two requests are running simultaneously! Since ActiveController::Base.allow_concurrency is still false, this can't be - or can it?
Once this occurs, WEBrick can be toast until it is restarted.
The Diagnosis
After a little poking around in WEBrick's service method, it seems that WEBrick is running two threads, and is doing a good job of only running one dynamic request at a time until, suddenly, it allows two dynamic requests to run at the same time!
The Problem
A static request does not lock the REQUEST_MUTEX, however, when the static file request is complete, the mutex is unlocked (in the Ensure block) even if this thread was not the one that locked the mutex. This allows a second dynamic request to start before the first one is finished, and chaos ensues.
The Fix
My patch moves the begin/ensure/end block inside the handle_file() method, allowing many static files to still be served, but only allowing one dynamic request.