Skip to content

🐛 Fix Bonjour service registering with port 0 due to race with server startup#3772

Merged
guiyanakuang merged 3 commits intomainfrom
fix/bonjour-port-race-condition
Feb 8, 2026
Merged

🐛 Fix Bonjour service registering with port 0 due to race with server startup#3772
guiyanakuang merged 3 commits intomainfrom
fix/bonjour-port-race-condition

Conversation

@guiyanakuang
Copy link
Copy Markdown
Member

@guiyanakuang guiyanakuang commented Feb 8, 2026

Summary

  • Expose server port as StateFlow<Int> and make EndpointInfoFactory.createEndpointInfo() suspend to await a valid port (> 0), fixing a race condition where DesktopPasteBonjourService could register with port 0 before the server finished starting
  • Propagate suspend to SyncInfoFactory.createSyncInfo(), QRCodeGenerator.buildQRCode(), and generateQRCode()
  • Add early return in DesktopServiceListener.serviceResolved/serviceRemoved when textBytes is empty, avoiding unnecessary JsonDecodingException during partial jmDNS discovery

Fixes #3771

Test plan

  • Verify service registration uses correct port after server startup
  • Verify QR code generation still works (uses suspend createEndpointInfo)
  • Verify heartbeat sync test passes (SyncTest.testTrustAndHeartbeat)
  • Verify empty textBytes events are handled gracefully without exceptions

🤖 Generated with Claude Code

guiyanakuang and others added 3 commits February 8, 2026 12:06
… startup

DesktopPasteBonjourService's init block launches coroutines that
immediately collect networkInterfaces and call setup(), which can
execute before DesktopPasteServer.start() has set the actual port.
This causes the Bonjour service to advertise port 0.

Fix: Expose server port as StateFlow<Int> in PasteServer (backed by
MutableStateFlow). DesktopPasteBonjourService.setup() now awaits
portFlow.first { it > 0 } before proceeding with registration,
ensuring the correct port is always used regardless of init ordering.

Closes #3771

Co-Authored-By: Claude <noreply@anthropic.com>
Move portFlow.first { it > 0 } from DesktopPasteBonjourService into
EndpointInfoFactory.createEndpointInfo(), so every caller (Bonjour,
SyncRouting, SyncResolver, QRCodeGenerator) is protected against
reading port before server start.

- EndpointInfoFactory.createEndpointInfo → suspend, awaits portFlow
- SyncInfoFactory.createSyncInfo → suspend (propagated)
- QRCodeGenerator.buildQRCode/generateQRCode → suspend (propagated)
- Revert pasteServer injection from DesktopPasteBonjourService
- Update SyncTest to call createEndpointInfo inside runBlocking

Co-Authored-By: Claude <noreply@anthropic.com>
When jmDNS fires serviceResolved/serviceRemoved events with empty
textBytes, skip decoding instead of letting it fail with
JsonDecodingException. This avoids unnecessary exception overhead
for a normal jmDNS behavior during partial discovery.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@guiyanakuang guiyanakuang merged commit 920f234 into main Feb 8, 2026
2 checks passed
@guiyanakuang guiyanakuang deleted the fix/bonjour-port-race-condition branch February 8, 2026 04:44
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.

Bonjour service registers with port 0 due to race with server startup

1 participant