fix(chat): rollback session on pre-stream failure log commit error - #97
Conversation
When _log_pre_stream_failure fails to commit the request log row, the request-scoped session is left in a dirty state with the pending INSERT still attached. The next DB operation on that session then fails with InvalidRequestError — a secondary failure caused by our own error handling, not the original upstream error. Add a rollback in the except clause so the session is clean for any subsequent operations (streaming _finalize, blocking path commit).
There was a problem hiding this comment.
🐳 OrcaCode Review
✅ No findings — nothing to flag in this PR. Great work!
OrcaCode Review — Route Smarter. Ship Safer. Spend Less.
Engine-reported: 376 calls · 22.8M tokens · 98% cached
❤️ Share · Install OrcaCode Review
Free on GitHub — the review runs on your own OrcaRouter key. If it helped, a shout-out goes a long way.
Share: X · Reddit · LinkedIn
Follow: X · Discord · LinkedIn · OrcaRouter
|
Verified end-to-end against I confirmed the premise with a real SQLAlchemy session: drop
Merging. Two notes for follow-up, neither blocking: 1. The test doesn't guard the fix. db.add(log_row)
try:
await db.commit()
except Exception:
try:
await db.rollback()
except Exception:
pass
db.rollback.assert_awaited_once()I deleted the 2. The guard isn't reachable in today's flow. Both call sites ( That doesn't argue against merging — it's a correct guard, and it becomes load-bearing the moment any DB call is added before that |
Orca-Code-Review — push 1
✅ no blocking findings
When
_log_pre_stream_failurefails to commit the request log row (e.g. transient SQLite busy error), the request-scoped session is left dirty with the pending INSERT still attached. Any subsequent DB operation on that session then fails withInvalidRequestError— a secondary failure caused by our own error handling, not the original upstream error.The streaming
_finalizepath already handles this correctly (its_commit_rowdoesrollbackon failure), but the pre-stream path was missing the same guard.Fix: add
await db.rollback()in the except clause so the session is clean for any subsequent operations (streaming_finalize, blocking path commit).Files changed:
app/routes/chat.py— rollback in_log_pre_stream_failureexcept clausetests/unit/test_pre_stream_rollback.py— regression test