Skip to content

fix(tray): report unavailable server ports instead of restart loops - #164

Open
scottsgevor wants to merge 3 commits into
ActivityWatch:masterfrom
scottsgevor:feat/port-bind-diagnostics
Open

scottsgevor wants to merge 3 commits into
ActivityWatch:masterfrom
scottsgevor:feat/port-bind-diagnostics

Conversation

@scottsgevor

@scottsgevor scottsgevor commented Dec 2, 2026 •

Copy link
Copy Markdown

Problem

If aw-server (or aw-server-rust) cannot bind its port, the process exits immediately and the tray only reports "Module aw-server crashed and was auto-restarted" three times before giving up. The actual reason is invisible: the server writes its bind error to stderr, which aw-qt intentionally does not capture (ActivityWatch/aw-server#27).

This is most visible on Windows when Hyper-V/WSL reserves the port (WinError 10013, see ActivityWatch/activitywatch#571), but it also happens when another process holds the port.

Change

  • New aw_qt/serverport.py:
    • bind_error() first checks for an existing listener with connect_ex: on Windows SO_REUSEADDR can let a bind-over succeed, so a bind test alone can miss an occupied port. It then binds with SO_REUSEADDR and calls listen(), matching the servers' own setup (werkzeug sets allow_reuse_address) and avoiding false positives from TIME_WAIT sockets.
    • The probe uses the server's configured host (read from aw-server.toml / aw-server-rust config.toml) and the matching address family, so IPv6 binds (::1) and IPv4-only conflicts are handled correctly.
    • describe_bind_error() explains the cause: Windows 10013 gets the Hyper-V/WSL explanation plus the netsh commands; EADDRINUSE reports another program using the port.
  • TrayIcon checks server modules before auto-restarting. If the port is unavailable it shows a dedicated dialog ({module} can't start: {host}:{port} is unavailable, the cause, and the full module log under Details, with a Restart button), stops the module and skips the restart loop.
  • Module.get_server_address() exposes the configured (host, port); the host is read alongside the existing port lookup.

Test

  • New tests/test_serverport.py: free port, a real open listener, an IPv6 listener, an IPv4 listener that must not block the IPv6 probe, bind failure, and both message variants.
  • New cases in tests/test_tray_status.py: an unavailable port shows the dialog and does not restart; an available port keeps the existing auto-restart path.
  • Host/address resolution tests in tests/test_manager.py.
  • Unit suite on Windows: 377 passed, 16 skipped. 3 pre-existing Linux-only autostart tests fail on Windows (unrelated).
  • flake8 reports no new warnings.

Companion PR: ActivityWatch/aw-server#243
Refs ActivityWatch/activitywatch#571

When a server module exits and its port cannot be bound, explain the
actual cause (in use / blocked by Windows) in a dialog and stop the
pointless auto-restart loop. The probe mirrors the server's
SO_REUSEADDR bind to avoid false positives from TIME_WAIT sockets.
@greptile-apps

greptile-apps Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge; no blocking issues remain.

Summary

The PR reports unavailable server ports instead of repeatedly restarting the server.

  • The tray explains unavailable server ports instead of restarting.

Reviews (3) · Last reviewed commit: "fix(tray): keep port probe errors from e..." · Reviewed by Greptile

Comment thread aw_qt/serverport.py Resolved
Comment thread aw_qt/trayicon.py Resolved
- Check for an existing listener with connect_ex before binding: on
  Windows SO_REUSEADDR can make a bind-over succeed, which made the
  probe miss a port that is already in use.
- Add listen() to the bind probe so it tests whether the server can
  actually listen, not just bind.
- Read the configured host from aw-server.toml / aw-server-rust
  config.toml and probe with the matching address family, so an
  unrelated IPv4 listener no longer blocks IPv6 recovery (and IPv6
  conflicts are detected).
- Update tests with real listeners, IPv6 cases, and host/address
  resolution.
@scottsgevor

Copy link
Copy Markdown
Author

Addressed both Greptile findings in 9005482:

  1. Windows restart loops: bind_error() now checks for an existing listener with connect_ex before binding, and the bind probe calls listen(). Added a test that keeps a real listener open (passes on Windows too).
  2. IPv4 vs IPv6: aw-qt now reads the configured host from aw-server.toml / aw-server-rust config.toml and probes with the matching address family. Added an IPv6 listener test and a test where an IPv4 listener must not block the IPv6 probe.

Local on Windows: 377 passed, 16 skipped (3 pre-existing Linux-only autostart failures, unrelated).

@scottsgevor

Copy link
Copy Markdown
Author

@greptileai review

Comment thread aw_qt/serverport.py Resolved
socket() can fail before the probe's try block (e.g. an unavailable
IPv6 stack on a ::1 config). Catch creation errors in bind_error and
return them like bind failures, so the tray shows the dialog and keeps
its status timer running instead of raising out of the Qt callback.
@scottsgevor

Copy link
Copy Markdown
Author

Fixed in 0bc70e7: bind_error() now catches socket() creation failures and returns them like bind failures, so an unavailable IPv6 stack no longer raises out of the Qt status callback. Added a test that simulates EAFNOSUPPORT.

@greptileai review

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.

1 participant