From 66536bebcdcc01633d615a1aaf6c43ad5e66a9eb Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Thu, 31 Mar 2022 14:52:18 -0600 Subject: [PATCH 1/2] @urbit/api: do not reconnect if there is a FatalError --- pkg/npm/http-api/src/Urbit.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/npm/http-api/src/Urbit.ts b/pkg/npm/http-api/src/Urbit.ts index ab637821b..ab473e6d3 100644 --- a/pkg/npm/http-api/src/Urbit.ts +++ b/pkg/npm/http-api/src/Urbit.ts @@ -14,6 +14,7 @@ import { SSEOptions, PokeHandlers, Message, + FatalError, } from './types'; import { hexString } from './utils'; @@ -291,7 +292,7 @@ export class Urbit { }, onerror: (error) => { console.warn(error); - if (this.errorCount++ < 4) { + if (!(error instanceof FatalError) && this.errorCount++ < 4) { this.onRetry && this.onRetry(); return Math.pow(2, this.errorCount - 1) * 750; } @@ -311,6 +312,9 @@ export class Urbit { * */ reset() { + if(this.verbose) { + console.log('resetting'); + } this.delete(); this.abort.abort(); this.abort = new AbortController(); From 185ee5f6618c93a4f20f3475bb6686af12fc19b1 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Thu, 31 Mar 2022 14:55:03 -0600 Subject: [PATCH 2/2] interface: fix reconnect logic --- pkg/interface/src/logic/api/bootstrap.ts | 10 +++++++++- pkg/interface/src/logic/api/index.ts | 2 +- pkg/interface/src/logic/state/local.tsx | 12 +++++------- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/pkg/interface/src/logic/api/bootstrap.ts b/pkg/interface/src/logic/api/bootstrap.ts index e6933fec7..4ff435da0 100644 --- a/pkg/interface/src/logic/api/bootstrap.ts +++ b/pkg/interface/src/logic/api/bootstrap.ts @@ -12,9 +12,17 @@ import useStorageState from '../state/storage'; import gcpManager from '../lib/gcpManager'; export async function bootstrapApi() { + airlock.reset(); airlock.onError = (e) => { (async () => { - const { reconnect } = useLocalState.getState(); + const { reconnect, errorCount, set } = useLocalState.getState(); + console.log(errorCount); + if(errorCount > 1) { + set(s => { + s.subscription = 'disconnected'; + }); + return; + } try { await reconnect(); } catch (e) { diff --git a/pkg/interface/src/logic/api/index.ts b/pkg/interface/src/logic/api/index.ts index 6679b8403..f06e4d176 100644 --- a/pkg/interface/src/logic/api/index.ts +++ b/pkg/interface/src/logic/api/index.ts @@ -1,7 +1,7 @@ import Urbit from '@urbit/http-api'; const api = new Urbit('', '', (window as any).desk); api.ship = window.ship; -// api.verbose = true; +api.verbose = true; // @ts-ignore TODO window typings window.api = api; diff --git a/pkg/interface/src/logic/state/local.tsx b/pkg/interface/src/logic/state/local.tsx index 796657be2..dbdaa060e 100644 --- a/pkg/interface/src/logic/state/local.tsx +++ b/pkg/interface/src/logic/state/local.tsx @@ -82,19 +82,17 @@ const useLocalState = create(persist((set, get) => ({ // resume doesn't work properly reconnect: async () => { const { errorCount } = get(); - set(s => ({ errorCount: s.errorCount+1, subscription: 'reconnecting' })); - - if(errorCount > 5) { - set({ subscription: 'disconnected' }); + if(errorCount > 1) { return; } - await wait(Math.pow(2, errorCount) * 750); + set(s => ({ subscription: 'reconnecting', errorCount: s.errorCount + 1 })); + console.log(get().errorCount); try { - airlock.reset(); await bootstrapApi(); } catch (e) { - console.error(`Retrying connection, attempt #${errorCount}`); + console.error(e); + set({ subscription: 'disconnected' }); } }, bootstrap: async () => {