[Mongrel] mongrel thread safety and global variables

Zed Shaw zedshaw at zedshaw.com
Fri Sep 1 18:37:30 EDT 2006

On Fri, 2006-09-01 at 17:36 +0530, hemant wrote:
> In environment.rb file, I initialize a connection to some data vending
> servers, through TCP sockets.

> As i know, mongrel serves requests on new threads, so what happens to
> this global variable that is shared among the threads.Is there a
> possiblity that, problem is here?
> I mean, lets say a thread is using this global variable for fetching
> data for protocol code xxx and same time another thread requests for
> protocol code yyy. My connection class, which is basically a proxy to
> these data vending servers, may not be thread safe.If this is the
> problem, then can anybody suggest better approach of making single
> connection to data vending servers, without using shared global
> variables.

Well, Mongrel locks Rails with a Sync (Mutex caused leaks on some
platforms) so there should be only one user of your global variable at
any one time.  Also, the threads aren't using your global after the
request is finished and the lock is released since rails is done

But, if you're running multiple Mongrels, then you'll get concurrent
access to your backend server.  Unless that server is written to support
concurrent access then you'll have these kinds of issues.

BTW, have you considered just using DRb?  It's really fast, very easy to
use, and it would probably solve many of these issues.

Zed A. Shaw
http://www.lingr.com/room/3yXhqKbfPy8 -- Come get help.

More information about the Mongrel-users mailing list