diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 1f856fafa..3979b415e 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -1844,8 +1844,12 @@ class Runtime extends EventEmitter { } } this.targets = newTargets; - // Dispose all threads. - this.threads.forEach(thread => this._stopThread(thread)); + // Dispose of the active thread. + if (this.sequencer.activeThread !== null) { + this._stopThread(this.sequencer.activeThread); + } + // Remove all remaining threads from executing in the next tick. + this.threads = []; } /** diff --git a/src/engine/sequencer.js b/src/engine/sequencer.js index 61a758f0a..54d4870a5 100644 --- a/src/engine/sequencer.js +++ b/src/engine/sequencer.js @@ -51,6 +51,8 @@ class Sequencer { * @type {!Runtime} */ this.runtime = runtime; + + this.activeThread = null; } /** @@ -97,8 +99,9 @@ class Sequencer { numActiveThreads = 0; let stoppedThread = false; // Attempt to run each thread one time. - for (let i = 0; i < this.runtime.threads.length; i++) { - const activeThread = this.runtime.threads[i]; + const threads = this.runtime.threads; + for (let i = 0; i < threads.length; i++) { + const activeThread = this.activeThread = threads[i]; // Check if the thread is done so it is not executed. if (activeThread.stack.length === 0 || activeThread.status === Thread.STATUS_DONE) { @@ -165,6 +168,8 @@ class Sequencer { } } + this.activeThread = null; + return doneThreads; } diff --git a/test/fixtures/execute/control-stop-all-leaks.sb2 b/test/fixtures/execute/control-stop-all-leaks.sb2 new file mode 100644 index 000000000..fb0ea115a Binary files /dev/null and b/test/fixtures/execute/control-stop-all-leaks.sb2 differ