Action Mutex Lock and AR

5 views
Skip to first unread message

Kyle Drake

unread,
Apr 20, 2008, 3:22:06 PM4/20/08
to me...@googlegroups.com
Hi all,

I haven't been able to figure out the reasoning behind the mutex lock
on controller actions.

The README says:

"Merb does have a mutex lock around the call to your controller's
action anywhere that you can call AR objects. Merb's lock is way
smaller then rails giant lock though and allows for many more
concurrent requests to be handled by one process."

But ActiveRecord says it's thread safe, if it's configured with
ActiveRecord::Base.allow_concurrency = true.

Is there a reason that this isn't implemented in Merb that I have not
been able to discover? Turning on AR concurrency and removing the
mutex lock I feel would improve performance quite a bit. I can whip up
some patches to enable this if it's a good idea, but I have an
impression that nobody has done this yet for a reason.

Thanks,

-Kyle

PS: We're doing a Merb session at Minnebar, all are invited!
http://barcamp.org/MinneBarSessions#Merb

Ezra Zygmuntowicz

unread,
Apr 20, 2008, 3:28:38 PM4/20/08
to me...@googlegroups.com


Yes the reason is that ActiveRecord lies about being thread safe. Not
only does it perform worse in thread safe 'mode' but it sometimes
leaks data across threads, returning the wrong result set to a
different request!

Also AR uses one db connection *per thread* and does not clean up
after itself so unless you do manual connection cleanups you will
quickly exhaust all of your db connections.

In general stay very far away from
ActiveRecord::Base.allow_concurrency = true

Cheers-

- Ezra Zygmuntowicz
-- Founder & Software Architect
-- ez...@engineyard.com
-- EngineYard.com

Kyle Drake

unread,
Apr 20, 2008, 4:20:21 PM4/20/08
to me...@googlegroups.com
Ah, yikes. Thanks for clarifying. I found a lot of blog posts with
people using allow_concurrency = true, I don't think a lot of people
know about this dirty little secret yet.

So, does AR not do a lock when allow_concurrency is set to false? Is
that why the action mutex lock is in place?

Thanks,

-Kyle

Ezra Zygmuntowicz

unread,
Apr 20, 2008, 8:09:01 PM4/20/08
to me...@googlegroups.com

On Apr 20, 2008, at 1:20 PM, Kyle Drake wrote:
>
> Ah, yikes. Thanks for clarifying. I found a lot of blog posts with
> people using allow_concurrency = true, I don't think a lot of people
> know about this dirty little secret yet.
>
> So, does AR not do a lock when allow_concurrency is set to false? Is
> that why the action mutex lock is in place?
>
> Thanks,
>
> -Kyle

AR does no locking when allow_concurrency is false, it just assumes
that only one thread will ever be running at one time when AR is
invoked. You can easily turn the mutex in merb off:

$ merb -X off

Reply all
Reply to author
Forward
0 new messages