Skip to content

fix(main): don't exit when aw-server starts slowly at boot - #164

Merged
ErikBjare merged 2 commits into
ActivityWatch:masterfrom
TimeToBuildBob:fix/startup-no-blocking-server-wait
Oct 14, 2026
Merged

ErikBjare merged 2 commits into
ActivityWatch:masterfrom
TimeToBuildBob:fix/startup-no-blocking-server-wait

Conversation

@TimeToBuildBob

Copy link
Copy Markdown
Contributor

Same startup failure as ActivityWatch/activitywatch#1525, in the window watcher. main() calls client.wait_for_start() before recording; aw-client gives the server 10 seconds and then raises Server at http://127.0.0.1:5600 did not start in time, so a slow (Windows) boot kills the watcher. The afk watcher got the same fix in ActivityWatch/aw-watcher-afk#93.

The wait is redundant: the bucket is already registered with queued=True and every heartbeat is queued, so the client's persistent queue creates the bucket and replays heartbeats once the server answers. The macOS swift helper already retries delivery and creates its bucket lazily (#162).

Verified — tests/test_startup.py drives main() with the real aw-client and its SQLite queue against a server that is down until the test brings it up:

  • records while the server is down, then the first request after it comes up is the bucket creation, followed by every queued heartbeat in order;
  • shutting down while still offline exits promptly and keeps the backlog in the queue.

Both fail on master (blocked in wait_for_start) and pass with this change; stable over 5 reruns. Full suite 109 passed, mypy clean.

Refs ActivityWatch/activitywatch#1525

aw-client's wait_for_start() gives the server 10 seconds and raises
'did not start in time' on a slow boot, killing the watcher. The bucket
is already registered with queued=True and heartbeats are queued, so the
client's persistent queue creates the bucket and replays heartbeats once
the server answers. Same fix as ActivityWatch/aw-watcher-afk#93.

Refs ActivityWatch/activitywatch#1525

Git-Session-Id: da745699-4420-404b-a5de-c076703d380c
@greptile-apps

greptile-apps Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issue remains.

Summary

The PR removes client.wait_for_start() so the watcher can record while the server is still starting.

  • Window tracking starts before aw-server is ready.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    Start[Watcher starts] --> Bucket[Register queued bucket]
    Bucket --> Record[Record window samples]
    Record --> Queue[Queue heartbeats]
    Queue --> Retry[Retry while server is down]
    Retry --> Replay[Create bucket and replay when available]
Loading

Reviews (2) · Last reviewed commit: "test(startup): assert every sampled wind..." · Reviewed by Greptile

Comment thread tests/test_startup.py Outdated
Comment on lines +184 to +187
heartbeats = [payload["data"] for _, url, payload in requests_[1:]]
assert all("/heartbeat?pulsetime=" in url for _, url, _ in requests_[1:])
assert heartbeats[0] == {"app": "editor", "title": "one"}
assert {"app": "editor", "title": "two"} in heartbeats

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Replay checks miss samples

test_records_offline_then_creates_bucket_before_replaying checks the first heartbeat and whether title two appears anywhere, but never checks title three or its position. The expected count also comes from the queue itself, so a missing sample can shrink both sides of that check. A dropped third sample or reordered later samples could therefore pass.

Assert that all three sampled windows appear in their original order, allowing any extra heartbeats that aw-client deliberately adds.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9e1728c. The test now pins the exact replayed sequence ([one, two], in order) and fixes the expected count at 2 instead of deriving it from the queue. It also asserts that three is in aw-client's pre-merge cache (client.last_heartbeat[bucket_id]). A queued heartbeat is enqueued only when the next, different sample arrives, so the newest sample is never in the queue at shutdown. That is client behaviour, not a dropped sample. Each of the three samples is now accounted for, so a dropped or reordered sample fails the test.

@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

CI-green and mergeable — waiting only on a maintainer click.

This PR is ready to merge, but the bot has pull-only access to this repo and can't self-merge — surfacing it here so it isn't lost. The monitoring loop will stop re-flagging it now that this note is posted.

@TimeToBuildBob

TimeToBuildBob commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 AI code review

Removes the client.wait_for_start() call from main() so the watcher starts recording immediately, relying on the persistent queue to create the bucket and replay heartbeats once the server is available. Adds tests/test_startup.py with an OfflineServer that patches requests to simulate a down server, verifying offline recording, bucket creation before replay, and offline shutdown preserving queued heartbeats.

Safe to merge — no P0/P1 findings

Confidence 5/5

✅ No thread-worthy findings. Advisory notes follow; they are retained without opening review threads.

1 advisory finding (summary-only, not scored)

These P2 guard, heuristic, trade-off, or documentation claims are retained for judgment without opening review threads.

⚠️ P2 medium — tests/test_startup.py:44

The test's OfflineServer.request returns a requests.Response with status 200 and empty JSON body for all requests when the server is available. This does not simulate the real server's behavior for bucket creation: the real server would return 404 if the bucket does not exist, prompting the client to create it. Since the test always returns 200, the client may not actually create the bucket, and the test's assertion that the first request is a POST to /buckets/... with payload["type"] == "currentwindow" might be checking a request that the client sends regardless of the response. However, the test's purpose is to verify that the watcher records offline and replays, not to test the server's API. The test is not hermetic because it depends on the internal behavior of aw_client's queue, which is not part of this PR. But the test is intended to be an integration test with the real client, so it's a trade-off. The main issue is the expected_heartbeats bug, which I already reported. I'll not report this as a separate finding because it's not a defect in the test's logic, but a limitation. However, the test's OfflineServer does not handle the case where the server is available but the bucket does not exist; it always returns 200, so the client might not create the bucket at all. The test's assertion that the first request is bucket creation might fail if the queue sends heartbeats first. But the queue likely sends items in order, so bucket creation first. However, the test's expected_heartbeats bug is a clear defect. I'll report that as a test-defect. The test's OfflineServer also does not simulate the server's response to bucket creation; it returns 200, but the real server would return 404 if the bucket doesn't exist, causing the client to create it. The test's assertion that the first request is bucket creation is based on the assumption that the queue sends the bucket creation first, which is likely true. But the expected_heartbeats bug is the main issue. I'll report that.

Add an assertion that the queue's persistqueue path is under tmp_path.

How this was verified: The OfflineServer always returns 200 for all requests, including bucket creation. The real server would return 404 for a missing bucket, but the test does not simulate that. This could cause the client to not create the bucket, but the test's assertions might still pass because the client sends the bucket creation request regardless. The test is not hermetic but it's a trade-off.

Consensus: 2/3 passes agreed — pass 1 looked and disagreed
Distinct keys: 1 (general)

Files changed (2) — the diff as I read it
  • aw_watcher_window/main.py — Removes the client.wait_for_start() call and adds a comment explaining that the queue creates the bucket and retries delivery.
  • tests/test_startup.py — Adds integration tests with a fake offline server to verify startup behavior when the server is down.

Reviewed 24ba1edce524 · openrouter/deepseek/deepseek-v4-flash-0731 · llm engine · 49s · about this reviewer

Maintainer commands

@TimeToBuildBob review (own line) — fresh review · @TimeToBuildBob fix — a worker acts on the findings. Once per comment; 👀 = received.

The replay test only checked the first heartbeat and that 'two' appeared
somewhere, and derived the expected count from the queue itself, so a
dropped or reordered sample could pass. Pin the exact replayed sequence
and assert the newest sample sits in aw-client's pre-merge cache.

Git-Session-Id: 9c782e13-0b54-5cb3-80d6-f34dbcb77d0d
@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

@greptileai review

@ErikBjare
ErikBjare merged commit e275219 into ActivityWatch:master Oct 14, 2026
7 checks passed
ErikBjare pushed a commit to ActivityWatch/activitywatch that referenced this pull request Oct 18, 2026
aw-watcher-afk 5aaab742 -> 2762612e (ActivityWatch/aw-watcher-afk#93:
keep recording while the server starts)
aw-watcher-window c8814e49 -> e2752196 (ActivityWatch/aw-watcher-window#164:
don't exit when aw-server starts slowly at boot)

Both fix commits are ancestors of the pinned SHAs. Minimal bump to the
merge commits only; no unrelated watcher changes are pulled in.

Git-Session-Id: 286d8831-484c-4f42-8c75-c884eaee25ea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants