From 546a12259185b66c0db333c69c164a2bf528f248 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Tue, 12 May 2026 00:44:56 +0700 Subject: [PATCH] fix: avoid race condition while resolving PDS I noticed about 1/4 of identities in my app did not have a handle after backfill. Looks like getPDS over dids run twice over the same set of dids creates race condition. One is from this loop, and the other getPDS is invoked from getClient function. I tested with my set of data and edited variant always gives 100% of handles in identities table. --- packages/contrail/src/core/backfill.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/contrail/src/core/backfill.ts b/packages/contrail/src/core/backfill.ts index 559131a..9b46d3a 100644 --- a/packages/contrail/src/core/backfill.ts +++ b/packages/contrail/src/core/backfill.ts @@ -350,16 +350,14 @@ export async function backfillPending( const dids = [...byDid.keys()]; - // Resolve PDS endpoints in background (populates in-memory cache) - const resolvePromise = (async () => { - for (let i = 0; i < dids.length; i += 200) { - await Promise.allSettled( - dids.slice(i, i + 200).map((did) => - getPDS(did as Did, db).catch(() => {}) - ) - ); - } - })(); + // Resolve PDS endpoints (populates in-memory cache) + for (let i = 0; i < dids.length; i += 200) { + await Promise.allSettled( + dids.slice(i, i + 200).map((did) => + getPDS(did as Did, db).catch(() => {}) + ) + ); + } let roundBackfilled = 0; let usersComplete = 0; @@ -471,7 +469,6 @@ export async function backfillPending( } } - await resolvePromise; totalBackfilled += roundBackfilled; // If nothing was backfilled this round, we're stuck -- 2.51.2