Skip to content

RAD-17385 Android: explicit timeouts, and keep a response read before a stop - #84

Open
dmurphy5 wants to merge 3 commits into
masterfrom
dylan/android-timeouts-landed-response
Open

dmurphy5 wants to merge 3 commits into
masterfrom
dylan/android-timeouts-landed-response

Conversation

@dmurphy5

Copy link
Copy Markdown

What was wrong

  1. Timeouts. The Android HTTP client set only a 24 h call cap. OkHttp's defaults then applied: 10 s to connect, 10 s of read silence, 10 s per write. The read timeout also covers the wait for the server's answer after the last body byte. A server that took more than 10 s to answer still stored the request, but the phone gave up and sent it again. The server could then store it twice.
  2. A response read just before a stop. awaitResponse used a plain suspendCancellableCoroutine. Sometimes OkHttp finished reading the response, and then a stop (a system stop, or pause()) took effect before the coroutine ran again. In that case kotlinx dropped the response. Its docs say the coroutine "will not resume successfully, even if CancellableContinuation.resume was already invoked". The request was then sent again: at once after a system stop, or at resume() after a pause.

What changes

  • UploadTransport.kt: connect 15 s, read 60 s, write 60 s. The 24 h call cap stays, and its comment now says what it covers.
    • Read and write are 60 s to match iOS, which uses the URLSession default of 60 s.
    • Connect is shorter on purpose. OkHttp tries the next address of a host only after a connect timeout, so a host with several addresses that do not answer holds a transfer slot for that many timeouts. A connect timeout cannot make a duplicate, because nothing was sent yet.
  • UploadUtils.kt: new awaitLanded(register, cancel), taken out of awaitResponse. It saves the result in an AtomicReference before the resume. On a stop it returns the saved result if there is one. A stop with no result cancels the call and throws, as before. A failure that came first is thrown as before.
  • A chunked part gets the same fix, because it goes through the same send. Its accepted flag is saved.
  • Comments in EntryRun.attempt, SimpleTransfer and ChunkedTransfer say that nothing may suspend between the returned response and ops.settle or markAccepted. Tests pin this.

Not changed: retryOnConnectionFailure (still OkHttp's default, on), iOS, part size, the retry table.

Behavior change

  • The read timeout now allows 60 s of silence after the body, not 10 s, before the attempt fails as a transport failure.
  • A dead connection is found after 60 s, not 10 s. It holds its transfer slot for that time.
  • The connect timeout is 15 s per address, not 10 s.
  • A response that OkHttp finished reading before the stop took effect now goes through the usual rules. An accepted response settles the entry, and an accepted part keeps its flag. cancel() is unchanged: the entry still settles cancelled.

What it does not fix

  • If a response has not been read when the stop lands, it is still lost, and the request is sent again. A later change will let a request whose whole body was sent run to the end.
  • If cancel() and a 200 land together, the entry correctly ends cancelled with one outcome. But the run still ends its progress as completed. This is open.

Tests

  • UploadHttpClientTest: the client's connect, read, write and call timeouts.
  • AwaitLandedTest checks:
    • a result that landed before a stop is returned.
    • a stop with no result cancels and throws.
    • a stop before the call starts cancels it.
    • a result or a failure with no stop comes through.
    • a failure that came before a stop is thrown as it is.
  • AwaitResponseTest runs okhttpSend on real OkHttp, against a small local socket server, with a dispatcher the test drives by hand. No new dependency. It checks:
    • a landed response survives a stop, and the connection is used again.
    • a stop with no response ends the call.
    • a stop before the call sends nothing.
    • a body that fails to read is not kept as a response.
  • TransferGateStopTest: the transfer gate returns the response on a stopped job, in both lanes.
  • EntryRunTest covers three cases:
    • a response that lands as the run is stopped still settles.
    • a part that lands as the run is stopped keeps its flag.
    • a 500 that lands at a stop is counted once, and the entry goes back to queued.
    • The test host's sleep() now throws on a stopped job, as a real delay does.

Before the fix, the timeout test and the landed-result test failed. To check the tests, each fix was broken on purpose, and at least one test failed each time:

  • each timeout set back to its default or changed.
  • the landed result not saved, or not returned.
  • the cancel hook removed, or call.cancel() replaced by nothing.
  • a failed body read handed back as a response.
  • a suspension added after the gate, before ops.settle, in SimpleTransfer, or before markAccepted.
  • the test host's sleep() no longer throwing.

Checks run locally, all exit 0:

  • yarn lint:ci
  • yarn typecheck
  • yarn test (305 tests)
  • ./gradlew :react-native-background-upload:testDebugUnitTest (439 tests)
  • swift test in ios/ (383 tests, 1 skipped)

An independent review was applied. Part of RAD-17385.

🤖 Generated with Claude Code

dmurphy5 and others added 3 commits November 17, 2026 16:38
…stop

Set connect 30 s, read 60 s and write 60 s on the OkHttp client. Before,
only the 24 h call cap was set, so OkHttp's 10 s defaults applied, and a
server that took more than 10 s to answer made the phone send again.

Extract awaitLanded() from awaitResponse(). It saves a result before the
resume, and a stop returns that result when it is there. A plain
suspendCancellableCoroutine drops it. A stop with no result still cancels
the call and throws. A chunked part saves its accepted flag the same way.
Comments and two EntryRunTest cases pin that nothing suspends between the
returned response and ops.settle or markAccepted.

retryOnConnectionFailure is unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Connect timeout 15 s, not 30 s. OkHttp tries the next address of a host
only after a connect timeout, so a host with several addresses that do not
answer holds a transfer slot for that many timeouts. A connect timeout
cannot make a duplicate, because nothing was sent yet. Read and write stay
60 s.

The CHANGELOG and the awaitLanded comment now say what the code does: it
keeps a response OkHttp finished reading before the stop took effect.
Before, the request was sent again at once after a system stop, and at
resume() after a pause. A response not yet read when the stop lands is
still lost.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ackoff

- AwaitResponseTest runs okhttpSend against a local socket server with a
  manual dispatcher. A landed response survives a stop and the connection
  is reused. A stop with no response cancels the call. A stop before the
  call sends nothing. A body that fails to read is not kept as landed.
- TransferGateStopTest: the gate returns the block's value on a stopped
  job, in both lanes.
- FakeHost.sleep() now throws on a stopped job, as a real delay does (the
  same form as on dylan/android-stop-not-failure). New test: a 500 that
  lands at a stop is counted once and the entry goes back to queued.

No new dependency.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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