Skip to content

fix: add timeout to infinite polling loops to prevent hung requests - #39

Open
memosr wants to merge 1 commit into
circlefin:masterfrom
memosr:fix/infinite-polling-loop-timeout
Open

fix: add timeout to infinite polling loops to prevent hung requests#39
memosr wants to merge 1 commit into
circlefin:masterfrom
memosr:fix/infinite-polling-loop-timeout

Conversation

@memosr

@memosr memosr commented May 17, 2026

Copy link
Copy Markdown

Problem

Two functions in lib/circle/gateway-sdk.ts use while (true) loops with no max retry count:

  1. waitForTransactionConfirmation (line 298) — polls every 2s indefinitely
  2. transferUnifiedBalanceCircle (line 885) — polls every 3s indefinitely

A stuck transaction or unresponsive Circle API can hang the HTTP request forever — particularly problematic in serverless environments where requests hit the timeout wall but the underlying loop keeps spinning.

The EOA path at line 791 already correctly caps polling at 60 attempts (5 minutes total). The two infinite loops should match this pattern.

Fix

Applied the same retry-limit pattern to both functions:

+ const MAX_RETRIES = 60;

  async function waitForTransactionConfirmation(challengeId) {
-   while (true) {
+   let attempts = 0;
+   while (attempts < MAX_RETRIES) {
      // ... existing logic
+     attempts++;
    }
+   throw new Error(`Transaction confirmation timed out after 5 minutes (60 attempts). Challenge ID: ${challengeId}`);
  }

Same pattern applied to transferUnifiedBalanceCircle.

Also standardized polling intervals to 5 seconds across all three functions (matching the EOA path) for consistency.

Impact

  • Reliability: Stuck transactions now fail cleanly with a timeout error instead of hanging indefinitely
  • Serverless-safe: Prevents zombie loops continuing after request timeout
  • Consistent: All three polling functions now share the same retry pattern (60 attempts × 5s = 5 min max)
  • Risk: Low — preserves all existing success/failure paths, only adds the timeout escape

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant