ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
fix(errors): roll the crashed session back before recording the 500
Juan Denis3e4bd69ab8df
diff --git a/backend/app/__init__.py b/backend/app/__init__.pyindex 29254ee4..f9c23a92 100644--- a/backend/app/__init__.py+++ b/backend/app/__init__.py@@ -903,6 +903,19 @@ def create_app(config_name=None):'Unhandled exception on %s %s', request.method, request.path,exc_info=exc)+ # Roll the failing request's session back BEFORE recording. The 500+ # handler shares the app-context-scoped db.session with the view that+ # just crashed, so without this a crash caused by a DB error leaves the+ # session in a failed transaction and record_error's very first query+ # raises PendingRollbackError — swallowed by its own contract, which+ # means database-caused 500s never reach the error log at all. It also+ # stops record_error's commit() from flushing any ORM work the crashed+ # view had left pending. Flask-SQLAlchemy's teardown rolls this session+ # back moments later regardless, so a clean request is unaffected.+ try:+ db.session.rollback()+ except Exception: # noqa: BLE001 - must never change the 500 response+ pass# Record into the centralized error log. error_log_service never raises# by contract, but belt-and-braces: a recording failure must never# change this response.diff --git a/backend/tests/test_error_logs.py b/backend/tests/test_error_logs.pyindex 810f59dc..cb81db55 100644--- a/backend/tests/test_error_logs.py+++ b/backend/tests/test_error_logs.py@@ -193,3 +193,63 @@ def test_500_handler_records_error_and_keeps_response(client, app):assert 'RuntimeError' in entry.tracebackassert entry.endpoint == '/api/v1/__test_boom'assert entry.method == 'GET'+++def test_500_from_a_db_error_is_still_recorded(client, app):+ """A crash caused by the database must not be the one crash we cannot log.++ The 500 handler shares the app-context-scoped ``db.session`` with the view+ that just failed. When the failure IS a database error, that session is+ already in a failed transaction, so ``record_error``'s first query raises+ PendingRollbackError -- which its own never-raise contract then swallows,+ silently dropping the report. Rolling back before recording is what makes+ this class of 500 visible in the tracker at all.+ """+ @app.route('/api/v1/__test_db_boom')+ def _db_boom():+ # A REAL failed flush, not a hand-built IntegrityError: only an actual+ # constraint violation leaves the session in the failed-transaction+ # state that makes the next query raise. `message` is NOT NULL.+ db.session.add(ErrorLog(fingerprint='x' * 64, source='backend',+ level='error', message=None))+ db.session.flush() # raises IntegrityError, poisoning the session++ app.config['TESTING'] = False+ app.config['PROPAGATE_EXCEPTIONS'] = False++ res = client.get('/api/v1/__test_db_boom')+ assert res.status_code == 500++ entry = ErrorLog.query.filter_by(endpoint='/api/v1/__test_db_boom').first()+ assert entry is not None, 'a DB-caused 500 was not recorded'+ assert entry.exception_type == 'IntegrityError'+++def test_500_handler_does_not_commit_the_crashed_request_work(client, app):+ """record_error's commit() must not persist what the failing view left pending.++ Without the rollback, the handler's commit flushes any ORM state still+ sitting in the shared session -- so a view that add()s a row and then+ crashes ships a half-applied write.+ """+ from app.models.error_log import ErrorLog as _EL++ @app.route('/api/v1/__test_partial_write')+ def _partial():+ db.session.add(_EL(fingerprint='pending-should-not-persist',+ source='backend', level='error', message='pending'))+ db.session.flush()+ raise RuntimeError('crash after a pending write')++ app.config['TESTING'] = False+ app.config['PROPAGATE_EXCEPTIONS'] = False++ assert client.get('/api/v1/__test_partial_write').status_code == 500++ assert ErrorLog.query.filter_by(+ fingerprint='pending-should-not-persist').count() == 0+ # ...but the crash itself was still recorded.+ assert ErrorLog.query.filter_by(+ endpoint='/api/v1/__test_partial_write').count() == 1