Skip to content

fix(solana): never pay twice after a paid 5xx; poll re-signs on a fresh blockhash (1.22.3) - #113

Merged
VickyXAI merged 1 commit into
mainfrom
fix/no-resign-after-paid-5xx
Nov 14, 2026
Merged

VickyXAI merged 1 commit into
mainfrom
fix/no-resign-after-paid-5xx

Conversation

@VickyXAI

Copy link
Copy Markdown
Contributor

The double charge (real)

GLM's review of #112 (Finding 1) and #111 asked whether a Solana paid call can be charged twice. It can, on every whole-request retry loop.

  1. Paid POST, sol settles (in parallel with the upstream call), the call then answers 502/503 ("CHARGED BUT REQUEST FAILED").
  2. The paid leg re-sends the same signature once. The gateway still holds that nonce claim and answers with sol's bare 402 PAYMENT_REPLAY ("Sign a new payment for each request"). That body has no field saying the first attempt settled.
  3. _is_safe_resign_error treats PAYMENT_REPLAY with no poll_url/job_id as pre-broadcast, so the retry loop signs a new payment and runs the call again. That payment settles too.

I reproduced this against a fake gateway that settles every distinct authorization it receives. The first one answers 502 and a resend is refused as a replay. On main, every site signed 2, settled 2, and returned OK:

site main: signed / settled this PR
chat (sync/async) 2 / 2 1 / 1, PaymentReplayError
streaming chat (sync/async) 2 / 2 1 / 1
raw POST, e.g. modal_sandbox_exec (sync/async) 2 / 2 1 / 1
raw GET (sync/async) 2 / 2 1 / 1

The Base client (LLMClient/AsyncLLMClient) and the TS SDK do not have this bug. Base re-sends the same signature and then raises PaymentReplayError, with no re-sign loop. TS Solana never re-sends after a 5xx and re-signs only on a stale-blockhash verify rejection.

Fix

  • A 402 that refuses a resend after a 5xx is tagged settled (_SETTLED_ATTR) and is never re-signed. _is_safe_resign_error now checks that tag first. The error says the first attempt may have been charged. It also carries response["prior_attempt"]: the status, the x-blockrun-gateway-request-id, and code/payment_status/tx_hash when the 5xx had them. This covers every 402, not only a replay: a verify rejection of the resend is pre-broadcast only for the resend.
  • A 5xx that reports the payment is raised as it is and not re-sent, so its receipt reaches the caller. That means a payment_status other than not_charged, a tx_hash, CHARGED_UPSTREAM_FAILED, or an X-Payment-Receipt/PAYMENT-RESPONSE header.
  • Kept on purpose: the single same-signature resend after a 5xx. One signature settles at most once, so it can never pay twice. It is also the only recovery from a 5xx the gateway never saw (an LB 502), and sol releases the nonce on uncharged failures so that resend can be served. Also kept: the re-sign after a payment_status: "not_charged" 5xx (sol's pre-settle refusals), and the re-sign after a bare replay with no 5xx before it (the documented collision case).
  • Same handling on the image/video submit leg, which has no re-sign loop, for the error's sake.

Also in this PR (#111 review)

  • Poll keep-fresh re-sign reused the quoted hash. sol quotes recentBlockhash on every 402, not only Modal create. I checked a live unpaid POST /api/v1/videos/generations and it quotes one. On x402>=2.17, re-signing the submit challenge signed on that same aging hash each time. Poll re-signs (proactive and fall-back-to-payment-polls) now sign a copy with recentBlockhash/lastValidBlockHeight/blockrunCreateProof removed, so x402 fetches the latest hash. Amount and payTo are unchanged. On sol today, video polls use ownership proofs and pay against a fresh completion challenge. So this mainly affected the payment-poll fallback, music, and gateways without poll proofs.
  • AsyncSolanaLLMClient streaming raised TypeError on every stream error. This includes a paid 4xx and exhausted 5xx retries. The sync class's staticmethod was aliased unwrapped, so self was bound into response. Now wrapped with staticmethod(...).
  • Warning text: pip install -U 'blockrun-llm[solana]'. x402[svm]>=2.17.0 resolved alone picks x402 2.28 + solders 0.29, which does not co-install with our solana<0.40 pin (x402 2.26 + solders 0.27).
  • Python 3.9: pip install --dry-run 'blockrun-llm[solana]' fails on 3.9. It also failed on 1.22.0 (x402>=2.0.0): no x402 2.x release supports 3.9, so this is not a fix(solana): sign on the 402's quoted blockhash (x402[svm]>=2.17.0), 1.22.1 #111 regression. The base package resolves on 3.9. CI's 3.9 leg already installs without the extra. No change here.

Tests

  • tests/unit/test_solana_paid_5xx_no_resign.py (40 parametrized): real client, real x402 signer, fake RPC and gateway. It counts signed vs settled payments for 4 sites × sync/async across five scenarios:

    • replay after a settled 5xx
    • verify refusal of the resend
    • 5xx carrying a receipt
    • uncharged LB 502 recovering with one payment (control)
    • not_charged 5xx keeping the collision re-sign (control)

    It also covers the async stream TypeError and a no-5xx bare replay that is still re-signed (control).

  • tests/unit/test_solana_quoted_blockhash.py (+3): sync and async video payment polls sign on the RPC hash, never the quote. Stripping keeps amount, payTo, asset and network, and does not mutate the original challenge.

  • Revert check: on origin/main, 28 of the new tests fail ("DID NOT RAISE": it paid twice and returned; the poll test's polls all carried the quoted hash). The controls pass on both. Mutations: removing the settled-tag check fails 16; removing the report-payment checks fails 8.

  • pytest (full): 1569 passed, 15 skipped (main: 1523). black --check ., ruff check . are clean. mypy blockrun_llm gives 248 errors, the same as main.

Patch bump 1.22.2 → 1.22.3. Not published.

…sh blockhash (1.22.3)

A Solana paid request can be charged and still answer 502/503 (sol settles in
parallel with the upstream call). The paid leg re-sent the same signature once,
the gateway refused that resend with a bare 402 PAYMENT_REPLAY, and the
whole-request retry read the bare replay as a concurrent nonce collision and
signed a new payment. Reproduced against a fake gateway on chat, streaming
chat, raw POST and raw GET, sync and async: 2 settlements for one call.

- A 402 that refuses a resend after a 5xx is tagged settled, never re-signed,
  and carries the first attempt's status, gateway request id and receipt under
  response["prior_attempt"]. Only a 5xx with payment_status "not_charged"
  keeps the collision re-sign.
- A 5xx that reports the payment (payment_status, tx_hash,
  CHARGED_UPSTREAM_FAILED, receipt header) is raised as is, not resent.
- The same-signature resend stays: one signature settles at most once.
- Media poll keep-fresh re-signs drop the challenge's quoted blockhash; on
  x402>=2.17 they reused the submit's aging quote.
- AsyncSolanaLLMClient streaming raised TypeError on every stream error
  (unwrapped staticmethod alias).
- Old-x402 warning says pip install -U 'blockrun-llm[solana]'.
@VickyXAI
VickyXAI merged commit 064b01c into main Nov 14, 2026
6 checks passed
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