Jelajahi Sumber

Add safety timeout for worker termination in abort method

Co-authored-by: sstidl <12804296+sstidl@users.noreply.github.com>
copilot-swe-agent[bot] 6 bulan lalu
induk
melakukan
027f602d21
1 mengubah file dengan 13 tambahan dan 1 penghapusan
  1. 13 1
      speedtest.js

+ 13 - 1
speedtest.js

@@ -384,6 +384,18 @@ Speedtest.prototype = {
    */
   abort: function() {
     if (this._state < 3) throw "You cannot abort a test that's not started yet";
-    if (this._state < 4) this.worker.postMessage("abort");
+    if (this._state < 4) {
+      this.worker.postMessage("abort");
+      // Safety: terminate worker after a short delay if it hasn't already been terminated
+      // This handles cases where the worker might not respond to the abort message
+      setTimeout(function() {
+        if (this.worker && this._state < 4) {
+          this.worker.terminate();
+          this.worker = null;
+          clearInterval(this.updater);
+          this._state = 4;
+        }
+      }.bind(this), 1000);
+    }
   }
 };