fjlikun / ice4j

Automatically exported from code.google.com/p/ice4j
0 stars 0 forks source link

Deadlock observed in ice4j code #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
We are observing deadlock using the latest version of ice4j(r300) library. We 
analysed ice4j code and could see a possibility of a deadlock occuring. Let me 
explain the scenario

After run() method of Pacemaker class is called through 
ConnectivityCheckClient.startChecks(CheckList checkList) method, Thread1 inside 
the run() method enters into the wait state due to wait(waitFor). Consider the 
case when stop method of ConnectivityCheckClient.java is called by 
Agent.terminate() method and it sets paceMaker.running as false and before it 
takes lock of paceMaker object, waitFor time elapses, Thread1 reaches the 
finally block and starts waiting for the lock of the linked list paceMakers 
object because of the statement synchronized (paceMakers)

Thread2 which is inside the stop() method of ConnectivityCheckClient.java and 
has taken the lock of the paceMakers object, now tries to take the lock of the 
pacemaker object in the statement  synchronized(paceMaker).

Now this could be a potential deadlock scenario as Thread1 is waiting to take 
the lock on paceMakers object held by Thread2 and Thread2 is waiting to take 
the lock on pacemaker object held by Thread1.

Please help us understand this scenario better. How does the ice4j code make 
sure that deadlock is avoided in this scenario, as we can see a deadlock which 
says 

"DEADLOCK DETECTED:
==================

[deadlocked thread] Timer-11:
----------------------------
Thread 'Timer-11' is waiting to acquire lock 
'org.ice4j.ice.ConnectivityCheckClient$PaceMaker@4e2753eb' that is held by 
thread 'ICE PaceMaker: null'

Stack trace:
------------
    org.ice4j.ice.ConnectivityCheckClient.stop(ConnectivityCheckClient.java:1067)
    org.ice4j.ice.Agent.terminate(Agent.java:1896)
    org.ice4j.ice.Agent.checkListStatesUpdated(Agent.java:1663)
    org.ice4j.ice.ConnectivityCheckClient$2.run(ConnectivityCheckClient.java:445)
    java.util.TimerThread.mainLoop(Timer.java:512)
    java.util.TimerThread.run(Timer.java:462)

[deadlocked thread] ICE PaceMaker: null:
---------------------------------------
Thread 'ICE PaceMaker: null' is waiting to acquire lock 
'java.util.LinkedList@77f69162' that is held by thread 'Timer-11'

Stack trace:
------------
    org.ice4j.ice.ConnectivityCheckClient$PaceMaker.run(ConnectivityCheckClient.java:1040)"

Possible Solution:

We might be able to avoid deadlock here by putting the statement 
paceMaker.running = false; in stop method of ConnectivityCheckClient.java 
inside the synchronised block, something like

                synchronized(paceMaker)
                {
                paceMaker.running = false;
                    paceMaker.notify();
                }

Please let us know your view on this

Original issue reported on code.google.com by amrinder...@gmail.com on 6 Mar 2012 at 5:09

GoogleCodeExporter commented 9 years ago
Hi,

Your analysis is correct and we have a deadlock here. I will test your patch.

Original comment by s...@sip-communicator.org on 9 Mar 2012 at 10:36

GoogleCodeExporter commented 9 years ago
Issue 12 has been merged into this issue.

Original comment by s...@sip-communicator.org on 9 Mar 2012 at 10:38

GoogleCodeExporter commented 9 years ago
Issue 13 has been merged into this issue.

Original comment by s...@sip-communicator.org on 9 Mar 2012 at 11:04

GoogleCodeExporter commented 9 years ago
We applied the patch in trunk (SVN revision 302). Thanks.

Original comment by s...@sip-communicator.org on 9 Mar 2012 at 12:04

GoogleCodeExporter commented 9 years ago
Thanks for the quick turnaround on this

Original comment by amrinder...@gmail.com on 12 Mar 2012 at 4:43

GoogleCodeExporter commented 9 years ago
Thanks for the quick turnaround on this. I came to know there is a reward 
policy in place for Google Code Hosting (code.google.com) related projects:

http://www.google.com/about/appsecurity/reward-program/

Do i wualify for this reward or any such reward policy

Original comment by amrinder...@gmail.com on 21 Aug 2013 at 5:05