Description
As an experiment in my application, I set optimised dispatch to be true for the broker and discovered during the startup of my application, a deadlock could occur. The problem is the classic lock hierarchy issue, one thread has asked for lock A then B, another has asked for lock B then A.
I've attached an annotated jstack output with just the deadlock information, annotated with the locks involved. I have also added a patch which seems to resolve the issue, but I can't be sure if the change has other ramifications.
The problem in a nutshell is one thread calls Queue.addSubscription(), which locks dispatchLock, and then calls wakeup/iterate() which blocks on iteratingMutex.
Another thread meanwhile has called Queue.send(), which locks iteratingMutex via wakeup/iterate(), but then tries to call doPageIn() which requires dispatchLock.
It seems to me addSubscription() doesn't need to hold on to dispatchLock while calling wakeup/iterate(), so I moved the wakeup() outside the usage of dispatchLock. This ensures the lock heirarchy between iteratingMutex and dispatchLock is always obtained in the same order. removeSubscription() has the same issue.