Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
## Unreleased

Behavior change (iOS): a late 2xx now completes an entry that earlier
versions left running and sent again. After a relaunch, iOS waits 10 s for
the result of an upload that finished while the app was not running. When no
result came in that time, earlier versions sent the request again as a new
attempt and dropped the first attempt's result if it arrived later. So a
first attempt that the server accepted was always followed by a second send.

Now the library keeps the first attempt's task key, marked as a late-result
key. When that result arrives later and is accepted (a 2xx, or a status the
definition's `accept` rules allow), the entry completes with the first
attempt's response, and the second attempt's task is cancelled. If the
second task had not started yet, nothing is sent twice. If it already sent
its request, the server still gets two requests, as before. A late failure
or cancel of the first attempt is ignored, and the second attempt goes on. A
first attempt's result that arrives after the entry settled is ignored, so
the app gets one outcome.

Every late-result key of a request is removed when the request settles, when
its row is forgotten (the ack of a `completed` or `cancelled` outcome, or
`cancel()` on a settled row), and when a same-id `mutate()` replaces its
body. Before, the end of the 10 s wait deleted every key of the waited
attempt, including keys of tasks the library had cancelled (superseded or
paused). Now such a key stays until its task's cancel callback removes it,
or until the row is gone and the next launch prunes it.

On iOS, a simple request's outcome now carries the `requestId` of the
attempt whose result settled it, so its response and `requestId` belong to
the same request. Before, it carried the last attempt's `requestId`, which
after a late result could be a task that was cancelled before it began.
`attempts` still counts every attempt. iOS chunked requests already did
this. On Android a simple request's last attempt is always the one that
settled it; a chunked request's `requestId` is the last part attempt that
started. Android and iOS chunked parts are otherwise unchanged.

## 11.3.0

Added `useRequests(filter?)` and `useRequestsWithProgress(filter?)`, React
Expand Down
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,13 @@ above lets the library journal their outcomes.
**Relaunch.** When the app comes back and an entry's task is gone but a
completion may still be in flight from the daemon, the library waits up to
10 s for it before it sends again. This is what stops a request that finished
while the app was dead from being sent twice.
while the app was dead from being sent twice. On iOS, when that result comes
after the wait, it still counts: an accepted result completes the entry with
the first attempt's response and cancels the new attempt's task. The
outcome's response and `requestId` then both belong to the first attempt;
`attempts` counts every attempt. A failure that comes late is ignored, and
the new attempt goes on. If the new task already sent its request, the server
gets the request twice.

# Reliable delivery

Expand Down Expand Up @@ -367,8 +373,11 @@ its place in the list:
- A same-id `mutate()` that reopens a settled entry starts every count at 0.
A same-id `mutate()` on a live entry keeps the counts.

Every attempt sends an `X-Request-Id` header, minted per attempt. `Meta.requestId`
carries the last one.
Every attempt sends an `X-Request-Id` header, minted per attempt.
`Meta.requestId` carries the one of the attempt whose result settled the
entry, or the last attempt's when no attempt settled it (a cancel, an
expiry). For a chunked request on Android it is the last part attempt that
started.

# API

Expand Down
2 changes: 2 additions & 0 deletions ios/QueueCoordinator+Enqueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ extension QueueCoordinator {
n.headerGeneration = settings.headerGeneration
n.state = paused ? .paused : .queued
try saveOrDiscard(n, staged: staged)
// A late result of the old body must not complete the new one.
removeLateResultKeys(p.id)
store.removeV9Manifest(p.id)
store.sweep(n) // deletes the old body
publish(n)
Expand Down
14 changes: 14 additions & 0 deletions ios/QueueCoordinator+Outcomes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ extension QueueCoordinator {
journaled = false
}
cancelTasks(id, purpose: .superseded)
removeLateResultKeys(id)
chunked.stop(id)
emitProgress(id, sent: e.bytesSent, total: e.totalBytes)
e.settledEventId = event.eventId
Expand Down Expand Up @@ -108,12 +109,23 @@ extension QueueCoordinator {
n.nextAttemptAt = nil
n.authParked = false
cancelTasks(e.id, purpose: .superseded)
removeLateResultKeys(e.id)
chunked.stop(e.id)
disarmExpiry(e.id)
commit(n, emit: false)
return n
}

/// Removes every late-result key of `id` (see withoutTask), whatever its
/// attempt or generation. Called when the id settles, is forgotten, or
/// gets a different body: a late result can then no longer complete what
/// the id holds, and a kept key must not reach a later use of the id. No
/// task in this process holds such a key, so none owes a cancel callback
/// that would read it. Keys of other purposes stay until their callback.
func removeLateResultKeys(_ id: String) {
taskMap.removeAll { _, m in m.id == id && m.purpose == .lateResult }
}

/// Relaunch, before any task is matched: every live row whose own
/// generation already has an unacked outcome takes that outcome, so it is
/// never sent again.
Expand All @@ -129,6 +141,7 @@ extension QueueCoordinator {
/// unacked outcomes (cancel on a settled entry).
func forget(_ id: String, dropEvents: Bool) {
cancelTasks(id, purpose: .superseded)
removeLateResultKeys(id)
chunked.stop(id)
store.remove(id)
index.remove(id)
Expand Down Expand Up @@ -163,6 +176,7 @@ extension QueueCoordinator {
}
pendingJournal = pendingJournal.filter { $0.value.id != id }
cancelTasks(id, purpose: .superseded)
removeLateResultKeys(id)
chunked.stop(id)
index.remove(id)
disarmExpiry(id)
Expand Down
30 changes: 21 additions & 9 deletions ios/QueueCoordinator+Reconcile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ extension QueueCoordinator {
let meta = taskMap.meta(forKey: task.key)
// A task the library replaced or dropped, still live because the
// process died before the daemon applied the cancel. Never adopt it:
// its replacement, or the re-issue below, drives the entry.
if meta?.purpose == .superseded {
// its replacement, or the re-issue below, drives the entry. A kept
// late-result key on a live task becomes superseded: the task now owes
// a cancel callback, so its key must stay until that callback, and a
// settle removes only late-result keys.
if let purpose = meta?.purpose, purpose.isOutdated {
if purpose == .lateResult { taskMap.setPurpose(.superseded, forKey: task.key, id: meta?.id ?? "") }
task.cancel()
continue
}
Expand Down Expand Up @@ -182,13 +186,21 @@ extension QueueCoordinator {
guard let self, let current = self.index.entry(e.id), current.state == e.state,
current.generation == e.generation, current.attempts == e.attempts,
!self.liveTasks.values.contains(where: { $0.id == e.id }) else { return }
// No replay moved the entry: the task is lost. Its key would send
// every later launch through this wait again.
self.taskMap.removeAll { _, m in
m.id == e.id && m.generation == e.generation && m.attempt == e.attempts
}
// It may have run, so the next attempt gets a new ordinal: a late
// replay of this one is then dropped as stale.
// No replay came in time. The task may be lost, or its result may
// still arrive later. Keep its keys as late-result keys instead of
// deleting them. A late accepted result then completes the entry and
// cancels the new attempt's task, and a late failure or cancel is
// dropped (taskCompletedLocked). The wait covers only the current
// attempt, so once the re-issue below moves the entry to a new
// attempt, a later launch does not wait for these keys again. A key
// already marked superseded or pause keeps its purpose: its task may
// still owe a cancel callback.
let stale = self.taskMap.keys(where: {
$0.id == e.id && $0.generation == e.generation && $0.attempt == e.attempts
&& ($0.purpose ?? .attempt) == .attempt
})
self.taskMap.setPurpose(.lateResult, for: stale.map { (key: $0, id: e.id) })
// It may have run, so the next attempt gets a new ordinal and request id.
self.reissue(current, advanceAttempt: true)
}
}
Expand Down
33 changes: 23 additions & 10 deletions ios/QueueCoordinator+Simple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,28 @@ extension QueueCoordinator {
// entry, or an older one the library replaced (see below).
guard case .request(let id, let generation, let attempt) = owner,
var e = index.entry(id), !e.legacy, !e.isChunked, e.generation == generation,
e.attempts == attempt || (meta?.purpose == .superseded && attempt < e.attempts) else { return }
e.attempts == attempt || (meta?.purpose?.isOutdated == true && attempt < e.attempts) else { return }

let cancelled = RetryClassifier.isCancellation(c.error)
if cancelled, meta?.purpose == .pause || meta?.purpose == .superseded { return }
if cancelled, meta?.purpose == .pause || meta?.purpose?.isOutdated == true { return }
let accepted = c.error == nil
&& c.statusCode.map { UploadOutcome.isAccepted($0, body: c.body, accept: e.accept) } == true
// A replaced task that finished before its cancel took effect. Its
// replacement drives the entry, unless this one landed while the entry
// is live. The server has the body then, so the entry completes, even
// when the replacement failed and the entry moved to a later attempt.
if meta?.purpose == .superseded && (!accepted || !e.isLive) { return }
// A replaced task that finished before its cancel took effect, or an
// older attempt whose result came after the relaunch grace gave up on it
// (a late-result key, see withoutTask). The newer task drives the entry,
// unless this one landed while the entry is live. The server has the
// body then, so the entry completes and settle cancels the newer task,
// even when the entry moved to a later attempt. A failure is dropped: it
// must not disturb the newer attempt.
if meta?.purpose?.isOutdated == true && (!accepted || !e.isLive) { return }
// The outcome names the request whose response settles it. A late
// result belongs to an older attempt than the entry's last one. Only an
// accepted result settles the entry as completed, so only it may change
// the entry's request id.
if accepted, let requestId = meta?.requestId, requestId != e.lastRequestId {
e.lastRequestId = requestId
index.upsert(e)
}
// A delayed task that ended before its begin date, with no response and
// no body byte sent, never reached the daemon: nsurlsessiond refused the
// connection (a simulator build with no entitlements does this). It is
Expand Down Expand Up @@ -260,8 +271,10 @@ extension QueueCoordinator {
return chunked.partWillBegin(id: id, part: part, incarnation: incarnation, key: key, meta: meta)
}
// A task the library already replaced (a session move keeps the
// attempt ordinal, so the ordinal alone cannot tell them apart).
guard meta?.purpose != .superseded,
// attempt ordinal, so the ordinal alone cannot tell them apart), or
// one the relaunch grace gave up on. Cancel it: its key becomes
// superseded below, because it now owes a cancel callback.
guard meta?.purpose?.isOutdated != true,
case .request(let id, let generation, let attempt) = owner,
var e = index.entry(id), !e.isChunked, e.generation == generation,
e.attempts == attempt, e.state == .queued || e.state == .running, !settings.isPaused(e.key),
Expand All @@ -285,7 +298,7 @@ extension QueueCoordinator {
/// a delayed retry that began while the app was dead to running.
func taskProgress(key: String, description: String?, sent: Int64, expected: Int64) {
let meta = taskMap.meta(forKey: key)
guard meta?.purpose != .superseded, let owner = TaskOwner.resolve(description: description, meta: meta),
guard meta?.purpose?.isOutdated != true, let owner = TaskOwner.resolve(description: description, meta: meta),
throttle.shouldEmit(owner.id, now: now()) else { return }
queue.async {
switch owner {
Expand Down
2 changes: 1 addition & 1 deletion ios/QueueCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ final class QueueCoordinator {
var parts: [String: [ChunkedCoordinator.Replaced]] = [:]
for (key, owner) in liveTasks {
let meta = taskMap.meta(forKey: key)
guard meta?.foreground != true, meta?.purpose != .superseded, owner.task.isLive,
guard meta?.foreground != true, meta?.purpose?.isOutdated != true, owner.task.isLive,
owner.task.bytesSent == 0, !owner.task.hasResponse,
let resolved = TaskOwner.resolve(description: owner.task.taskDescription, meta: meta) else { continue }
switch resolved {
Expand Down
12 changes: 12 additions & 0 deletions ios/TaskMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ final class TaskMap {
case attempt
case pause
case superseded
/// A simple task the relaunch grace gave up on: it was not live, and
/// no result came in the wait. The entry moved to a new attempt, and the
/// key stays so a late accepted result can still complete the entry. No
/// task in this process holds it, so it owes no cancel callback, and
/// every key of this purpose for an id is removed when that id settles,
/// is forgotten, or gets a different body. An older build reads it as
/// nil and drops the late result.
case lateResult

/// A task that no longer drives its entry: its failure and its cancel
/// produce nothing, and only an accepted result on a live entry counts.
var isOutdated: Bool { self == .superseded || self == .lateResult }
}

struct Meta: Codable, Equatable {
Expand Down
Loading
Loading