Skip to content

[Android] A request that fails after it was sent is sent again (duplicate POSTs) #504

Closed

Description

@kaloudis

Description

On Android, a request that fails after the server has received it can be sent a second time without the app knowing. For a POST that changes state (a payment, an order), the server acts on it twice. We hit this in ZEUS (a Lightning wallet): a send-coins POST to lnd whose reply came back after the configured timeout made two on-chain payments to the same address.

Version: 0.25.1 (latest), OkHttp 4.12.0 as resolved by React Native 0.8x. iOS does not show it.

There are three paths, all in android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java:

  1. The request interceptor sends the request again after any exception. The interceptor added with clientBuilder.addInterceptor(...) wraps chain.proceed(req) in try, catches SocketException, SocketTimeoutException and Exception, and then falls through to return chain.proceed(chain.request());. A read timeout (set from config({ timeout })) therefore sends the request again. The JS promise has usually rejected by then, so nothing shows the second request. This is also why [Android] Unexpected End of Stream Error when retrying after Connection closed by server #137 ended up with "unexpected end of stream" on the retry.

  2. retryOnConnectionFailure(true). The builder calls retryOnConnectionFailure(false) and then retryOnConnectionFailure(true) a few lines later, so it ends up on. With it on, OkHttp's RetryAndFollowUpInterceptor treats any IOException other than a timeout (a connection reset, "unexpected end of stream") as recoverable even after the request body was sent, and ExchangeFinder.retryAfterFailure() returns true when the call reused a pooled connection. Since all requests share the static ConnectionPool, a POST that the server received on a kept-alive socket is sent again if the socket closes before the reply.

  3. 503 with Retry-After: 0. OkHttp follows such a response by sending the same request again, and that branch does not check retryOnConnectionFailure.

Reproduction

A proxy in front of the server that forwards the POST and then:

  • holds the reply past config({ timeout }): the server receives the POST twice (path 1);
  • closes a reused keep-alive socket without replying: the server received the POST 4 times in one run (path 2);
  • replies 503 with Retry-After: 0: the server receives it twice (path 3).

Suggested fix

  • In the request interceptor, rethrow the IOException instead of calling chain.proceed() again, and wrap other exceptions in an IOException (OkHttp rethrows non-IO exceptions on its dispatcher thread after onFailure, which would crash the app). onFailure already reports the timeout as "The request timed out.".
  • Only enable retryOnConnectionFailure for idempotent methods (GET, HEAD), and remove the duplicate retryOnConnectionFailure(false) call.
  • In the network interceptor, drop Retry-After from 503 responses to other methods, so OkHttp does not follow up.

POSTs then lose OkHttp's silent recovery from failures before the request was sent (next IP, HTTP/2 refused stream, a pooled socket the server just closed). Those surface as errors instead, which is safe because the server never saw the request.

We carry this as a postinstall patch in ZEUS (ZeusLN/zeus#5469) and can open a PR here with a JVM unit test against a MockWebServer if that is welcome.

Activity

  1. RonRadtke commented on Nov 9, 2026

    @RonRadtke
    Owner

    Fixed in 1.1.1 and 0.25.3: on Android a request that fails after it was sent is no longer sent again. The request interceptor rethrows the error instead of retrying, OkHttp's retry after a connection failure is on only for GET and HEAD, and a 503 with Retry-After is no longer resent for other methods. For 0.25.3: npm install react-native-blob-util@support-0.25.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions