Long-range queries: unbounded memory (8.6 GB), no cancellation after client timeout, heartbeats get 503 during teardown
メンテナーはふだん 5 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 12/100
- issue の種類
- バグ
- 明瞭さ
- 説明が足りない
- 活発さ
- 活発
- 領域
- backend, performance
調査の方向性
Start with q2_query_bucket and the timeperiods loop in ServerAPI.query2, where results are fully materialized and no client-disconnect check exists. The HeartbeatResource lock timeout and the QueryCache are the other entry points named. The report offers several non-exclusive options (logging, cancellation, work caps, streaming, subprocess isolation) with no maintainer decision, so there is no single clear definition of done.
索引モデルが issue の本文から書いたものです。
説明
Summary
A long-range query from the web UI (All time, then Year) left aw-server (Python, v0.14.0b9) holding 8.6 GB of Python objects. The client had already timed out, but the server kept computing and then spent ~45 minutes freeing the result. Because that teardown holds the GIL, every watcher heartbeat hit the 10 s HeartbeatResource lock timeout and got 503 Server busy for the duration, so tracking stopped.
The server recovered on its own eventually, but it should not get into this state in the first place.
What happened (erb-m2, macOS, 1.9 GB peewee-sqlite.v2.db, 42 buckets)
| time | event |
|---|---|
| 15:52–15:59 | ~500 query2 requests from the web UI (Activity view: All time, which timed out client-side after the configured 120 s, then Year, which errored). aw_transform.flood warnings were the only trace, since successful requests are not logged. |
| ~16:00 | process at 8.6 GB (top: MEM 8606M, CMPRS 8107M), state stuck. One thread spends ~90 % of its samples in frame_dealloc → list_dealloc → dict_dealloc (macOS sample), i.e. a request handler returning and freeing a huge list of event dicts. The machine was swapping (14/15 GB swap used), so each page had to be decompressed to be freed and got recompressed under pressure. 3–30 % CPU, no net progress for 40+ min. |
| 16:04 → 16:37 | Heartbeat lock could not be acquired within timeout, rejecting request + 503 for aw-watcher-window, aw-watcher-afk and aw-watcher-web heartbeats. Watchers queued/retried; the macOS window watcher ended up with 6 parallel in-flight heartbeats (ActivityWatch/aw-watcher-window#160). |
| ~16:45 | teardown finished, process back to 2.9 GB, heartbeats OK again. |
The in-memory QueryCache (#174) is bounded (128 MB of JSON) and is not the cause.
Why
- Query results are fully materialized.
q2_query_bucketcallsdatastore[bucket].get(starttime, endtime)which returns aList[Event]; every transform (flood,filter_period_intersect,categorize, …) builds new lists. A query over a long range with window + afk + browser buckets is millions ofEventdicts, ~1 KB each in Python. There is no bound on result size or on the range a single request may cover. - No cancellation. The web UI aborts the Axios request after
requestTimeout, but Flask/werkzeug keeps running the handler to completion. The user retrying with a smaller range (as I did) stacks a second heavy query on top of the first. - The GIL couples queries and heartbeats. A query thread that is allocating or freeing millions of objects (or page-faulting while doing so) starves the heartbeat thread, which only waits 10 s for its lock before returning 503. Heartbeats are the one thing the server must never drop; they should not share fate with analytics queries.
- Not diagnosable from the log. Only non-2xx responses are logged, so there is no record of which query/period produced the giant result or how long it took.
Proposals (any subset helps)
- Log slow/large query requests at INFO: name, timeperiod(s), duration, result size (events/bytes). Cheap and makes the next report actionable.
- Cancel on client disconnect. Check the request's socket/
environbetween query2 periods (and ideally inside long transforms) and abort when the client has gone away. At minimum, stop iterating thetimeperiodsloop inServerAPI.query2once the client is gone. - Bound work per request. A configurable cap on the time range of one
query_bucket(or on the number of events it may return, erroring with a 4xx that tells the client to split) would turn a 45-minute stall into an immediate, actionable error. aw-webui already splits most views into days; the server should enforce the contract rather than rely on it. - Stream from SQLite instead of materializing where the pipeline allows (peewee
.iterator()), and consider__slots__/lighterEventobjects. Even a 2–3× reduction in per-event memory would have kept this inside RAM. - Isolate heartbeats from queries. Options: run queries in a worker subprocess (multiprocessing pool, so the GIL and the memory are not shared with the request thread that serves heartbeats); or give heartbeats a longer lock timeout plus server-side queuing so a slow query delays rather than drops them.
- The Rust server presumably does not have the GIL problem, but the unbounded-materialization and no-cancellation points may apply there too; worth checking.
Related
- #49 (query performance over longer periods)
- ActivityWatch/aw-webui#1094 (Year / All time view behaviour)
- ActivityWatch/aw-watcher-window#160 (macOS watcher parallel heartbeats under server slowness)
- 主要言語
- Python
- スター
- 129
- フォーク
- 104
- 平均マージ
- 5日 14時間
- マージ済み PR(30日)
- 15
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
ActivityWatch/aw-server のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 51/100
ActivityWatch/aw-server#213 ·
メンテナーはふだん 5 日以内に返信
-
Return structured 400 when event storage rejects a foreign-bucket ID対応中かも @TimeToBuildBob が 26 日前に担当しました。 オープン
難易度 4/5 1〜2日 初心者へのやさしさ 35/100
ActivityWatch/aw-server#211 ·
メンテナーはふだん 5 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
ActivityWatch/aw-server#86 · コメント 1 件 ·
メンテナーはふだん 5 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
ActivityWatch/aw-server#82 ·
メンテナーはふだん 5 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 42/100
ActivityWatch/aw-server#77 ·
メンテナーはふだん 5 日以内に返信
ActivityWatch/aw-server の issue をすべて見る
似ている issue
-
bug docs
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
ActivityWatch/docs#223 ·
-
[Bug]: OpenCode v2 skips an assistant message that gains text after an earlier capture対応中かも @jianyx1 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
volcengine/OpenViking#8354 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
ARTPARK-SAHAI-ORG/calibrate-frontend#842 ·
メンテナーはふだん 1 日以内に返信
-
comp/tools P2 tool/skills type/bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
NousResearch/hermes-agent#159541 ·
メンテナーはふだん 1 日以内に返信