Skip to content

imsg-read: wait for Messages to rebuild its window after opening a chat - #172

Merged
nixfred merged 1 commit into
nixfred:mainfrom
ianswope:fix/read-push-window-rebuild
Nov 25, 2026
Merged

nixfred merged 1 commit into
nixfred:mainfrom
ianswope:fix/read-push-window-rebuild

Conversation

@ianswope

Copy link
Copy Markdown
Contributor

What happened

Every --chat read push failed with exit 64:

imsg-read: Messages has no accessible window; log in and open Messages on the Mac

This lasted from 23 Nov 17:21 UTC until this fix, on a gateway (macOS 27.0.1) that was unlocked and logged in, with Messages open. ioreg reported no lock, and launchctl print gui/<uid> returned 0. Read-only imsg-read --status worked through the confined key.

Cause

open imessage:open?address=… makes Messages tear its window down and build a new one. Timing on the gateway, counting windows through System Events after the open:

before=1
t~0.5  0
t~1    0
t~1.5  1
t~3    1

select_chat sleeps 1.5 s and then looks once. That is right at the edge of the gap. With the Mac under load (load average around 20 at the time), the single look fell inside the gap on every attempt.

Fix

Keep the 1.5 s settle, then look again every 0.5 s, up to WINDOW_TRIES = 13 looks (about 7.5 s in all). A Mac with really no window still gets the same message after that, and a locked screen is still named through screen_locked().

Tests

  • test_window_rebuilt_after_open_is_selection_success: zero, zero, then one window. The selection succeeds after three looks.
  • test_window_wait_is_bounded: never a window. It gives up after exactly WINDOW_TRIES looks with the existing message.
  • python3 -m unittest test_read test_read_sync passes.

Verified live: with this imsg-read installed on the gateway, the queued push that had failed 13 times went through on its next retry (exit=0 marked (0 unread)), and pendingReads emptied.

Opening a conversation with `open imessage:` makes Messages tear its window
down and build a new one. System Events counts zero windows in between. On
the macOS 27.0.1 gateway the gap ran from about 0.5 s to 1.5 s after the open,
and longer under load. select_chat looked once at 1.5 s, so every --chat push
failed with "Messages has no accessible window; log in and open Messages on
the Mac" while the Mac was unlocked and Messages was open. Read pushes failed
for about 22 hours before this was found.

Keep the 1.5 s settle, then look again every 0.5 s, up to 13 looks (about
7.5 s). A Mac that really has no window still gets the same message, and a
locked screen is still named.
@ianswope
ianswope requested a review from nixfred as a code owner November 24, 2026 17:33
@nixfred
nixfred merged commit 01671c4 into nixfred:main Nov 25, 2026
1 check passed
nixfred added a commit that referenced this pull request Nov 25, 2026
WHAT: Weekly contributor notes for the three changes landed today, with what was
verified, and the open X-handle question for Nick (@k7cfo).

LR-T: blip, weekly-notes, contributors
LR-D: blip
LR-K: w48, k7cfo, ianswope

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BXN1v5rSKpVomPJvPhZCng
@nixfred

nixfred commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Merged in 01671c4. Thank you, Ian. This is your 25th merged pull request, and it is the kind that makes the gateway trustworthy.

You did the part that is usually missing: instead of raising the sleep and hoping, you counted the windows over time after open, showed the gap (zero windows from about 0.5 s to 1.5 s, longer under load) and showed why a single look at 1.5 s sat right on its edge. The fix follows directly from the measurement, it is bounded, a Mac with really no window still gets the same message, and a locked screen is still named as locked. And you verified it where it counts: a push that had failed 13 times went through.

One addition from me, folded into the merge (pushes to contributor forks are blocked on this side): the loop now stops if osascript itself times out (rc 124). Each look may block for TIMEOUT (150 s), so a hung System Events would have been asked 13 times, about 32 minutes with the Messages UI lock held. There is a test for it, which sees 13 calls instead of 1 without the two lines.

Checked: test_read.py (89) and test_read_sync.py pass, your two new tests fail on the previous helper, CI is green on main. It is installed on my gateway Mac, where I ran only the read-only --status; I have not forced a live push through the new wait. It is in the CHANGELOG under Unreleased with your name on it.


Generated by Claude Code

@ianswope
ianswope deleted the fix/read-push-window-rebuild branch November 25, 2026 22:36
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.

2 participants