Long-range queries: unbounded memory (8.6 GB), no cancellation after client timeout, heartbeats get 503 during teardown

オープン
#187 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 5 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
12/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
活発
技術スタック
flask, python, sqlite

調査の方向性

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

  1. Query results are fully materialized. q2_query_bucket calls datastore[bucket].get(starttime, endtime) which returns a List[Event]; every transform (flood, filter_period_intersect, categorize, …) builds new lists. A query over a long range with window + afk + browser buckets is millions of Event dicts, ~1 KB each in Python. There is no bound on result size or on the range a single request may cover.
  2. 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.
  3. 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.
  4. 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/environ between query2 periods (and ideally inside long transforms) and abort when the client has gone away. At minimum, stop iterating the timeperiods loop in ServerAPI.query2 once 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__/lighter Event objects. 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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ActivityWatch/aw-server のほかの issue

ActivityWatch/aw-server の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。