@cortexkit/subc-client 0.19.0 → 0.21.0

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.
package/src/provider.ts CHANGED
@@ -680,8 +680,7 @@ export class SubcProvider {
680
680
 
681
681
  async close(): Promise<void> {
682
682
  if (!this.closeStarted) {
683
- this.closeStarted = true;
684
- this.cancelRestoredDebounce();
683
+ this.endServing(new SubcProviderError("provider closed", "provider_closed"));
685
684
  const sock = this.sock;
686
685
  try {
687
686
  await sendFrame(sock, buildFrame(FrameType.Goodbye, controlFlags(), 0, 0, 0n, new Uint8Array(0)));
@@ -722,7 +721,9 @@ export class SubcProvider {
722
721
  const frame = await sock.readFrame(Number.POSITIVE_INFINITY, { afterHeaderMs: BODY_READ_TIMEOUT_MS });
723
722
  const keepGoing = await this.dispatch(frame, sock, generation, control);
724
723
  if (!keepGoing) {
725
- if (this.sock === sock && this.generation === generation) this.closeStarted = true;
724
+ if (this.sock === sock && this.generation === generation) {
725
+ this.endServing(new SubcProviderError("daemon closed provider connection", "connection_closed"));
726
+ }
726
727
  break;
727
728
  }
728
729
  }
@@ -903,12 +904,16 @@ export class SubcProvider {
903
904
 
904
905
  private abortGeneration(_generation: number): void {
905
906
  this.abortAllInflight();
907
+ this.rejectPendingRequests(new SubcProviderError("provider connection dropped", "connection_dropped"));
908
+ this.liveRoutes.clear();
909
+ }
910
+
911
+ private rejectPendingRequests(error: Error): void {
906
912
  for (const [key, pending] of this.pending) {
907
913
  this.pending.delete(key);
908
914
  clearTimeout(pending.timer);
909
- pending.reject(new SubcProviderError("provider connection dropped", "connection_dropped"));
915
+ pending.reject(error);
910
916
  }
911
- this.liveRoutes.clear();
912
917
  }
913
918
 
914
919
  private abortAllInflight(): void {
@@ -1410,12 +1415,19 @@ export class SubcProvider {
1410
1415
  }
1411
1416
 
1412
1417
  private failFatal(err: Error): void {
1418
+ this.endServing(err);
1419
+ this.sock.close();
1420
+ this.finishClosed();
1421
+ }
1422
+
1423
+ /** End work before closing the socket, which cannot settle requests on its own. */
1424
+ private endServing(err: Error): void {
1413
1425
  if (!this.closedErr) this.closedErr = err;
1414
1426
  this.closeStarted = true;
1415
1427
  this.cancelRestoredDebounce();
1428
+ this.rejectPendingRequests(this.closedErr);
1416
1429
  this.abortAllInflight();
1417
- this.sock.close();
1418
- this.finishClosed();
1430
+ this.liveRoutes.clear();
1419
1431
  }
1420
1432
 
1421
1433
  private finishClosed(): void {