Skip to content

Fix deadlock when UDP port is already in use#58

Merged
greynewell merged 2 commits intomainfrom
fix/udp-port-deadlock
Apr 7, 2026
Merged

Fix deadlock when UDP port is already in use#58
greynewell merged 2 commits intomainfrom
fix/udp-port-deadlock

Conversation

@greynewell
Copy link
Copy Markdown
Contributor

@greynewell greynewell commented Apr 7, 2026

Summary

  • listenUDP now signals startup success/failure via a buffered channel instead of logging and silently returning
  • If the port is already bound and FSWatch is disabled, Run() returns immediately with a clear error: UDP port 7734 already in use — is supermodel watch already running?
  • If FSWatch is active and UDP fails, it logs a warning and continues (fs-watch is the primary trigger)

Root cause

When the previous watch process left port 7734 bound, listenUDP returned early from its goroutine. The Run() select loop had ctx.Done() as its only exit, but in the test scenario the process was killed — triggering Go's deadlock detector.

Test plan

  • Kill a running supermodel watch, immediately rerun — should get a clear error instead of hanging
  • supermodel watch with no prior instance — starts normally
  • go test ./internal/files/... passes

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • UDP listener startup now blocks until the listener reports success or failure, providing clearer error handling when the port is already in use.
    • Improved graceful degradation on UDP bind failure: startup returns an explicit error when file-watching is disabled, and logs a warning and continues when enabled.

When listenUDP failed silently and FSWatch was disabled, the Run() select
loop had no way to receive events — causing a deadlock panic on kill or
hanging forever. Now listenUDP signals startup success/failure via a
buffered channel; if the port is already bound (watch already running),
we return a clear error immediately instead of hanging.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 7, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 162d26f4-3f74-48a0-a217-73206594823f

📥 Commits

Reviewing files that changed from the base of the PR and between 81bbfb8 and d6d4e36.

📒 Files selected for processing (1)
  • internal/files/daemon.go

Walkthrough

Run now synchronizes UDP listener startup using a buffered error channel. It waits for the listener to report bind success or an error; on bind failure it returns an error when FSWatch is disabled, or logs a warning and continues when FSWatch is enabled.

Changes

Cohort / File(s) Summary
UDP listener startup
internal/files/daemon.go
Reworked startup to use a buffered ready error channel. Run blocks until listenUDP sends nil (success) or an error. listenUDP now sends the bind error (or nil) instead of logging directly. Bind failure: return error when FSWatch is off; log warning and continue when FSWatch is on.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

📡 A tiny channel hums, "I'm here" or "No,"
The daemon waits—no race, no shadowed woe.
If files watch sleeps, a crash will show the cue;
If it watches on, a warning sees us through. 🎶

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main issue being fixed: a deadlock that occurred when the UDP port was already in use.
Description check ✅ Passed The description covers the key changes, root cause, and test plan, matching the template structure with detailed context about the fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/udp-port-deadlock

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@internal/files/daemon.go`:
- Around line 87-94: The current UDP bind error handling assumes every failure
is "address in use"; update the handler where d.listenUDP sends errors to
udpReady (the go routine started with go d.listenUDP(ctx, udpReady)) to inspect
the returned error before emitting the "already in use" message: use
errors.Is(err, syscall.EADDRINUSE) (or equivalent platform-aware check) and only
return the "port already in use — is `supermodel watch` already running?"
message when that specific error matches; for any other error, return or log a
generic "failed to start UDP listener" message including the real error and
include d.cfg.NotifyPort in context and preserve the existing FSWatch fallback
path that logs a warning when FSWatch is active.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d061b9cd-a3f6-4db4-b48d-9e15d46ba2b9

📥 Commits

Reviewing files that changed from the base of the PR and between 1ca605e and 81bbfb8.

📒 Files selected for processing (1)
  • internal/files/daemon.go

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@greynewell greynewell merged commit d888a81 into main Apr 7, 2026
7 checks passed
@greynewell greynewell deleted the fix/udp-port-deadlock branch April 7, 2026 21:24
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