Repository navigation
Description
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:
-
The request interceptor sends the request again after any exception. The interceptor added with
clientBuilder.addInterceptor(...)wrapschain.proceed(req)intry, catchesSocketException,SocketTimeoutExceptionandException, and then falls through toreturn chain.proceed(chain.request());. A read timeout (set fromconfig({ 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. -
retryOnConnectionFailure(true). The builder callsretryOnConnectionFailure(false)and thenretryOnConnectionFailure(true)a few lines later, so it ends up on. With it on, OkHttp'sRetryAndFollowUpInterceptortreats any IOException other than a timeout (a connection reset, "unexpected end of stream") as recoverable even after the request body was sent, andExchangeFinder.retryAfterFailure()returns true when the call reused a pooled connection. Since all requests share the staticConnectionPool, a POST that the server received on a kept-alive socket is sent again if the socket closes before the reply. -
503 with
Retry-After: 0. OkHttp follows such a response by sending the same request again, and that branch does not checkretryOnConnectionFailure.
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
503withRetry-After: 0: the server receives it twice (path 3).
Suggested fix
- In the request interceptor, rethrow the
IOExceptioninstead of callingchain.proceed()again, and wrap other exceptions in anIOException(OkHttp rethrows non-IO exceptions on its dispatcher thread afteronFailure, which would crash the app).onFailurealready reports the timeout as "The request timed out.". - Only enable
retryOnConnectionFailurefor idempotent methods (GET,HEAD), and remove the duplicateretryOnConnectionFailure(false)call. - In the network interceptor, drop
Retry-Afterfrom 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
fix(android): close idle pooled connections before a 5s server does
fix(android): close idle pooled connections before a 5s server does
fix(android): do not send a request again after it fails
RonRadtke commentedon Nov 9, 2026
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-Afteris no longer resent for other methods. For 0.25.3:npm install react-native-blob-util@support-0.25.test(e2e): device checks of what 1.1.1 shipped untested
feat(ios): ios.backgroundTask shares the one background session