-
-
Notifications
You must be signed in to change notification settings - Fork 57
fix(modules): auto-restart crashed watchers and fix toggle for dead processes #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ErikBjare
merged 2 commits into
ActivityWatch:master
from
TimeToBuildBob:bob/fix-module-restart
Mar 2, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """Unit tests for the Module manager.""" | ||
|
|
||
| import subprocess | ||
| from pathlib import Path | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from aw_qt.manager import Module | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def module(): | ||
| """Create a test module with a mock path.""" | ||
| return Module("aw-test-module", Path("/usr/bin/true"), "system") | ||
|
|
||
|
|
||
| class TestModuleToggle: | ||
| """Tests for Module.toggle() behavior, especially with crashed processes.""" | ||
|
|
||
| def test_toggle_starts_stopped_module(self, module): | ||
| """Toggle should start a module that was never started.""" | ||
| assert not module.started | ||
| assert not module.is_alive() | ||
|
|
||
| with patch.object(module, "start") as mock_start: | ||
| module.toggle(testing=True) | ||
| mock_start.assert_called_once_with(True) | ||
|
|
||
| def test_toggle_stops_running_module(self, module): | ||
| """Toggle should stop a module that is running.""" | ||
| # Simulate a running process | ||
| mock_proc = MagicMock(spec=subprocess.Popen) | ||
| mock_proc.returncode = None # None means still running | ||
|
|
||
| # After terminate+wait, process should report as dead | ||
| def fake_wait(): | ||
| mock_proc.returncode = -15 # SIGTERM | ||
|
|
||
| mock_proc.wait.side_effect = fake_wait | ||
| module._process = mock_proc | ||
| module.started = True | ||
|
|
||
| module.toggle(testing=True) | ||
| assert not module.started | ||
| assert module._process is None | ||
|
|
||
| def test_toggle_restarts_crashed_module(self, module): | ||
| """Toggle should restart a module that crashed (started=True, process dead). | ||
|
|
||
| This is the key fix: previously, toggle checked `self.started` instead of | ||
| `self.is_alive()`, so the first click would only call stop() (cleaning up | ||
| state) without starting, requiring a second click to actually start. | ||
| """ | ||
| # Simulate a crashed process: started=True but process exited | ||
| mock_proc = MagicMock(spec=subprocess.Popen) | ||
| mock_proc.returncode = 1 # Non-zero = exited | ||
| module._process = mock_proc | ||
| module.started = True | ||
|
|
||
| assert not module.is_alive() # Process is dead | ||
| assert module.started # But flag says started | ||
|
|
||
| with patch.object(module, "start") as mock_start: | ||
| module.toggle(testing=True) | ||
| # Should have cleaned up and started in one toggle | ||
| mock_start.assert_called_once_with(True) | ||
| assert not module.started # stop() was called to clean up | ||
|
|
||
|
|
||
| class TestModuleIsAlive: | ||
| """Tests for Module.is_alive() behavior.""" | ||
|
|
||
| def test_is_alive_no_process(self, module): | ||
| assert not module.is_alive() | ||
|
|
||
| def test_is_alive_running(self, module): | ||
| mock_proc = MagicMock(spec=subprocess.Popen) | ||
| mock_proc.returncode = None | ||
| module._process = mock_proc | ||
| assert module.is_alive() | ||
|
|
||
| def test_is_alive_exited(self, module): | ||
| mock_proc = MagicMock(spec=subprocess.Popen) | ||
| mock_proc.returncode = 0 | ||
| module._process = mock_proc | ||
| assert not module.is_alive() | ||
|
|
||
|
|
||
| class TestGetUnexpectedStops: | ||
| """Tests for Manager.get_unexpected_stops().""" | ||
|
|
||
| def test_detects_crashed_module(self): | ||
| from aw_qt.manager import Manager | ||
|
|
||
| with patch.object(Manager, "discover_modules"): | ||
| mgr = Manager(testing=True) | ||
|
|
||
| mod = Module("aw-test", Path("/usr/bin/true"), "system") | ||
| mock_proc = MagicMock(spec=subprocess.Popen) | ||
| mock_proc.returncode = 1 | ||
| mod._process = mock_proc | ||
| mod.started = True | ||
| mgr.modules = [mod] | ||
|
|
||
| unexpected = mgr.get_unexpected_stops() | ||
| assert len(unexpected) == 1 | ||
| assert unexpected[0].name == "aw-test" | ||
|
|
||
| def test_ignores_intentionally_stopped(self): | ||
| from aw_qt.manager import Manager | ||
|
|
||
| with patch.object(Manager, "discover_modules"): | ||
| mgr = Manager(testing=True) | ||
|
|
||
| mod = Module("aw-test", Path("/usr/bin/true"), "system") | ||
| mod.started = False # Intentionally stopped | ||
| mgr.modules = [mod] | ||
|
|
||
| unexpected = mgr.get_unexpected_stops() | ||
| assert len(unexpected) == 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if crashes happen infrequently but consistently? Like for the aw-watcher-macos crashes we've been rarely having (not more than weekly), it could in theory have crashed 3+ times across the uptime of a multi-week session (if no proper reboots or system crashes)
Might be better to use some type of time-based backoff here? Or a "max-tries in a short period" triggering no more tries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point — fixed in bfeb138.
Changed from an absolute counter to a sliding 10-minute window:
RESTART_WINDOW_SECONDS = 600)Old timestamps are pruned when new restarts are recorded, so no memory leak. Manual restart/toggle still resets the history.