apps-android-commons icon indicating copy to clipboard operation
apps-android-commons copied to clipboard

[Feature]: Wait before automatically retrying uploads after HTTP 429 or 503

Open Medhansh-741 opened this issue 1 week ago • 3 comments

What is the user problem or growth opportunity you want to see solved?

When an upload fails with 429 or 503, the app retries it automatically the next time you come back to it. There's no check on how long ago it failed, so it can retry before the wait the server asked for, and while Commons is still having trouble each retry fails again. After 10 of these the app stops retrying on its own.

I'd like automatic retries after these errors to wait first.

How do you know that this problem exists today? Why is this important?

This came up in #7340. On main (99bb33d), MainActivity.onResume() calls retryAllFailedUploads(), and ContributionsFragment.retryUpload() checks the network, permanent errors and the retry count, but not how long ago the upload failed.

I wrote a local test for this. It calls the same retry helper onResume() uses, on a mocked activity (so not the real lifecycle or app switching), with the real database, presenter, upload worker and upload client against a local server. I tested Retry-After: 120 with 429 and 503 on both the stash and publish requests, and 503 with an HTTP date or no header on the stash request only. In all six:

  • while the upload is queued, running the resume helper again doesn't retry it
  • once the worker saves the failure, the next resume queues it again right away
  • after 10 rounds the automatic limit is reached, with 11 failed requests

Where the server sent a wait, all 11 came well inside it. After the limit, the manual retry presenter the Failed screen uses still queued and ran another attempt (I didn't test the screen itself). Separately, a saved permanent error (filetype-banned) wasn't retried.

The errors are simulated and the test runs as fast as it can, so it shows there's no wait in the app, not how often this hits real users. After adding the linked test file to 99bb33d, ./gradlew :app:testProdDebugUnitTest --tests fr.free.nrw.commons.contributions.PersistedUploadRetryEvidenceTest gives 7 tests passed. They pass on main because they check the current behavior. Test: https://gist.github.com/Medhansh-741/c9feb6c0fce80fd719d3186053dd64f6

For the question in #7340 about how often the app gets Retry-After: the three-day check made 4,276 logged-out filename checks from a server, once a minute. All returned 200, with no 429 or 503 at all, so I still can't say how often Retry-After is sent on failed uploads.

Who will benefit from it?

People whose uploads fail while Commons is overloaded or rate limiting, and Commons itself, since the app would stop sending retries the server asked it not to send yet.

Anything else you would like to add?

One option: use Retry-After when it's there. When it's missing, the Wikimedia rate limit docs say to wait at least five seconds or back off exponentially, so a 5 second wait that grows each time could work.

Some things that would matter:

  • UnsuccessfulResponseInterceptor keeps only the status code and drops the headers. The publish request skips it and fails through Retrofit, so both need handling.
  • #7309 clears errorInfo when it requeues an upload and changes the work policy, so a wait based on the saved error would need to fit with that.
  • Captions and depicts retries use the same retry path and limit.

Medhansh-741 avatar Nov 21 '26 19:11 Medhansh-741

@nicolas-raoul could I work on this?

Why it matters

  • It happens for real: #7191 (uploads failing with "Unsuccessful response", the message for any HTTP error before #7328) was opened around the "Wikimedia API is unavailable" incident on Oct 28.
  • The rate limit docs say the API returns 429 when a client is over its limit and 503 when a backend is overloaded, both "typically" with Retry-After. Without it, wait at least 5 seconds or back off.
  • The test linked above uses up all 10 automatic retries in 1 to 2 seconds, even when the server asks to wait 120.
  • One case I missed in the issue: MediaWiki's own upload limit isn't a 429. It's the API error ratelimited, which as far as I can tell has no Retry-After. The app saves it as errorInfo = "ratelimited" (stash, publish) and retries it on the next resume, like any other failure.

Fix

  1. On 429, 5xx or ratelimited, the worker saves the earliest time the upload may be retried: Retry-After if sent, otherwise 5 seconds doubling each retry, up to a cap. Retry-After is dropped today in UnsuccessfulResponseInterceptor, so it has to be passed through there, and read from the Retrofit error for the publish request.
  2. That time goes in a new column. No existing field works: dateModified isn't set on most failure paths (ContributionDao.kt). It needs a real migration, because the app falls back to wiping the database on upgrade, which would delete queued uploads.
  3. On resume, retryUpload() skips uploads whose time hasn't come yet, without counting it as a retry. The next resume after that time retries as usual.
  4. Manual retry from the Failed screen stays as it is.

Tests: change the linked test to expect the wait, and add a migration test.

#7309 touches the same retry code (it clears errorInfo on requeue), so I'll keep this separate and rebase if it's merged first.

Medhansh-741 avatar Nov 21 '26 23:11 Medhansh-741

Assigned to you, @Medhansh-741! Thanks!

nicolas-raoul avatar Nov 21 '26 23:11 nicolas-raoul

@nicolas-raoul Opened #7393 for this, tested on device too (logs in the PR).

Medhansh-741 avatar Nov 22 '26 07:11 Medhansh-741