Merge pull request #1865 from PR0M3TH3AN/fix-nip07-retry-logic-16812290615855532987

Fix NIP-07 retry logic to prevent hangs on timeout
This commit is contained in:
thePR0M3TH3AN authored and GitHub committed 2026-01-27 21:05:29 -05:00
commit 0376d0ecda
1 file changed
+8 -20
+8 -20
View File
@@ -192,26 +192,13 @@ export async function runNip07WithRetry(
retryMultiplier = 2,
} = {},
) {
let hasStarted = false;
let cachedPromise = null;
const getOrStartOperation = () => {
if (!hasStarted) {
hasStarted = true;
try {
cachedPromise = Promise.resolve(operation());
} catch (error) {
hasStarted = false;
cachedPromise = null;
throw error;
}
}
return cachedPromise;
};
// We wrap the operation invocation to ensure it returns a fresh promise each time
// we attempt it. This is critical for retries: if the first attempt stalls (dropped
// by extension), re-awaiting the same promise would just keep waiting on the dead request.
const invokeOperation = () => Promise.resolve(operation());
try {
return await withNip07Timeout(getOrStartOperation, {
return await withNip07Timeout(invokeOperation, {
timeoutMs,
message: NIP07_LOGIN_TIMEOUT_ERROR_MESSAGE,
});
@@ -230,10 +217,11 @@ export async function runNip07WithRetry(
);
devLogger.warn(
`[nostr] ${label} taking longer than ${timeoutMs}ms. Waiting up to ${extendedTimeout}ms for extension response.`,
`[nostr] ${label} taking longer than ${timeoutMs}ms. Retrying with ${extendedTimeout}ms timeout.`,
);
return withNip07Timeout(getOrStartOperation, {
// On retry, we invoke operation() again to send a fresh request to the extension.
return withNip07Timeout(invokeOperation, {
timeoutMs: extendedTimeout,
message: NIP07_LOGIN_TIMEOUT_ERROR_MESSAGE,
});