hypha-rpc 0.20.80 → 0.20.83

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -336,6 +336,9 @@ class RPC extends _utils_index_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
336
336
  services: this._services,
337
337
  };
338
338
 
339
+ // Track background tasks for proper cleanup
340
+ this._background_tasks = new Set();
341
+
339
342
  // Set up global unhandled promise rejection handler for RPC-related errors
340
343
  const handleUnhandledRejection = (event) => {
341
344
  const reason = event.reason;
@@ -734,6 +737,37 @@ class RPC extends _utils_index_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
734
737
  }
735
738
  }
736
739
 
740
+ // Clean up background tasks
741
+ try {
742
+ // Cancel all background tasks
743
+ for (const task of this._background_tasks) {
744
+ if (task && typeof task.cancel === "function") {
745
+ try {
746
+ task.cancel();
747
+ } catch (e) {
748
+ console.debug(`Error canceling background task: ${e}`);
749
+ }
750
+ }
751
+ }
752
+ this._background_tasks.clear();
753
+ } catch (e) {
754
+ console.debug(`Error cleaning up background tasks: ${e}`);
755
+ }
756
+
757
+ // Clean up connection references to prevent circular references
758
+ try {
759
+ // Clear connection reference to break circular references
760
+ this._connection = null;
761
+
762
+ // Replace emit_message with a no-op to prevent further calls
763
+ this._emit_message = function () {
764
+ console.debug("RPC connection closed, ignoring message");
765
+ return Promise.reject(new Error("Connection is closed"));
766
+ };
767
+ } catch (e) {
768
+ console.debug(`Error during connection cleanup: ${e}`);
769
+ }
770
+
737
771
  this._fire("disconnected");
738
772
  }
739
773
 
@@ -877,8 +911,18 @@ class RPC extends _utils_index_js__WEBPACK_IMPORTED_MODULE_0__.MessageEmitter {
877
911
  }
878
912
 
879
913
  async disconnect() {
914
+ // Store connection reference before closing
915
+ const connection = this._connection;
880
916
  this.close();
881
- await this._connection.disconnect();
917
+
918
+ // Disconnect the underlying connection if it exists
919
+ if (connection) {
920
+ try {
921
+ await connection.disconnect();
922
+ } catch (e) {
923
+ console.debug(`Error disconnecting underlying connection: ${e}`);
924
+ }
925
+ }
882
926
  }
883
927
 
884
928
  async _get_manager_with_retry(maxRetries = 20) {