Correctness bugs: heartbeat cache, query cache invalidation, commit rollback state

Open
#825 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@0xbrayo is already working on this.

Since Oct 20, 2026.

  • #830 by @0xbrayo — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
rust
Domain
backend, databases

Research direction

The issue identifies four areas: aw-datastore/src/worker.rs, aw-server/src/android/mod.rs, aw-server/src/endpoints/query.rs, and commit handling in work_loop. Start by reviewing the linked fixes #826, #827, #830, and #831 and the described repro steps. Done means each cache or in-memory state remains consistent after the relevant delete, write, race, or failed commit.

Written by the indexing model from the issue text.

Description

enhancement

A correctness review of master (at 1d101d4) found the following bugs. Fixing PRs will be linked to the items below as they land.

  • Stale heartbeat cache after deleting events, deleting a bucket, or renaming a bucket. — fix: #826
    The datastore worker caches each bucket's last heartbeat (last_heartbeat in aw-datastore/src/worker.rs). It is invalidated on InsertEvents, but not on DeleteEventsById, DeleteBucket or RenameBucket. The next heartbeat with the same data then merges into a row that no longer exists, and fails with:

    InternalError("replace_last_event matched 0 rows for event_id 1 - cache/DB inconsistency")
    

    That heartbeat is lost and the client gets a 500. The heartbeat after that recovers, because the bad cache entry is removed before the error. Repro: send two heartbeats, then either delete the latest event or delete and re-create the bucket, then send another heartbeat with the same data. This is easy to hit by deleting the current event from the web UI while a watcher is running.

  • On Android, JNI writes skip query-cache invalidation. — fix: #831
    The query cache is enabled by default on Android too (server_state.query_cache_enabled = server_config.query_cache in aw-server/src/android/mod.rs). But the JNI functions heartbeat, createBucket, migrateHostname and the bucket renames write straight to the datastore. The cache is only invalidated in the HTTP handlers (endpoints/bucket.rs, endpoints/import.rs). So when the usage watcher backfills events for a past day after the web UI has cached that day, the stale result is served until the app restarts. Bucket creates and renames over JNI also leave cached find_bucket results wrong.

  • A race in the query endpoint can cache a stale result when a read-only reader is in use. — fix: #827
    query() in aw-server/src/endpoints/query.rs flushes pending writes once, at the start of the request (pending_overlapping / force_commit). It reads query_cache.generation() later, once per period. Suppose a write to a past period arrives mid-request. Its invalidate() has already run when a later period reads the generation, so that period's generation includes the write. But the period is evaluated on the reader, which cannot see the write until the writer's batch commits. put() then finds no newer overlapping write and stores the stale result, which stays until another write touches that range. This needs a multi-period request (Year / All time views) running while past events are edited or imported.

  • A failed commit leaves the worker's in-memory state out of sync with the database. — fix: #830
    When tx.commit() fails in work_loop, only the privacy engine is restored. The bucket cache in DatastoreInstance and the last_heartbeat cache keep the rolled-back changes:

    • A bucket whose creation was rolled back is still listed.
    • A bucket whose deletion was rolled back disappears.
    • Cached heartbeats can point at rows that were rolled back.

    The legacy-import path already rebuilds DatastoreInstance after a rollback. Doing the same here, and clearing last_heartbeat, would fix it. This only happens on real commit failures, such as a full disk.

Dominant language
Rust
Stars
322
Forks
105
Avg merge
3d 10h
Merged PRs (30d)
22

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ActivityWatch/aw-server-rust

All issues in ActivityWatch/aw-server-rust

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.