Skip to content

fix(library): keep a newer play when an older progress update arrives late - #64

Merged
GianniCarlo merged 5 commits into
mainfrom
fix/ignore-stale-progress
Nov 21, 2026
Merged

GianniCarlo merged 5 commits into
mainfrom
fix/ignore-stale-progress

Conversation

@GianniCarlo

Copy link
Copy Markdown
Contributor

Problem

The apps queue progress updates and can send them late. On the web, closing the tab within about a second of a pause or a seek leaves the update in the queue, and it goes out the next time the app opens. A phone coming back online does the same. updateLibraryItem keeps the last write, so that older position overwrites a newer play from another device.

Found while testing bookplayer-web#10 against production: six queued rewinds from the web, stamped 16:31:57, were waiting to overwrite a 16:35:52 play from Safari.

Fix

Every progress update already says when it was played (lastPlayDateTimestamp: the web sends whole seconds, iOS a fractional timeIntervalSince1970, Android its progress timestamp). Before saving, updateObject reads the row's last_play_date (by uuid, or by path when there's no uuid):

  • Played strictly before the stored play: the update keeps everything except actual_time, percent_completed, is_finish and last_play_date, so a title or speed in the same update still applies. It answers success, so the app clears the task instead of retrying it forever.
  • Same second or later: applied as before.
  • No play time, or 0: never held back. That covers mark-as-finished (iOS and Android send no timestamp) and clearing last played (sends 0).

The extra read only happens for updates that carry a play time.

Testing

  • tsc --noEmit passes.
  • 8 new database-backed tests in LibraryServiceStaleProgress.test.ts: a late older update is kept out and still answers success; newer and same-second updates apply; the rest of a stale update applies; a stale update can't un-finish a book; mark-as-finished without a timestamp applies; a never-played book takes the first update; the by-path check without a uuid. They run in CI's Postgres. Docker wasn't available locally.
  • The same cases passed locally against a faked DB layer.

After deploy

Open the web build that still holds the six stale updates, let them go out, and check in the database that the book keeps the newer Safari position.

🤖 Generated with Claude Code

… late

The apps queue progress updates and can send them late: a browser tab
closed before its queue drained, a phone back online. updateLibraryItem
kept the last write, so that older position overwrote a newer play from
another device. Every progress update says when it was played
(lastPlayDateTimestamp); an update played strictly before the stored
last_play_date now keeps everything except its position, progress,
finished flag and play time, and still answers success so the app clears
it from its queue. Updates without a play time (mark as finished, a
rename) or with 0 (a clear) are never held back.
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🟡 Claude PR Review — WARN

Adds a check in LibraryService.updateObject: a progress update (lastPlayDateTimestamp) played before the row's stored last_play_date keeps its other fields but drops actual_time, percent_completed, is_finish and last_play_date, and still answers 200. Authorization: the new read (getLibraryByUuid / getLibrary with exactly) and the write are both scoped to user.id_user and active: true, so there is no IDOR. Routes and middleware are unchanged, and no IAP or passkey code is touched. A failed read (null) lets the update through, which is a safe default. A stale progress-only body never becomes an empty update({}), because parseLibraryItemDb always sets key. The main risk is design: device clocks decide which update wins, so clock skew can silently drop real progress. The check is also a read followed by a separate write, so two simultaneous updates can still race.

Findings: 1 warn · 1 info

Findings not visible inline (no line in this diff, beyond the 25-comment cap, a comment the API refused, on a thread that could not be reopened, or on one a maintainer had the last word on)
  • 🟡 src/services/LibraryService.ts:185 — Device clocks decide which update wins. playedAt and storedPlayedAt come from different devices' clocks.

  • Small differences: pause on a phone whose clock is a few seconds fast, pick up on the web right away, and the web's real progress is dropped while the app still gets a 200.

  • Large differences: a device whose clock is hours ahead writes a future last_play_date. Every other device's progress is then silently dropped until real time catches up.

Fix: let a stored timestamp block an update only if it isn't in the server's future, e.g.

const now = Date.now() / 1000;
if (!(storedPlayedAt > playedAt) || storedPlayedAt > now + 60) return item;

Optionally add a few seconds of tolerance (storedPlayedAt - playedAt > 5) so a quick hand-off isn't dropped. Add a test with a future stored timestamp.

  • 🔵 src/services/LibraryService.ts:181 — Read-then-write race. The check reads the row, then updateLibraryItem writes in a separate query, with no transaction or condition between them. Two updates for the same item arriving together (two devices draining their queues) can still end with the older one saved. It also adds a read to every progress update.

Fix: do the check inside the write. Add a LibraryDB method that updates the progress columns only where last_play_date is null or last_play_date <= ?, and apply the other fields without that condition. That closes the gap and removes the extra read.

Model claude-opus-5-5 · run log · 0 new · 0 carried over · 0 resolved · advisory (a human should still review). Findings are de-duplicated across pushes; an earlier finding closes only when the verification pass judges it against the current code — fixed, no longer applicable, accepted by a maintainer, or a duplicate of a finding reported on this push.

Log when an older progress update is held back. sync_operations skips
playback-only updates, so this is the only trace when someone asks why
their progress didn't move.
@GianniCarlo

Copy link
Copy Markdown
Contributor Author

Replies to the review on 2cd6500 (neither finding had a line in the diff, so they're answered here):

  • 🟡 Check and write not atomic. Not fixing. The race needs a late older update and a newer play of the same book from another device to land within the few milliseconds between the read and the write. The late update goes out when the user opens the app they're about to use, not while they're playing on the other device. If it ever happens, the playing device's next pause or seek writes the newer position again.
  • 🔵 1. Silent drop. Fixed in 7750abe. sync_operations skips playback-only updates, so a log line is the only trace when someone asks why their progress didn't move.
  • 🔵 2. Timestamp meaning. Checked against the apps. iOS markAsFinished and jumpToStart send no play time (LibraryService.swift), and clearing last played sends lastPlayDate: 0. Android's finished() sends no timestamp, and its clear sends 0. A full-item PUT / upload carries the item's play date, but it never reaches updateObject. The only way an older play time rides along with a mark-as-finished is a merge into a still-pending progress task, while another device played the same book in between. That needs the phone offline the whole time, and marking it finished again fixes it.

Comment thread src/services/LibraryService.ts Resolved
Comment thread src/services/LibraryService.ts Resolved
Comment thread src/services/LibraryService.ts Resolved
Log a held-back update at warn: production drops info. A stored play
dated in the future (a device clock set ahead) no longer holds updates
back, so it can't freeze a book's progress on every other device.
Name the held-back log after the method that raises it.
@GianniCarlo

GianniCarlo commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Replies to the round-3 notes on 337f37b (none had a line in the diff):

  • 🔵 Read then write, not atomic. Not changing it, as answered above: the race needs two updates for the same book within milliseconds, and the playing device's next pause or seek repairs it.
  • 🔵 A device clock running behind. Not changing it. Phones, Macs and PCs set their time from the network by default, so devices differ by seconds. Even a clock minutes behind only delays that device's progress until its clock passes the other play. Its next update after that applies. A margin would let a queued update that much older overwrite a newer play, which is the case this PR fixes.
  • 🔵 Log origin. Fixed in 417706a: LibraryService.withoutStaleProgress.

Comment thread src/services/LibraryService.ts Resolved
Two 5% samples of production library_items (about 14k played books)
found no play time ahead of the server clock: devices set their time
from the network, so the case it guarded doesn't happen.
@GianniCarlo

Copy link
Copy Markdown
Contributor Author

Replies to the review on ebd8035. Neither finding had a line in the diff, and neither leads to a change:

  • 🟡 Device clocks decide which update wins. That's the design. The future-clock check was removed on purpose: two 5% samples of production (about 14k played books) had no play time ahead of the server clock. Devices set their time from the network, so they differ by seconds. A hand-off within those seconds only loses the saves made inside them; the next save after that applies.
  • 🔵 Read-then-write race. Answered above: it needs two updates for the same book within the milliseconds between the read and the write. The extra read is a single-row lookup by uuid, and only for updates that carry a play time.

@GianniCarlo
GianniCarlo merged commit a41928f into main Nov 21, 2026
4 checks passed

This branch was successfully deployed

1 active deployment
reviewer — ebd80356 Deployed Nov 21, 2026 by GianniCarlo via review #136
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