Proxy: paid requests can't be recovered when the client disconnects before a long upstream request finishes
Check existing issues
- [X] I checked there isn't already an issue for the bug I encountered.
Related: #729 (closed by #748).
Describe the bug
mppx/proxy settles the charge and then forwards the request upstream, passing the client's request.signal to the upstream fetch. When the upstream takes longer than the client, or an edge proxy in front of the server, is willing to wait, the client connection is dropped. The upstream request is then aborted with it, and the payment has already settled.
The payer has no way to collect what they paid for:
- Retrying with the same credential returns
402, because charge replay protection rejects it. - Retrying with a new credential runs the request again and charges again.
#748 added onUpstreamError retries inside the original request, which covers transient upstream errors. As its description notes, it only covers failures before a response is returned. It can't help once the client is gone, and it doesn't offer the same-credential recovery that #729 proposed as its first option.
Long-running paid endpoints hit this consistently. I ran into it with a video generation request (Seedance, typically 2 to 4 minutes) through a public MPP gateway built on mppx/proxy. The charge settled, and the client received an HTTP 524 (origin timeout) after about 2 minutes with no result.
Deferring settlement until the upstream finishes isn't an option for jobs this long, because Tempo transaction credentials stop being valid about 45 seconds after signing (#395).
I'd like to submit a PR for this. I have a working implementation with tests on a branch, and I've summarized the design under "Anything else?". I'm happy to adjust the approach before opening it.
Link to Minimal Reproducible Example
No response
Steps To Reproduce
-
Configure
Proxy.create()with a paid route whose upstream responds slowly:const proxy = Proxy.create({ services: [ Service.from('video', { baseUrl: 'https://upstream.example', // responds after ~3 minutes routes: { 'POST /generate': mppx.charge({ amount: '0.70' }) }, }), ], }) -
Pay for
POST /video/generatewith a valid credential. -
Drop the client connection before the upstream responds (client timeout, or an edge proxy timeout such as Cloudflare's 524).
-
Observe that the charge has settled, the upstream request was aborted along with the client's
request.signal, and no response was delivered. -
Retry the identical request with the same
Authorization: Paymentcredential. Observe a402, because the credential is already used. -
Observe that a fresh credential would run the request again and charge a second time.
Package Version
0.13.4
Anything else?
Runtime: Node.js 22. The gateway runtime is unknown.
Proposed fix (opt-in, so there's no behavior change without it): a replay option on Proxy.create.
Proxy.create({ replay: { store: Store.redis(client) }, services })
- After payment succeeds, the proxy records the delivery in the store. The key is derived from the credential plus the request's method, target, and body, so a credential can only collect the request it paid for.
- The paid upstream request is detached from the client's signal and bounded by a
ttl(default 15 minutes), so it completes and is stored even if the client leaves. - A retry with the same credential never re-verifies or re-settles the payment. Instead:
- If the delivery is complete, it gets the stored response and receipt.
- If the delivery is still in flight, it gets
202withRetry-After. - If the delivery failed, the upstream is retried without charging, up to
maxAttempts(default 3), then the proxy returns502. That's #729's option 1.
- Streaming responses and responses over
maxBodySizeare delivered but not stored. - Concurrent retries of a failed delivery are serialized through
store.update, so at most one upstream retry runs.
This follows the core spec's guidance that servers SHOULD let clients retry non-idempotent paid requests and receive the original response without the operation running again.
Known limits:
- A proxy crash mid-delivery leaves the record pending until
ttl. - A retry that lands between settlement and the pending record being written falls through to the payment handler and gets the normal replayed-credential
402.