Skip to content

Commit

Permalink
client: fix connection persistence bug (#1938)
Browse files Browse the repository at this point in the history
* fix connection persistence error

* changeset

* await disconnection

* handle error

* clean up active connection resources
  • Loading branch information
TalDerei authored Dec 10, 2024
1 parent be8b50c commit 0cadc3c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tasty-pears-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@penumbra-zone/client': minor
---

attempt to reconnect on broken client connection
20 changes: 19 additions & 1 deletion packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { PenumbraManifest } from './manifest.js';
import { PenumbraProvider } from './provider.js';
import { PenumbraState } from './state.js';
import { PenumbraRequestFailure } from './error.js';

const isLegacyProvider = (
provider: PenumbraProvider,
Expand Down Expand Up @@ -188,7 +189,24 @@ export class PenumbraClient {
await this.attach(providerOrigin);
}
this.connection ??= this.createConnection();
await this.connection.port;

// Connection timeouts, provider detachments, etc. appear similar to a connection denial.
// Explicitly handle denial errors and propagate them back to the caller. Without this,
// a denied connection would immediately trigger an attempt to re-establish the connection.
try {
await this.connection.port;
} catch (error) {
if (error instanceof Error && error.cause) {
if (error.cause === PenumbraRequestFailure.Denied) {
throw error;
}
}

// Clean up dangling connection resources and attempt to establish reconnection
this.destroyConnection();
this.connection = this.createConnection();
await this.connection.port;
}
}

/** Call `disconnect` on the associated provider to release connection
Expand Down

0 comments on commit 0cadc3c

Please sign in to comment.