Skip to content

[AI] Exit with an error when the sync server can't bind its port - #9339

Merged
tabedzki merged 4 commits into
actualbudget:masterfrom
tabedzki:tabedzki/fix/sync-server-listen-error
Nov 11, 2026
Merged

tabedzki merged 4 commits into
actualbudget:masterfrom
tabedzki:tabedzki/fix/sync-server-listen-error

Conversation

@tabedzki

@tabedzki tabedzki commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Description

When the sync server can't bind its port (EADDRINUSE, or EACCES on a port below 1024), it prints Listening on :::<port>... and exits with code 0. The failure looks like a clean shutdown, so Docker's restart: unless-stopped treats it as success, and the real error is never shown.

Root cause: Express 5's app.listen attaches its callback to server.once('error', ...) as well as to 'listening'.

  • The callback in run() ignored its error argument, so a failed bind ran the "started" path.
  • That handled the error, and the process then exited 0 because nothing was keeping it alive.
  • Even if run() had rejected, app.ts discarded that promise with void. The process-wide unhandledRejection handler only logs, so nothing would have stopped the process.
  • src/util/listen.ts:** resolves on 'listening', rejects on 'error', and removes its error listener once listening.
  • run(): creates the http/https server and awaits listen for both. "Listening on ..." is now printed only after a successful bind, and the Electron server-started message is sent at the same point.
  • app.ts: returns the run() promise, so the existing .catch logs Error starting app: with the error and exits 1.

Related issue(s)

N/A

Testing

  • I added src/util/listen.test.ts before the helper existed and confirmed it failed. It covers:
    • resolving on a free port;
    • rejecting with EADDRINUSE when the port is taken (the server is not left listening);
    • the error listener being removed after success.
  • yarn workspace @actual-app/sync-server run test passed (58 files, 700 tests), and the sync-server typecheck passed.
  • I held port 5199 open and started node packages/sync-server/build/app.js on it. It now prints the EADDRINUSE error and exits 1; before the fix it printed "Listening" and exited 0. On a free port it prints "Listening on :::5198..." and keeps running.

Checklist

  • Release notes added (see link above)
  • No obvious regressions in affected areas
  • Self-review has been performed - I have read every line of this diff and can explain what each change does and why it is needed

@netlify

netlify Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for actualbudget ready!

Name Link
🔨 Latest commit 854d8d1
🔍 Latest deploy log https://app.netlify.com/projects/actualbudget/deploys/6ae8aa22c42176491ca3832c
😎 Deploy Preview https://deploy-preview-9339.demo.actualbudget.org
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for actualbudget-website ready!

Name Link
🔨 Latest commit 854d8d1
🔍 Latest deploy log https://app.netlify.com/projects/actualbudget-website/deploys/6ae8aa22efd4239e342d0a3e
😎 Deploy Preview https://deploy-preview-9339.www.actualbudget.org
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Express 5's app.listen passes its callback to server.once('error'), so a
failed bind (EADDRINUSE, EACCES) ran the success callback, printed
"Listening on ...", and the process exited 0 once nothing kept it alive.

Listen through a small helper that resolves on 'listening' and rejects on
'error', and return the startup promise from app.ts so the existing catch
logs the error and exits 1.

Assisted-by: ClaudeCode:claude-opus-5-5
@tabedzki
tabedzki force-pushed the tabedzki/fix/sync-server-listen-error branch from febecbe to a33ddca Compare October 26, 2026 18:59
@tabedzki
tabedzki marked this pull request as ready for review October 27, 2026 22:05
@tabedzki
tabedzki requested a review from matt-fidd as a code owner October 27, 2026 22:05
@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between d27d531 and 854d8d1.

Files selected for processing: 3
  • packages/sync-server/src/app.ts
  • packages/sync-server/src/util/server-listen.test.ts
  • packages/sync-server/src/util/server-listen.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Walkthrough

The sync server now uses a promise-based listen helper. HTTP and HTTPS startup await listening before reporting the server address, and startup failures propagate to the existing error handler.

Changes: Sync server startup

Layer / File(s) Summary
Promise-based server listening
packages/sync-server/src/util/server-listen.ts, packages/sync-server/src/util/server-listen.test.ts
listen returns a promise that resolves with the bound address or rejects on a server error. Tests cover bound addresses, port conflicts, and error-listener cleanup.
Startup integration and error propagation
packages/sync-server/src/app.ts, packages/sync-server/app.ts, upcoming-release-notes/sync-server-listen-error.md
HTTP and HTTPS startup await listen before reporting the server address. The startup chain returns the app run promise, and the release note describes port-unavailable behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

Suggested reviewers: matissjanis, tim-smart

Merge Risk: Minimal · up to 854d8

Port-binding failures now cause a nonzero exit instead of reporting successful startup. No actionable merge-blocking risk was identified.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: the sync server exits with an error when it cannot bind its port.
Description check Passed The description directly explains the port-binding failure, root cause, implementation changes, tests, and expected exit behavior.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

coderabbitai[bot]
coderabbitai Bot previously approved these changes Oct 27, 2026
matt-fidd
matt-fidd previously approved these changes Oct 29, 2026

@matt-fidd matt-fidd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm!

Resolve conflict with actualbudget#9226, which added util/server-listen. Fold this
PR's error-rejecting promise into that helper so it keeps the IPv6
fallback and bound-address reporting, and drop the duplicate util/listen.

Assisted-by: ClaudeCode:claude-opus-5-5
@tabedzki
tabedzki added this pull request to the merge queue Nov 11, 2026
Merged via the queue into actualbudget:master with commit a353f6a Nov 11, 2026
34 checks passed
@tabedzki
tabedzki deleted the tabedzki/fix/sync-server-listen-error branch November 11, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants