Skip to content

Commit 777aa6e

Browse files
BitHighlanderclaude
andcommitted
fix: guard finally clause with promise identity check
When a forceRefresh call bypasses dedup while a prior fetch is still in-flight, both promises run concurrently. The older promise's finally would null out balancesFetchInProgress even after the newer force-refresh promise had replaced it, causing subsequent callers to miss the in-flight force fetch and spawn redundant RPC calls. Fix by only clearing the ref when it still points to this promise. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7651fc5 commit 777aa6e

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

chrome-extension/src/background/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ async function fetchBalancesFromPioneer(forceRefresh = false): Promise<any[]> {
103103
// Deduplicate concurrent calls — but honor forceRefresh
104104
if (balancesFetchInProgress && !forceRefresh) return balancesFetchInProgress;
105105

106-
balancesFetchInProgress = (async () => {
106+
const thisPromise: Promise<any[]> = (async () => {
107107
try {
108108
const allPubkeys = wallet.getPubkeys();
109109
if (allPubkeys.length === 0) return cachedBalances;
@@ -301,11 +301,16 @@ async function fetchBalancesFromPioneer(forceRefresh = false): Promise<any[]> {
301301
console.error('[fetchBalances] Error:', e.message || e);
302302
return cachedBalances;
303303
} finally {
304-
balancesFetchInProgress = null;
304+
// Only clear the in-flight ref if it still points to this promise — a newer
305+
// forceRefresh call may have replaced it while we were running.
306+
if (balancesFetchInProgress === thisPromise) {
307+
balancesFetchInProgress = null;
308+
}
305309
}
306310
})();
307311

308-
return balancesFetchInProgress;
312+
balancesFetchInProgress = thisPromise;
313+
return thisPromise;
309314
}
310315

311316
const onStart = async function () {

0 commit comments

Comments
 (0)