Index: interpreter/concurrency/Activity.cpp
===================================================================
--- interpreter/concurrency/Activity.cpp	(revision 13006)
+++ interpreter/concurrency/Activity.cpp	(working copy)
@@ -311,7 +311,7 @@
  * We're leaving the current thread.  So we need to deactivate
  * this.
  */
-void Activity::exitCurrentThread()
+void Activity::exitCurrentThread(bool dispatch)
 {
     // deactivate the nesting level
     deactivate();
@@ -322,7 +322,7 @@
     }
     // this activity owned the kernel semaphore before entering here...release it
     // now.
-    releaseAccess();
+    releaseAccess(dispatch);
 }
 
 
@@ -2180,7 +2180,7 @@
 /**
  * Release exclusive access to the kernel
  */
-void Activity::releaseAccess()
+void Activity::releaseAccess(bool dispatch)
 {
     // make sure we're really the holder on this
     if (ActivityManager::currentActivity == this)
@@ -2187,7 +2187,7 @@
     {
         // reset the numeric settings
         Numerics::setDefaultSettings();
-        ActivityManager::releaseAccess();
+        ActivityManager::releaseAccess(dispatch);
     }
 }
 
Index: interpreter/concurrency/Activity.hpp
===================================================================
--- interpreter/concurrency/Activity.hpp	(revision 13006)
+++ interpreter/concurrency/Activity.hpp	(working copy)
@@ -183,7 +183,7 @@
     bool        setTrace(bool);
     inline void yieldControl() { releaseAccess(); requestAccess(); }
     void        yield();
-    void        releaseAccess();
+    void        releaseAccess(bool dispatch = true);
     void        requestApiAccess();
     void        requestAccess();
     void        setupCurrentActivity();
@@ -261,7 +261,7 @@
     void inheritSettings(Activity *parent);
     void setupExits();
     void enterCurrentThread();
-    void exitCurrentThread();
+    void exitCurrentThread(bool dispatch = true);
     void run(ActivityDispatcher &target);
     void run(CallbackDispatcher &target);
     void run(TrappingDispatcher &target);
Index: interpreter/concurrency/ActivityManager.cpp
===================================================================
--- interpreter/concurrency/ActivityManager.cpp	(revision 13006)
+++ interpreter/concurrency/ActivityManager.cpp	(working copy)
@@ -727,14 +727,17 @@
 /**
  * release the kernel access and make sure waiting activites are woken up.
  */
-void ActivityManager::releaseAccess()
+void ActivityManager::releaseAccess(bool dispatch)
 {
     DispatchSection lock;                // need the dispatch queue lock
 
     // since we're releasing the semaphore, give any queued activities a nudge
     // forward in the request process.
-    dispatchNext();
-
+    if (dispatch)
+    {
+        dispatchNext();
+    }
+    
     // now release the kernel lock
     unlockKernel();
 }
Index: interpreter/concurrency/ActivityManager.hpp
===================================================================
--- interpreter/concurrency/ActivityManager.hpp	(revision 13006)
+++ interpreter/concurrency/ActivityManager.hpp	(working copy)
@@ -95,7 +95,7 @@
         kernelSemaphore.release();
     }
 
-    static void releaseAccess();
+    static void releaseAccess(bool dispatch = false);
     static bool lockKernelImmediate();
     static void createLocks();
     static void closeLocks();
Index: interpreter/runtime/InterpreterInstance.cpp
===================================================================
--- interpreter/runtime/InterpreterInstance.cpp	(revision 13006)
+++ interpreter/runtime/InterpreterInstance.cpp	(working copy)
@@ -531,15 +531,15 @@
         sysInstance.terminate();
 
         // ok, deactivate this again...this will return the activity because the terminating
-        // flag is on.
-        current->exitCurrentThread();
+        // flag is on. Don't nudge the dispatch queue yet
+        current->exitCurrentThread(false);
     }
     // do the release in a catch block to ensure we really release this
     catch (NativeActivation *)
     {
         // ok, deactivate this again...this will return the activity because the terminating
-        // flag is on.
-        current->exitCurrentThread();
+        // flag is on. Don't nudge the dispatch queue yet
+        current->exitCurrentThread(false);
 
     }
 
@@ -559,6 +559,9 @@
     commandHandlers = OREF_NULL;
     requiresFiles = OREF_NULL;
 
+    // Release the kernel lock (with the usual nudge to the dispatch queue) as a
+    // new activity may have been created for cleanup causing an extra kernel lock
+    ActivityManager::releaseAccess(); 
 
     // tell the main interpreter controller we're gone.
     Interpreter::terminateInterpreterInstance(this);
