Repository navigation
fix(android): do not send a request again after it fails - #505
Conversation
A request that failed once the server may have received it could go out a second time without the app knowing (RonRadtke#504). For a POST that changes state, a payment or an order, the server acted on it twice. There were three ways in. The application interceptor caught any exception from chain.proceed(req), a read timeout from config({ timeout }) included, and then called chain.proceed() again. JS had usually rejected already, so nothing showed the second request. The failure is now rethrown for OkHttp to report through onFailure, which already turns a timeout into ETIMEDOUT. An exception that is not an IOException is wrapped in one, because OkHttp rethrows anything else on its dispatcher thread after onFailure, which crashes the app. The timeout flag the interceptor set could only reach JS through a resend that succeeded, so it is gone and the response info reports timeout: false, as it did for every other response. The builder called retryOnConnectionFailure(false) and then (true). With it on, OkHttp 4 resends a request after an IOException other than a timeout (a reset, "unexpected end of stream") even after the body was sent, when the call used a pooled connection, and every request shares one ConnectionPool. It is now on only for GET and HEAD. Other methods lose OkHttp's silent recovery from failures before the request went out (next route, refused HTTP/2 stream, a pooled socket the server just closed); those fail with an error instead, and the server never saw the request. OkHttp follows a 503 with Retry-After: 0 by sending the request again, and that path does not check retryOnConnectionFailure. The network interceptor drops Retry-After from a 503 to any method other than GET and HEAD, so OkHttp returns the 503 instead. JS no longer sees that header on such a response. The three rules are companion functions (mayResend, proceedOnce, withoutRetryAfter) so ReactNativeBlobUtilResendTest can wire them into a real OkHttpClient against a local ServerSocket server and count what arrives. Each POST test fails with its part of the fix reverted (2 requests, or no failure where one is expected) and passes with it; the GET tests check that GET keeps OkHttp's retry.
Since the previous commit, methods other than GET and HEAD no longer get OkHttp's retry on a pooled connection (RonRadtke#504). OkHttp only checks that a pooled connection is still open before a request other than GET once it has been idle for 10s or more. A server that closes idle connections after 5s, as Node's http server and uvicorn do by default, therefore got a POST sent 5 to 10s after the previous request on a connection it had already closed. The call failed with "unexpected end of stream" although the server never saw the request. Before that commit OkHttp hid this by sending the request again on a new connection. The shared ConnectionPool now keeps an idle connection for 4s instead of the default 5 minutes (still up to 5 idle connections), so the pool drops it before such a server does. The cost is a new connection for requests more than 4s apart. A server that closes idle connections in under 4s still leaves a window. This was reproduced in an app on an Android 15 emulator against a real Node/Express server (BlueWallet LndHub, 5s keep-alive): a POST 7s after a GET failed with "unexpected end of stream" and never reached the server, and with the 4s pool it went out on a new connection and succeeded. The pool comes from newConnectionPool(), which ReactNativeBlobUtilResendTest now uses for all its clients. Its server can close a connection after 5s idle, as Node does. A POST 7s after a GET succeeds and arrives once on a second connection; with newConnectionPool() returning the default ConnectionPool() it fails with "unexpected end of stream". A POST 2s after a GET still reuses the connection.
|
Added aed220b: the shared ConnectionPool now keeps idle connections for 4s instead of OkHttp's default 5 minutes. Without OkHttp's retry for POST, a server that closes idle connections after 5s (Node's http server and uvicorn by default) gets a POST sent 5 to 10s after the previous request on a connection it already closed. OkHttp only checks a pooled connection before a request other than GET once it has been idle for 10s, so the call failed with "unexpected end of stream" and the server never saw it. ZEUS hit this against a real Node/Express server (BlueWallet LndHub) on an Android 15 emulator: a POST 7s after a GET failed without the change and succeeded with it. ReactNativeBlobUtilResendTest now builds its clients on newConnectionPool() and has two more tests against a server that closes connections idle for 5s: a POST 7s after a GET goes out on a new connection and arrives once, and a POST 2s after a GET still reuses the connection. With newConnectionPool() returning the default ConnectionPool(), the 7s test fails with "unexpected end of stream". npm test (158), npm run lint (0 errors) and npm run test:android (96 tests) pass. |
A request that failed once the server may have received it could go out a second time without the app knowing (#504). For a POST that changes state, a payment or an order, the server acted on it twice. There were three ways in. The application interceptor caught any exception from chain.proceed(req), a read timeout from config({ timeout }) included, and then called chain.proceed() again. JS had usually rejected already, so nothing showed the second request. The failure is now rethrown for OkHttp to report through onFailure, which already rejects a timeout with "The request timed out." An exception that is not an IOException is wrapped in one, because OkHttp rethrows anything else on its dispatcher thread after onFailure, which crashes the app. The timeout flag the interceptor set could only reach JS through a resend that succeeded, so it is gone and the response info reports timeout: false, as it did for every other response. The builder called retryOnConnectionFailure(false) and then (true). With it on, OkHttp 4 resends a request after an IOException other than a timeout (a reset, "unexpected end of stream") even after the body was sent, when the call used a pooled connection, and every request shares one ConnectionPool. It is now on only for GET and HEAD. Other methods lose OkHttp's silent recovery from failures before the request went out (next route, refused HTTP/2 stream, a pooled socket the server just closed); those fail with an error instead, and the server never saw the request. OkHttp follows a 503 with Retry-After: 0 by sending the request again, and that path does not check retryOnConnectionFailure. The network interceptor drops Retry-After from a 503 to any method other than GET and HEAD, so OkHttp returns the 503 instead. JS no longer sees that header on such a response. The three rules are package-private static methods (mayResend, proceedOnce, withoutRetryAfter) so ReactNativeBlobUtilResendTest can wire them into a real OkHttpClient against a local ServerSocket server and count what arrives. This branch had no JVM unit tests, so the library's build.gradle gains testImplementation junit 4.13.2 and nothing else; it does not reach apps that depend on the library. Each POST test fails with its part of the fix reverted (2 requests, or no failure where one is expected) and passes with it; the GET tests check that GET keeps OkHttp's retry. Backport of #505 to support/0.25.
1.1.1 fixed three Android behaviours that only unit tests covered; each now has a parity case, recorded on the Android 14 emulator: - fetch-no-resend (#504, PR #505): a POST whose connection drops before an answer rejects, and one that times out rejects ETIMEDOUT; the server counts one request each. The server gains /count, which counts per token, drops the connection on reset=1 and answers after ms=<n>. - android-download-listed (#390): a download through OkHttp listed in the Downloads app (android.downloadManager without useDownloadManager) resolves, with the defaults and with a title. It used to end the app. open.chooser (#486) was checked by hand: the system chooser (com.android.intentresolver ChooserActivity) comes up in front; a case cannot read the app while the chooser covers it.
Fixes #504.
On Android a request that failed after the server may have received it could be sent again without the app knowing, so a state-changing POST was acted on twice. Three paths in ReactNativeBlobUtilReq.kt:
Trade-offs:
timeoutfield in the response info was only ever true after a resend that succeeded. It is now always false on a response.The pooled socket case in the first trade-off is common. OkHttp only checks that a pooled connection is still open before a request other than GET once it has been idle for 10s, so a server that closes idle connections after 5s (the default for Node's http server and uvicorn) got a POST sent 5 to 10s after the previous request on a connection it had already closed, and the call failed with "unexpected end of stream". The second commit makes the shared ConnectionPool keep idle connections for 4s instead of 5 minutes, so the pool drops them before such a server does. The cost is a new connection for requests more than 4s apart; a server that closes idle connections in under 4s still leaves a window.
Tests: the rules are companion functions (mayResend, proceedOnce, withoutRetryAfter, newConnectionPool). ReactNativeBlobUtilResendTest wires them into a real OkHttpClient against a local ServerSocket server and counts the requests that arrive:
With the fix reverted, the four POST/exception tests fail (2 requests received, or a response where a failure is expected). Each part of the fix is covered by at least one of them. With newConnectionPool() returning the default ConnectionPool(), the 7s POST fails with "unexpected end of stream" and never reaches the server.
npm test (158 pass), npm run lint (0 errors, 23 warnings as before) and npm run test:android (96 tests) pass.
ZEUS ships the same change as a postinstall patch against 0.25.1. On device, before the patch, a held reply past the timeout reached the server 2 times, a dropped reused connection 4 times, and a 503 with Retry-After: 0 2 times. After the patch, each was sent once. ZEUS also ships the 4s pool: against a real Node/Express server (BlueWallet LndHub, 5s keep-alive) on an Android 15 emulator, a POST 7s after a GET failed with "unexpected end of stream" and never reached the server without it, and went out on a new connection and succeeded with it. The backport to support/0.25 is #506.