Skip to content

fix(linux): don't stop the watcher when its terminal has gone away - #176

Open
officialasishkumar wants to merge 1 commit into
ActivityWatch:masterfrom
officialasishkumar:fix/xlib-print-eio
Open

officialasishkumar wants to merge 1 commit into
ActivityWatch:masterfrom
officialasishkumar:fix/xlib-print-eio

Conversation

@officialasishkumar

Copy link
Copy Markdown

aw-watcher-window stops for good on Linux once the terminal aw-qt was started from has been closed, as soon as it next polls a window with no WM_CLASS, or one that has just vanished. The tray keeps running, but window tracking ends.

get_window_class() had a leftover print("") on that fallback path. aw-qt starts modules with inherited stdio, so after the terminal closes, stdout is a hung-up tty and the write raises OSError: [Errno 5] Input/output error. heartbeat_loop treats OSError as fatal, so the watcher exits.

This matches the logs in ActivityWatch/activitywatch#1147. The BadWindow warning is logged, the traceback points at the print line (xlib.py:120 in that release), and the "Code made an unclear branch" warning right after it never appears.

Fix

Removed the print. The next line already logs this branch, and logging handlers don't raise when their stream is broken, so the fallback goes on to the parent window lookup. No other code on the Linux polling path writes to stdout.

This is separate from ActivityWatch/aw-qt#135, which repairs output that is already disconnected when aw-qt starts. It doesn't cover a terminal that closes later, which is the case here.

Testing

  • Added tests/test_xlib.py, which runs both fallback paths (parent lookup and vanished window) with a stdout that raises EIO. Both tests fail on master with OSError: [Errno 5] and pass with the fix.
  • Called the real get_window_class() with stdout on an actual hung-up pty (master end closed) and fake windows. On master it raises OSError(5, 'Input/output error'); with the fix it returns the parent's class.
  • pytest tests (192 passed, 14 skipped) and mypy aw_watcher_window/ --ignore-missing-imports pass on Linux with Python 3.11.

Not tested against a live X server: BadWindow depends on a race, so the tests use fake windows. I also couldn't confirm on the reporters' machines that their terminals had closed.

Fixes ActivityWatch/activitywatch#1147

get_window_class() had a leftover print("") on the fallback taken when a
window has no WM_CLASS, or vanished before it could be queried (BadWindow).
aw-qt starts the watcher with inherited stdio, so once the terminal aw-qt was
launched from is closed, stdout is a hung-up tty and that write raises
OSError: [Errno 5] Input/output error. heartbeat_loop treats OSError as fatal,
so the first such window permanently stops window tracking, which can be long
after startup.

Remove the print. The warning logged on the next line already records this
branch, and log handlers tolerate a broken stream, so the fallback now reaches
the parent window lookup and tracking continues.

Add tests that run both fallback paths with a stdout that raises EIO.

Fixes ActivityWatch/activitywatch#1147
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-07T06:31:23.903273Z
We couldn’t complete this review. Comment @codex review to try again.
7877908 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@greptile-apps

greptile-apps Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Removes the stray stdout write from get_window_class() so a disconnected terminal does not interrupt its fallback.

  • Linux window tracking continues after its launch terminal closes.

Reviews (1) · Last reviewed commit: "fix(linux): don't stop the watcher when ..." · Reviewed by Greptile

@TimeToBuildBob

Copy link
Copy Markdown
Contributor

I verified the failure path independently, because a one-line deletion fix is only worth taking if the deleted line is really what raises.

  • Checked out this head (7877908) and reverted only aw_watcher_window/xlib.py to master. Both new tests in tests/test_xlib.py then fail with OSError: [Errno 5] Input/output error raised from xlib.py:125 (print("")), on both the parent-lookup and vanished-window paths. With the fix both pass. So the tests do gate the actual regression rather than just exercising the fallback.
  • Confirmed the mechanism in aw_watcher_window/main.py: heartbeat_loop catches (FatalError, OSError) and breaks, while its own logger.exception and logger.error calls are already wrapped to swallow OSError. That asymmetry is exactly why a stray print is fatal and the existing logging is not, and it supports removing the write rather than reclassifying the exception.
  • Checked the other print calls in xlib.py: they are all inside the if __name__ == "__main__": debug block, not on the polling path, so the "no other stdout writes here" claim holds for this module.

What is needed to merge: maintainer approval to run the workflow on this fork head. Build and CodeQL did not run, so there is no CI evidence for the 192-test suite or mypy on the PR commit itself, even though I reproduced the targeted fix locally. I am Triage-only here and cannot approve fork workflow runs. No code change is requested from me.

Two notes that are not blockers:

  • The wider exposure is unchanged: any other stdout write reachable from the poll loop (including from a dependency) still terminates the watcher the same way. ActivityWatch/aw-qt#135 covers output that is already disconnected at startup; this PR covers a terminal that closes later. A general guard around the loop's stdio, or reclassifying OSError from the display connection separately from a broken stream, is a separate change and worth its own issue rather than expanding this one.
  • Thanks for stating the untested boundary (no live X server, no reporter confirmation). That is the honest scope, and the reporter thread in ActivityWatch/activitywatch#1147 is the place to confirm it once merged.

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.

Fatal error, stopping (aw_watcher_window.main:120)

2 participants