toland/patron — pull request #47 by TwP (2011-11-12 20:05:15+00:00)
Title: Enables support for Ruby 1.9 "thread blocking region"

Two issues were preventing proper operation of the Ruby thread blocking region code.

As you mentioned before, using a ruby string buffer to collect the header and body data from the curl request was unsafe. Calling back into the Ruby interpreter without first reacquiring the global interpreter lock is very dangerous. So I took your advice and created a memory buffer structure to accumulate the curl responses instead of relaying on the rb_str_buf method.

When curl is used within a multi-threaded environment, it uses signals for handling some of the timeout code. The Ruby interpreter will handle these signals by default. This results in a situation where the signal fired from curl (which has released the GIL) is handled by Ruby code - again triggering this unsafe situation where the GIL has not been reacquired. I have set the CURLOPT_NOSIGNAL flag on the session. This appears to resolve the issue, and timeouts still function properly.