Repository navigation
fix(macos): start the Swift helper after all globals are initialized - #172
Conversation
Since ActivityWatch#162, `heartbeatSender` is a top-level `let` declared after the top-level `start()` and `RunLoop.main.run()` calls. Top-level globals in the helper are initialized only when execution reaches them, and the run loop never returns, so the first heartbeat used an uninitialized HeartbeatSender and the helper crashed with SIGSEGV right after launch. Nothing was recorded with the default macOS strategy. Move startup to the end of the file so every global is initialized first.
[Medium impact] This PR appears safe to merge. SummaryMoves
Reviews (1) · Last reviewed commit: "fix(macos): start the Swift helper after..." · Reviewed by Greptile |
TimeToBuildBob
left a comment
There was a problem hiding this comment.
The diagnosis matches current master: top-level start() / RunLoop.main.run() at lines 211–212 never return, so let heartbeatSender at 615 is never initialized and the first heartbeat SIGSEGVs in enqueue. Moving startup to the end of the file is the right fix — more future-proof than relocating this one global. The comment is load-bearing.
I can't run the helper from this Linux host. Greptile is 5/5 on b2f6a99. Build and CodeQL are held at action_required because this is a first-time-contributor fork PR; those runs need a maintainer "Approve and run".
#146 also fixes this as a side effect via @main. If that lands first, this can close. If this lands first, #146 needs the four end-of-file lines removed on rebase (as the PR already notes).
|
Just ran into this on the current nightly (0.14.2b1.dev-0d114d7, macOS 27.0.1, arm64). Same segfault and same stack as in your description. It crashed right after I granted Accessibility, then again on every restart, and aw-tauri kept popping up its "A module stopped" window. No window events were recorded at all. As a workaround I copied the aw-watcher-window module from the v0.14.1 release into a folder listed in discovery_paths. No crashes since (6 launches so far) and events are coming in again. I haven't built this branch, so I can't say I've tested the fix itself. I did throw together a tiny Swift file with the same ordering though: it segfaults, and it runs fine once the global is initialized before start(). So your diagnosis matches perfectly. The activitywatch master pin already includes #162, so unless this gets merged first, I think the next release will have the same problem on macOS. |
|
Thanks Lukas, for testing it on the nightly and for the 0.14.1 workaround. That'll help anyone who hits this before it's merged. |
|
@TimeToBuildBob Address the failing CI |
|
@TimeToBuildBob Actually, I'm merging this. Fix it in a fresh PR. |
|
Thanks for the contribution @andreivince! |
|
Confirmed: the failing checks are Linux/Python 3.9 tests and typecheck; the macOS build passed. I’ll investigate those failures and put the repair in a fresh PR against master, leaving this merged Swift fix untouched. |
|
Opened the fresh repair PR: #174 (863680b). Both failures happen before project code runs: ubuntu-latest now uses Ubuntu 26.04, and setup-python has no Python 3.9 build for it. The repair pins only the Linux tests and typecheck jobs to Ubuntu 24.04; Python 3.9 and macOS/Windows coverage stay unchanged. Verified locally with Python 3.9.25 and the locked dependencies: 196 passed, 6 skipped; mypy and CLI startup passed. Hosted CI is running; the merged Swift fix is untouched. |
The pinned aw-watcher-window (10638b2, #168) is one commit past the regression introduced by ActivityWatch/aw-watcher-window#162: the Swift helper declares `heartbeatSender` after the top-level `start()` / `RunLoop.main.run()`, so the first heartbeat hits an uninitialized sender and the helper exits 139 (SIGSEGV in HeartbeatSender.enqueue). Both candidate repairs merged on 2026-11-04: - ActivityWatch/aw-watcher-window#172 (86c5203) - move startup to the end of macos.swift - ActivityWatch/aw-watcher-window#146 (12000f5) - keep foreground tracking when the browser context fails; fixes the same ordering 12000f5 is aw-watcher-window master, contains 86c5203 (compare: ahead 2, behind 0), and is a fast-forward of the current pin. v0.14.1 pins c8814e4978 (pre-#162) and is unaffected. A v0.14.2 tag at the current pin ships a macOS window watcher that crashes on start, so this needs to land before the tag. Git-Session-Id: 91c15f3b-345b-4a65-8679-c98b93dddbac
Since #162 the Swift helper crashes right after it starts, so the default macOS strategy records nothing.
heartbeatSenderis a top-levelletat line 615 ofmacos.swift, butstart()andRunLoop.main.run()run at line 211. Top-level globals in this file are only initialized when execution reaches them, and the run loop never returns, so the first heartbeat goes to an uninitializedHeartbeatSender:The Python side then logs
Swift helper exited with status 139. v0.14.1 isn't affected, since its pin is older than #162, but the current activitywatch master pin is.This moves
start()and the run loop to the end of the file, with a comment, so every global is initialized first, including ones added later.Checked on macOS 26.6.2 with Swift 6.3.1, built like the Makefile, against a testing-mode server on a free port. The helper built from master dies with SIGSEGV within a second and records nothing. With this change it keeps running and records events, both launched directly and through
aw-watcher-window --strategy swift. There's no automated test: CI builds the helper but doesn't run it, and the helper only sends heartbeats with Accessibility permission and a focused window.#146 also fixes this as a side effect, because its
@mainmakes the globals lazy. If it lands first, this can be closed. If this lands first, #146 needs the four lines at the end of the file removed when rebasing, or the build fails. #159 moves startup to the end of the file the same way.