Client/server sync pitfalls (deletes, timestamps, merge policies)
syncofflinetombstoneshard-deletepollingdraftslocal-storageconflict-resolution
Apps that keep user data locally (drafts, personal bests, settings) and sync it to a server accumulate the same handful of bugs. These were all found in Run (Flutter game with a PHP API) during 2026-07 and 2026-10 audits; the lessons are general.
Bugs and their fixes
- Deletes resurrect. The server filtered out tombstoned rows, and the upsert set
deleted_at = NULL, so a device that still had the item re-uploaded it and undeleted it. Fix: expose deletions (Run addedGET /drafts/deletedreturning[{draft_id, deleted_at}]), have clients delete locally when the server's tombstone is newer, and never let a plain upsert clear a tombstone. - Timestamp ping-pong. Pulling a draft from the server and saving it locally stamped a new
updatedAt, which made it look newer and triggered an upload, and so on. Preserve the server timestamp when saving pulled data. - Saving on exit clobbers newer copies. The editor re-stamped and saved every draft on exit, so an old device opening a level briefly overwrote work done elsewhere. Save only when content actually changed (compare a fingerprint of the content minus timestamps).
- Unparseable server data. Never overwrite a server copy you could not parse; skip it and keep both.
- Four different merge policies for four data types meant nobody could predict outcomes. Pick one policy (revision numbers with 409 Conflict on stale writes is the robust option) and apply it everywhere.
- Sync only at login leaves long sessions diverged; sync on resume and after edits too.
- Partial sync: Run synced built-in level PBs but not legacy/world/community PBs; check every data family is covered.
Delete propagation without a tombstone table (tldr DM hard delete)
tldr v1.48.0 (2026-08-15) added sender-only hard delete of single DM messages. Design and the races
review found:
- DELETE /dm/messages/{id} does the ownership check inside the statement (WHERE id=? AND
sender=?) and returns 404 for both unknown ids and other people's messages, so ids cannot be
probed. The client treats 404 as success, which makes retries idempotent.
- There is no tombstone table. Other clients learn of a delete only because the chat screen does a
full fetch every 5th poll (about every 20 s) and removes ids that were known before the fetch
but are absent from the snapshot. Do not optimize that full fetch away without adding a
deleted_ids feed, or deletes silently stop propagating.
- Capture "known ids" before the await: a stale snapshot must never judge messages that arrived
during the fetch (that bug deleted just-sent messages).
- Keep a session tombstone set of locally deleted ids so an in-flight poll cannot resurrect them.
- Merge, never overwrite, in the final state update; dedupe the optimistic bubble at swap time.
- The fetch must return null on failure and [] for a genuinely empty thread, or deleting the
last message never propagates.
- Attachments could not be deleted with the message: their URLs live inside E2E ciphertext and the
server has no message-to-file ownership record. Never add delete-by-filename to an
unauthenticated capability-URL route shared with public posts.
- Known leftovers: a push already delivered is not retracted; deleting a thread's only message
leaves its last-message date NULL and sinks it to the bottom of the list.
- Terminology lesson: Justin's "delete a DM" meant one message, not a conversation.
Local data must be per user
Run's logout kept local PBs, drafts and skins, and the next account to log in on that browser uploaded the previous user's PBs as its own. Namespace local storage by user id (or wipe on logout, a product decision). Same class of bug as singleton caches surviving logout: Singleton services, session caches, and cross-user data leaks in Flutter.
Local storage hygiene
- Do not keep multi-MB blobs in one SharedPreferences/localStorage key (Run stored every web draft in one key); browsers enforce quotas and one corruption loses everything.
- Version the stored schema; decode each entry independently; back up corrupt data instead of crashing (Game content data models (stable ids, schemas, serialization)).
- Stale-while-revalidate for lists: show the cached copy immediately, refresh in the background, and only re-render if a change signature differs. On first launch with no cache, await the network with a cap (Run: 8 s) instead of showing "nothing available".
Auth expiry
Clear the session on 401 only (server uses 403 for permission denials), and only when the failing request actually carried a token; otherwise anonymous calls log people out.
See also
- Autosaved drafts that come back from the dead (lifecycle pitfalls)
- Slim 4 PHP APIs (and migrating from Limonade)