DEV Community

Cover image for The Upload Succeeded. Why Did WorkManager Send It Again?
KlarVPN
KlarVPN

Posted on AI-assisted

The Upload Succeeded. Why Did WorkManager Send It Again?

An Android app starts uploading a photo. The server saves it. Before the app receives the response, the connection drops during a VPN reconnect.

WorkManager runs the upload again. Now the server has two copies.

From the worker’s perspective, the first attempt ended with a network error. From the server’s perspective, it succeeded. The app has to handle the possibility that both are true.

The fix starts with one rule: give each logical upload a stable ID, reuse it on every attempt, and have the server enforce uniqueness.

What WorkManager can tell you

A NetworkType.CONNECTED constraint lets WorkManager schedule the job when Android reports connectivity that meets the constraint. If the constraint stops being met while the job runs, WorkManager can stop the worker and run it later. Your own Result.retry() can also schedule another attempt, with backoff.

Neither event tells you what the server did with the previous request. A timeout can happen before the server receives the file, partway through the transfer, or after the server commits the upload. Even if the VPN reconnects quickly enough that the network constraint stays satisfied, the existing HTTP connection can still fail.

That gap is where duplicates happen. Backoff helps control how often you retry. The upload ID makes retrying safe.

Create the upload ID when the user queues the file

Generate a UUID when the user initiates an upload. Save it alongside the account and a reference to a staged copy of the file. Keep the staged bytes unchanged while the upload is pending.

If the worker runs again after a timeout or process restart, it reads the same record. If the user taps Retry in your app, enqueue work for that record again. Generate a new ID when the user starts a different upload, even if they choose the same file.

This example sends the file using PUT /uploads/{uploadId}. The client chooses the resource ID. The server uses the authenticated account and that ID to decide whether it has already accepted the upload.

Keep the Kotlin worker focused on one attempt

The example assumes your app supplies two dependencies through a WorkerFactory: a local store for pending uploads and a cancellable HTTP client. UploadApi.put() returns the raw HTTP status, authenticates as the account that queued the upload, and throws IOException for transport failures. The server returns 201 for the initial commit and 200 for a replay of the same upload.

import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import java.io.File
import java.io.IOException

data class PendingUpload(
    val id: String,
    val accountId: String,
    val file: File
)

interface UploadStore {
    suspend fun find(id: String): PendingUpload?
}

interface UploadApi {
    suspend fun put(upload: PendingUpload): Int
}

class UploadWorker(
    context: Context,
    params: WorkerParameters,
    private val store: UploadStore,
    private val api: UploadApi
) : CoroutineWorker(context, params) {

    override suspend fun doWork(): Result {
        val id = inputData.getString("uploadId")
            ?: return Result.failure(workDataOf("reason" to "missing_id"))

        val upload = store.find(id)
            ?: return Result.failure(workDataOf("reason" to "missing_upload"))

        val status = try {
            api.put(upload)
        } catch (_: IOException) {
            return retryOrReportUnconfirmed()
        }

        return when (status) {
            200, 201 -> Result.success()
            408, 500, 502, 503, 504 -> retryOrReportUnconfirmed()
            else -> Result.failure(workDataOf("httpStatus" to status))
        }
    }

    private fun retryOrReportUnconfirmed(): Result =
        if (runAttemptCount < 4) {
            Result.retry()
        } else {
            Result.failure(workDataOf("reason" to "upload_unconfirmed"))
        }
}
Enter fullscreen mode Exit fullscreen mode

The worker lets coroutine cancellation propagate. The HTTP client should cancel its in-flight call when the coroutine is cancelled. The status list is an example for this endpoint. An API that returns 429 with Retry-After needs a policy that honors the server’s delay.

upload_unconfirmed matters too. Reaching the retry limit means the app still lacks confirmation. It should keep the upload ID so it can check the server’s record or let the user retry the same upload later.

Use unique work to avoid duplicate jobs on the device

import androidx.work.BackoffPolicy
import androidx.work.Constraints
import androidx.work.ExistingWorkPolicy
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.workDataOf
import java.util.concurrent.TimeUnit

fun enqueueUpload(workManager: WorkManager, uploadId: String) {
    val request = OneTimeWorkRequestBuilder<UploadWorker>()
        .setInputData(workDataOf("uploadId" to uploadId))
        .setConstraints(
            Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build()
        )
        .setBackoffCriteria(
            BackoffPolicy.EXPONENTIAL, 30, TimeUnit.SECONDS
        )
        .build()

    workManager.enqueueUniqueWork(
        "upload:$uploadId",
        ExistingWorkPolicy.KEEP,
        request
    )
}
Enter fullscreen mode Exit fullscreen mode

KEEP avoids enqueuing another unfinished job with the same name. A single job can still make multiple HTTP attempts. The server must recognize those attempts as belonging to the same upload, even if they arrive close together.

Enforce uniqueness where the file is committed

For a small-file test endpoint, you can store the bytes and metadata in the same PostgreSQL row. That makes the database commit the point at which the upload becomes durable.

CREATE TABLE uploads (
    account_id uuid NOT NULL,
    upload_id uuid NOT NULL,
    content_sha256 bytea NOT NULL,
    content bytea NOT NULL,
    PRIMARY KEY (account_id, upload_id)
);

INSERT INTO uploads (account_id, upload_id, content_sha256, content)
VALUES ($1, $2, $3, $4)
ON CONFLICT (account_id, upload_id) DO NOTHING
RETURNING upload_id;
Enter fullscreen mode Exit fullscreen mode

Compute the hash on the server from the received bytes. If the insert returns a row, commit the transaction before sending 201. If it returns zero rows, read the existing row for that account and upload ID in a separate statement under PostgreSQL’s default READ COMMITTED isolation. Return 200 if the hash matches, or 409 if the ID was reused for different content.

The unique constraint handles two attempts arriving at nearly the same time. A backend that only checks for an existing row before inserting leaves a gap in which both attempts can see no row and proceed.

If you use object storage, the database constraint protects the row. Give the object write its own idempotent behavior, such as a stable object key with a conditional create, and make sure a retry can reconcile an object saved before the database update.

Test the failure that creates the duplicate

Start with a controlled test. On a staging endpoint you own, commit the file and then deliberately close the first response before the client can receive it. On the next attempt, let the endpoint respond normally.

Check three things in the client and server logs:

Check Expected result
Upload ID on the first and second requests Both requests use the same ID
Server records for that account and ID One committed upload
Worker result after the replay Success after the server acknowledges the existing upload

Also try reusing the ID with different bytes. The server should return 409 and keep the original file.

For a physical-device check, use the same staging endpoint and a VPN for Android. Try a reconnect during an upload and see whether the request fails, completes, or gets retried. The outcome can vary with timing. The staging test is how you reliably exercise the case where the server committed the file and the client lost the response.

If you currently generate a new upload ID inside doWork(), start there. Move ID creation to the point where the user queues the file, then run the lost-response test. You should be able to see two requests in the logs and one upload on the server.

Top comments (2)

Collapse
 
mosmox_studio profile image
Mosmox Studio •

The stable upload ID is the right call. One extra pitfall worth noting: if the staged file is edited or re-compressed between attempts, the server can see the same ID with different bytes, so it helps to store a content hash alongside the ID and return a conflict instead of silently accepting the replay.

Collapse
 
klarvpn profile image
KlarVPN •

Good catch. Stage the file once and reuse the exact bytes on retry. Same ID but different bytes? Return 409 Conflict.