Skip to content

fix: race-loser detection in self-heal takeover paths - #107

Merged
joelteply merged 2 commits into
canaryfrom
fix/self-heal-takeover-race
Apr 25, 2026
Merged

joelteply merged 2 commits into
canaryfrom
fix/self-heal-takeover-race

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Two-tab race condition: concurrent self-heal-as-host attempts produce split-brain (two competing gists for the same room). Both takeover paths now jitter-sleep before delete, then re-scan for an existing fresh gist with this room name. If another tab won the race, join their gist as joiner instead of publishing a duplicate.

joelteply and others added 2 commits April 25, 2026 16:03
PR #105 added logged-out detection to airc (bash) + install.sh but
left airc.ps1 and install.ps1 with the original "daemon down" fallback
that misdiagnoses signed-out users. Mirror the bash logic:

- Advise-TailscaleIfDown: distinguish logged-out (status output contains
  'Logged out' / 'NeedsLogin') from daemon-down. Tell the user to click
  the tray icon to sign in or run 'tailscale up'.
- install.ps1: post-flight check tests for the logged-out marker and
  surfaces a warning + actionable instruction before user runs 'airc
  join' for the first time.

Same exit-code semantics as bash; same env-var override (AIRC_NO_TAILSCALE).
Windows users now get the same correct diagnosis macOS/Linux do.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Two tabs that hit the same dead-host gist simultaneously both used to
delete + publish, ending up with split-brain (two competing #room
gists, peers split between them). Caught by running two real airc
join tabs against a stale gist — not by the integration scenario,
which only spawns one joiner.

Both takeover paths (heartbeat-stale fast-path, SSH-failure self-heal)
now:
  1. Jitter sleep 100-1500ms before delete to de-synchronize racers.
  2. Attempt the delete (rc tells us nothing definitive about the race
     — gh API may report success on either side under contention).
  3. Re-scan gh for OTHER gists with this room name (excluding our
     stale ID).
  4. If found: race-loser branch — re-exec with the winner's gist ID
     as JOINER, not as host. Avoids publishing a duplicate.
  5. If not found: race-winner branch — re-exec as host
     (existing AIRC_NO_DISCOVERY=1 path).

Doesn't fix every theoretical race window — perfectly-aligned timing
+ gh API caching could still produce a duplicate publish — but covers
the reproducible split-brain that real two-tab usage was creating.

Caught by Joel's "i imagine you are fucking over windows users" /
"this has to do with not testing ever" feedback. Memory rule added:
test_real_user_surface (run the actual user command, two-tab
scenarios cannot be validated from one tab).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings April 25, 2026 21:18

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.

Pull request overview

This PR aims to prevent “split-brain” room hosting when two concurrent airc connect self-heal/takeover paths run at the same time, and also improves Windows-side Tailscale readiness messaging during install and join diagnostics.

Changes:

  • Add jittered backoff + “race-loser” detection to two takeover/self-heal paths so the loser joins the winner’s newly-created room gist instead of publishing a duplicate.
  • Improve Windows Tailscale diagnostics (Advise-TailscaleIfDown) to distinguish “logged out” from “daemon down”.
  • Add a proactive Tailscale “installed but logged out” check to install.ps1 and adjust post-install guidance.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
airc Adds jitter + re-scan logic in takeover/self-heal paths to reduce duplicate-room gist publishing.
airc.ps1 Enhances Tailscale down detection by checking tailscale status output for logged-out state and improving guidance.
install.ps1 Adds an installer-time Tailscale logged-out warning and updates “Next steps” messaging.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread install.ps1
Comment on lines +326 to +331
# -- Tailscale login check -----------------------------------------------
# Tailscale-installed-but-logged-out is the most common 'tailscale down'
# state in practice (post-reboot, fresh install, expired auth). Detect
# proactively and tell the user to sign in before they hit a confusing
# 'daemon down' error on their first 'airc join'. Mirrors install.sh
# ts_post_check.

Copilot AI Apr 25, 2026

Copy link

Choose a reason for hiding this comment

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

PR description is focused on the self-heal takeover race, but this PR also adds a new Tailscale login check + user guidance in the installer. Please either update the PR description to mention this additional behavior change (Windows install UX) or split it into a separate PR to keep review scope clear.

Copilot uses AI. Check for mistakes.
Comment thread airc
Comment on lines 1815 to 1819
if gh gist delete "$_resolved_gist_id" --yes 2>/dev/null; then
echo " ✓ Stale gist removed."
else
echo " ⚠ Couldn't remove stale gist (already gone? not on this gh account?)."
echo " New host will publish a fresh #${resolved_room_name} gist anyway."
echo " ⚠ Stale gist already gone — another tab may have taken over first."
fi

Copilot AI Apr 25, 2026

Copy link

Choose a reason for hiding this comment

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

When gh gist delete fails here, the new message assumes the gist is already gone due to another tab winning. In practice this can also fail for other reasons (e.g., gh auth/scope issues, transient network/API errors). Consider keeping the broader wording (already gone vs not owned vs other error) or branching on gh stderr to avoid misleading guidance.

Copilot uses AI. Check for mistakes.
Comment thread airc
Comment on lines +1822 to +1823
| awk -F'\t' -v re="airc room: ${resolved_room_name}\$" -v skip="$_resolved_gist_id" \
'$2 ~ re && $1 != skip { print $1; exit }')

Copilot AI Apr 25, 2026

Copy link

Choose a reason for hiding this comment

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

This awk filter treats the room name as a regex ($2 ~ re). If resolved_room_name contains regex metacharacters (e.g., ., [, *, ?), it can match the wrong gist and cause an incorrect takeover/join. Prefer an exact string comparison on the description field (e.g., compare $2 to "airc room: "), or properly regex-escape the room name before building re.

Suggested change
| awk -F'\t' -v re="airc room: ${resolved_room_name}\$" -v skip="$_resolved_gist_id" \
'$2 ~ re && $1 != skip { print $1; exit }')
| awk -F'\t' -v desc="airc room: ${resolved_room_name}" -v skip="$_resolved_gist_id" \
'$2 == desc && $1 != skip { print $1; exit }')

Copilot uses AI. Check for mistakes.
Comment thread airc
Comment on lines +1808 to +1814
# Same race-loser detection as the SSH-failure self-heal path
# below. Two tabs concurrently deciding "host is stale" both
# delete + publish, end up with split-brain — caught only by
# running two tabs together.
local _race_jitter_s; _race_jitter_s=$(awk -v r="$RANDOM" 'BEGIN{printf "%.3f", 0.1 + (r%1500)/1000}')
sleep "$_race_jitter_s"

Copilot AI Apr 25, 2026

Copy link

Choose a reason for hiding this comment

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

The new jitter + re-scan logic is intended to prevent split-brain when two tabs self-heal simultaneously, but there doesn’t appear to be automated coverage for the two-process race. Since the repo already has integration coverage for heartbeat self-heal, consider extending it to spawn two concurrent joiners against the same stale room and assert only one fresh room gist is created (the loser should join the winner).

Copilot uses AI. Check for mistakes.
Comment thread airc
Comment on lines 1998 to 2002
if gh gist delete "$_resolved_gist_id" --yes 2>/dev/null; then
echo " ✓ Stale gist removed."
else
echo " ⚠ Couldn't remove stale gist (already gone? not on this gh account?)."
echo " New host will publish a fresh #${resolved_room_name} gist anyway."
echo " ⚠ Stale gist already gone — another tab may have taken over first."
fi

Copilot AI Apr 25, 2026

Copy link

Choose a reason for hiding this comment

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

Same as above: if gh gist delete fails, the current message assumes it was already deleted by another tab. This can also fail when the gist isn’t on this account (cross-account room join), or due to auth/network issues. Suggest reverting to a message that includes those cases or capturing stderr to distinguish them.

Copilot uses AI. Check for mistakes.
Comment thread airc
Comment on lines +2008 to +2009
| awk -F'\t' -v re="airc room: ${resolved_room_name}\$" -v skip="$_resolved_gist_id" \
'$2 ~ re && $1 != skip { print $1; exit }')

Copilot AI Apr 25, 2026

Copy link

Choose a reason for hiding this comment

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

Same regex issue as the earlier takeover path: $2 ~ re uses resolved_room_name as a regex. Room names containing regex characters can cause false matches and lead to joining the wrong gist. Prefer exact description comparison or regex-escape the room name before building re.

Suggested change
| awk -F'\t' -v re="airc room: ${resolved_room_name}\$" -v skip="$_resolved_gist_id" \
'$2 ~ re && $1 != skip { print $1; exit }')
| awk -F'\t' -v desc="airc room: ${resolved_room_name}" -v skip="$_resolved_gist_id" \
'$2 == desc && $1 != skip { print $1; exit }')

Copilot uses AI. Check for mistakes.
Comment thread install.ps1
Comment on lines +332 to +337
$tsBin = $null
if (Get-Command tailscale -ErrorAction SilentlyContinue) {
$tsBin = 'tailscale'
} elseif (Test-Path 'C:\Program Files\Tailscale\tailscale.exe') {
$tsBin = 'C:\Program Files\Tailscale\tailscale.exe'
}

Copilot AI Apr 25, 2026

Copy link

Choose a reason for hiding this comment

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

The new Tailscale binary detection only checks PATH and C:\Program Files\Tailscale\tailscale.exe, but other parts of the repo also look in C:\Program Files (x86)\Tailscale\tailscale.exe. This can miss common 32-bit installs and skip the login warning. Consider checking both locations (or factoring to a shared Resolve-TailscaleBin helper) so behavior matches airc.ps1.

Suggested change
$tsBin = $null
if (Get-Command tailscale -ErrorAction SilentlyContinue) {
$tsBin = 'tailscale'
} elseif (Test-Path 'C:\Program Files\Tailscale\tailscale.exe') {
$tsBin = 'C:\Program Files\Tailscale\tailscale.exe'
}
function Resolve-TailscaleBin() {
if (Get-Command tailscale -ErrorAction SilentlyContinue) {
return 'tailscale'
}
$tailscaleCandidates = @(
'C:\Program Files\Tailscale\tailscale.exe',
'C:\Program Files (x86)\Tailscale\tailscale.exe'
)
foreach ($candidate in $tailscaleCandidates) {
if (Test-Path $candidate) {
return $candidate
}
}
return $null
}
$tsBin = Resolve-TailscaleBin

Copilot uses AI. Check for mistakes.
@joelteply
joelteply merged commit 1a6589e into canary Apr 25, 2026
5 checks passed
@joelteply
joelteply deleted the fix/self-heal-takeover-race branch April 25, 2026 21:29
joelteply added a commit that referenced this pull request Apr 26, 2026
fix: cold-host split-brain — post-publish race-loser detection

Two tabs running `airc join --room X` simultaneously both saw empty
gist list (gh propagation lag), both fell through to host mode, both
published — split-brain. Pre-publish recheck doesn't help because
neither tab's gist is globally visible at the moment of recheck.

Real fix: AFTER publishing, look for OTHER gists with the same room
name. Deterministic tiebreaker (lowest gist id alphabetically) picks
the winner. Loser stops its heartbeat, deletes its gist, clears
scope state, and re-execs as joiner targeting the winner's gist
(which goes through the existing multi-address pick → 127.0.0.1
for same-machine).

Verified: spawn two `airc join` processes simultaneously against a
shared room name → one wins host, the other yields cleanly + joins
via localhost. Exactly one gist on the gh account at the end.

Removed the previous pre-publish recheck I added in the prior commit
— it was redundant and didn't address the real race window. Comment
left at the discovery site pointing at the post-publish fix.

This is the same class of race PR #107 patched on the takeover paths
(stale gist takeover); now also patched on the cold-host path
(no gist exists yet → both create).

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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