Bug 497028. Make sure to have mRunningEvent still incremented when we call the runnable's destructor, since that can also do work. r=bsmedberg
--- a/xpcom/threads/nsThread.cpp
+++ b/xpcom/threads/nsThread.cpp
@@ -491,34 +491,42 @@ nsThread::ProcessNextEvent(PRBool mayWai
if (notifyGlobalObserver)
sGlobalObserver->OnProcessNextEvent(this, mayWait && !ShuttingDown(),
mRunningEvent);
nsCOMPtr<nsIThreadObserver> obs = mObserver;
if (obs)
obs->OnProcessNextEvent(this, mayWait && !ShuttingDown(), mRunningEvent);
- // If we are shutting down, then do not wait for new events.
- nsCOMPtr<nsIRunnable> event;
- mEvents->GetEvent(mayWait && !ShuttingDown(), getter_AddRefs(event));
-
- *result = (event.get() != nsnull);
+ ++mRunningEvent;
nsresult rv = NS_OK;
- if (event) {
- LOG(("THRD(%p) running [%p]\n", this, event.get()));
- ++mRunningEvent;
- event->Run();
- --mRunningEvent;
- } else if (mayWait) {
- NS_ASSERTION(ShuttingDown(), "This should only happen when shutting down");
- rv = NS_ERROR_UNEXPECTED;
+ {
+ // Scope for |event| to make sure that its destructor fires while
+ // mRunningEvent has been incremented, since that destructor can
+ // also do work.
+
+ // If we are shutting down, then do not wait for new events.
+ nsCOMPtr<nsIRunnable> event;
+ mEvents->GetEvent(mayWait && !ShuttingDown(), getter_AddRefs(event));
+
+ *result = (event.get() != nsnull);
+
+ if (event) {
+ LOG(("THRD(%p) running [%p]\n", this, event.get()));
+ event->Run();
+ } else if (mayWait) {
+ NS_ASSERTION(ShuttingDown(),
+ "This should only happen when shutting down");
+ rv = NS_ERROR_UNEXPECTED;
+ }
}
+ --mRunningEvent;
if (obs)
obs->AfterProcessNextEvent(this, mRunningEvent);
if (notifyGlobalObserver && sGlobalObserver)
sGlobalObserver->AfterProcessNextEvent(this, mRunningEvent);
return rv;
}