Skip to content

fix(macos): start the Swift helper after all globals are initialized - #172

Merged
ErikBjare merged 1 commit into
ActivityWatch:masterfrom
andreivince:fix/macos-helper-init-order
Nov 4, 2026
Merged

ErikBjare merged 1 commit into
ActivityWatch:masterfrom
andreivince:fix/macos-helper-init-order

Conversation

@andreivince

Copy link
Copy Markdown
Contributor

Since #162 the Swift helper crashes right after it starts, so the default macOS strategy records nothing. heartbeatSender is a top-level let at line 615 of macos.swift, but start() and RunLoop.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 uninitialized HeartbeatSender:

EXC_BAD_ACCESS (SIGSEGV), KERN_INVALID_ADDRESS at 0x0000000000000010
HeartbeatSender.enqueue(_:) < sendHeartbeat(_:) < MainThing.windowTitleChanged < MainThing.focusedWindowChanged < MainThing.focusedAppChanged < start()

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 @main makes 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.

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.
Copilot AI balanced review requested due to automatic review settings October 25, 2026 15:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@greptile-apps

greptile-apps Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge.

Summary

Moves start() and RunLoop.main.run() to the end of macos.swift so globals are initialized before window capture begins.

  • The macOS helper starts after its shared values are ready.

Reviews (1) · Last reviewed commit: "fix(macos): start the Swift helper after..." · Reviewed by Greptile

@TimeToBuildBob TimeToBuildBob left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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).

@quickstar

Copy link
Copy Markdown

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.

@andreivince

Copy link
Copy Markdown
Contributor Author

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.

@ErikBjare

Copy link
Copy Markdown
Member

@TimeToBuildBob Address the failing CI

@ErikBjare

Copy link
Copy Markdown
Member

@TimeToBuildBob Actually, I'm merging this. Fix it in a fresh PR.

@ErikBjare
ErikBjare merged commit 86c5203 into ActivityWatch:master Nov 4, 2026
5 of 7 checks passed
@ErikBjare

Copy link
Copy Markdown
Member

Thanks for the contribution @andreivince!

@TimeToBuildBob

Copy link
Copy Markdown
Contributor

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.

@TimeToBuildBob

Copy link
Copy Markdown
Contributor

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.

ErikBjare pushed a commit to ActivityWatch/activitywatch that referenced this pull request Nov 6, 2026
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
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.

5 participants