Skip to content

Better server error messaging#3102

Merged
ahopkins merged 10 commits into
mainfrom
server-errors
Dec 28, 2025
Merged

Better server error messaging#3102
ahopkins merged 10 commits into
mainfrom
server-errors

Conversation

@ahopkins

Copy link
Copy Markdown
Member

Requires #3101
Resolves #3100

This has long been something that was less than clear: errors when starting with an already in use port.

This PR introduces a new pattern for catching some common startup issues and providing better display.

Copilot AI review requested due to automatic review settings December 27, 2025 20:46
@ahopkins ahopkins requested review from a team as code owners December 27, 2025 20:46
@ahopkins ahopkins changed the title Server errors Better server error messaging Dec 27, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 improves server startup error handling by introducing a new error handling pattern that provides clearer, more user-friendly error messages for common startup issues like port conflicts. It addresses issues #3100 where errors were logged multiple times with confusing stack traces, and requires PR #3101 which upgrades tracerite.

Key changes:

  • New error handler module provides clean error reporting for OSError and ServerError during startup
  • Simplified socket configuration by removing nested error handling wrapper
  • Better management of worker state and EOFError handling with conditional cleanup logic

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
sanic/startup/errors.py New module introducing error handler pattern for startup failures with OSError and ServerError support
sanic/main.py Integrates tracerite loading and wraps CLI execution with startup error handler
sanic/server/socket.py Removes OSError wrapping logic, allowing errors to propagate to central handler
sanic/mixins/startup.py Adds EOFError handling for Manager creation and tracks worker state to prevent cleanup issues
tests/test_startup_errors.py New test suite covering all error handling scenarios
tests/test_exceptions_handler.py Updates tests to accommodate tracerite HTML rendering changes
setup.py Updates tracerite dependency to version 2.2.0 or higher
pyproject.toml Adds ruff ignore rules for import ordering in main.py

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

Comment thread sanic/startup/errors.py Outdated
Comment thread tests/test_startup_errors.py Outdated
Comment thread tests/test_startup_errors.py Outdated
Comment thread sanic/mixins/startup.py
Comment thread sanic/__main__.py

Copilot AI commented Dec 27, 2025

Copy link
Copy Markdown
Contributor

@ahopkins I've opened a new pull request, #3106, to work on those changes. Once the pull request is ready, I'll request review from you.

ahopkins and others added 2 commits December 27, 2025 23:18
…value (#3106)

* Initial plan

* Replace hardcoded errno 98 with errno.EADDRINUSE constant

Co-authored-by: ahopkins <166269+ahopkins@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ahopkins <166269+ahopkins@users.noreply.github.com>
Tronic
Tronic previously approved these changes Dec 27, 2025

@Tronic Tronic 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, works here - no more tracebacks shown. Thanks for the quick response!

@Tronic

Tronic commented Dec 27, 2025

Copy link
Copy Markdown
Member

Maybe that's for another PR and pending another tracerite release, but one might consider also adding import tracerite; tracerite.load() early in Sanic init, making it print cleaner tracebacks on console as well. The module can be styled by setting color constants tracerite.tty.* This captures logging.exception as well (unless disabled by kwarg on load), overriding the custom exception styling that Sanic in dev mode is doing there.

@ahopkins

Copy link
Copy Markdown
Member Author

I did put int in main.py, it would run only in CLI.

I went back and forth on this, but I think this is the right compromise. Happy to entertain other thoughts though.

@codecov

codecov Bot commented Dec 28, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.683%. Comparing base (f240432) to head (90369b7).
⚠️ Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
sanic/mixins/startup.py 42.105% 10 Missing and 1 partial ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##              main     #3102       +/-   ##
=============================================
+ Coverage   87.643%   87.683%   +0.039%     
=============================================
  Files          103       104        +1     
  Lines         7729      7778       +49     
  Branches      1225      1236       +11     
=============================================
+ Hits          6774      6820       +46     
- Misses         660       662        +2     
- Partials       295       296        +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ahopkins ahopkins merged commit c0848da into main Dec 28, 2025
30 checks passed
@ahopkins ahopkins deleted the server-errors branch December 28, 2025 09: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.

Erratic server error handling

4 participants