# Puzzled by Mutex lock call

**URL:** <https://www.ruby-forum.com/t/puzzled-by-mutex-lock-call/187408>\
**Category:** Rails\
**Created:** [April 22, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/puzzled-by-mutex-lock-call/187408 "2010-04-22T18:01:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [April 22, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/puzzled-by-mutex-lock-call/187408/1 "2010-04-22T18:01:37Z")

</div>

I have some code that uses and instance of Mutex and calls  
@mutex.lock, executes some code and then does  
unlock(). When I call lock() I get some ActiveRecord error which seems  
unrelated.

Couldn’t find ActiveJob with ID=3  
["/home/LGuild/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active\_record/  
base.rb:1586:in `find_one'", "script/jobmgr.rb:107:in`lock’",

This is on Fedora 12, I haven’t a good idea on how to troubleshoot  
this …

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [April 22, 2010, 10:17pm UTC](https://www.ruby-forum.com/t/puzzled-by-mutex-lock-call/187408/2 "2010-04-22T22:17:28Z")

</div>

On Apr 22, 5:00 pm, Jedrin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have some code that uses and instance of Mutex and calls  
> @mutex.lock, executes some code and then does  
> unlock(). When I call lock() I get some ActiveRecord error which seems  
> unrelated.

What are you calling lock on / how are you calling it ? There’s an  
active record method called lock and it looks like you’re somehow  
ending up calling that

Fred

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [April 22, 2010, 11:10pm UTC](https://www.ruby-forum.com/t/puzzled-by-mutex-lock-call/187408/3 "2010-04-22T23:10:10Z")

</div>

I do

@mutex = Mutex.new

and then

@mutex.lock  
…

@mutex.unlock

On Apr 22, 4:15 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [April 23, 2010, 6:37am UTC](https://www.ruby-forum.com/t/puzzled-by-mutex-lock-call/187408/4 "2010-04-23T06:37:24Z")

</div>

Not sure what the purpose of your use of such a mutex lock is in your  
app, but if it’s something other than restricting access to some  
resource amongst some threads handled within a given instance of your  
rails app running in some process, … but in terms of rails, I don’t  
think a mutex lock will do what you’re thinking it will do if your  
rails app is running / can run in more than one process (ie via  
passenger or via a mongrel cluster or …).

Assuming you aren’t talking about dealing with threads within a given  
process, and you really are trying to restrict access to some resource  
from potentially multiple process instances of your rails app, then  
you’ll need to use a locking strategy that can be used in a multi  
process context (ie like via your app’s db, or potentially via files,  
or …).

Note however that if you are only running one instance or rails, ie a  
single instance of webrick or mongrel, then your rails app is  
essentially single-threaded (running in a single process) in terms of  
requests handling. In such a single-instance setup, you likely  
wouldn’t need the use of mutex (unless your dealing with threads in  
that single process) since your app would only be able to handle one  
request at a time anyway.

Jeff

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [April 23, 2010, 5:12pm UTC](https://www.ruby-forum.com/t/puzzled-by-mutex-lock-call/187408/5 "2010-04-23T17:12:34Z")

</div>

I basically have a job manager that runs with script/runner and is  
independent of the webserver. It allows DRB connections, TCP  
connections,  
and has a scheduling loop. Inside the scheduling loop is where the  
error sometimes happens which doesn’t seem related to anything I did.

I haven’t worked on this code for awhile, but had not seen these  
types of errors when I was running on Centos, I just moved the code to  
Fedora …

jobmgr = JobMgr.new

DRb.start\_service(‘druby://’ + jobmgr.ipaddr, jobmgr)

if jobmgr.tcp\_port  
Thread.start do  
jobmgr.get\_tcp\_reqs  
end  
end

puts ‘run’

jobmgr.run\_scheduler

DRb.thread.join

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [April 23, 2010, 6:04pm UTC](https://www.ruby-forum.com/t/puzzled-by-mutex-lock-call/187408/6 "2010-04-23T18:04:41Z")

</div>

On Apr 22, 10:08 pm, Jedrin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I do
> 
> @mutex = Mutex.new
> 
> and then
> 
> @mutex.lock  
> …
> 
> @mutex.unlock

There’s clearly something else going on. Can you reduce the code with  
the problem to a small sample you can post in its entirety ? What is  
the ActiveJob model and does the code exhibiting the problem also  
manipulate instances of it ?

Fred

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [April 23, 2010, 5:44pm UTC](https://www.ruby-forum.com/t/puzzled-by-mutex-lock-call/187408/7 "2010-04-23T17:44:46Z")

</div>

I basically have a job manager that runs with script/runner and is  
independent of the webserver. It allows DRB connections, TCP  
connections,  
and has a scheduling loop. Inside the scheduling loop is where the  
error sometimes happens which doesn’t seem related to anything I did.

I haven’t worked on this code for awhile, but had not seen these  
types of errors when I was running on Centos, I just moved the code to  
Fedora …

jobmgr = JobMgr.new

DRb.start\_service(‘druby://’ + jobmgr.ipaddr, jobmgr)

if jobmgr.tcp\_port  
Thread.start do  
jobmgr.get\_tcp\_reqs  
end  
end

puts ‘run’

jobmgr.run\_scheduler

DRb.thread.join
