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

未关闭
#825 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@0xbrayo 已经在做这个了。

开始于 2026年10月20日。

  • #830 来自 @0xbrayo —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
rust
领域
backend, databases

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Rust
星标
322
派生
105
平均合并
3 天 10 小时
30 天内合并 PR
22

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ActivityWatch/aw-server-rust 的其他 Issue

查看 ActivityWatch/aw-server-rust 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。