A commit on a lost advanced API session answers 200 for work that was rolled back

オープン
#2,519 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
postgresql, python, sqlalchemy
領域
api, backend, databases

調査の方向性

Start by reading api/sessions.py's load_session_from_context and api/actions.py's commit_raw_connection to understand the session fallback. Examine the oedialect client's engine.py lines 47-71 to see how SQLAlchemy pools reuse connection IDs. The fix must distinguish between a lost session with uncommitted work and an empty transaction after a rollback, likely by tracking recently closed IDs. Test against a real oedialect setup to ensure the fix doesn't break normal pool behavior.

索引モデルが issue の本文から書いたものです。

説明

API bug

Found while deciding slice 4 of #2492 (WF-20). Its fix is kept out of that work on purpose, because the obvious fix breaks oedialect.

The defect

load_session_from_context (api/sessions.py) looks up the connection_id a client sends. If no session is registered under it, it silently builds a new one under the same id and returns that:

with _LOCK:
    sess = _SESSION_CONTEXTS.get(connection_id)
if sess is None:
    return SessionContext(connection_id=connection_id, owner=user)

commit_raw_connection (api/actions.py) commits whatever that returns. So a POST /api/v0/advanced/connection/commit for an id whose session is gone commits a fresh, empty transaction and answers 200, while the work the client did on the old session has already been rolled back by Postgres.

A session goes away without the client being told in two ways:

  1. A server restart or reload. Sessions live only in the memory of the /api/v0/advanced process, so every deploy drops all of them, and each open transaction is rolled back.
  2. The idle sweep. A session idle for longer than TIME_OUT (30 s) with no open cursor is closed the next time any session is constructed.

A client that did its inserts before either of these and then commits is told its work was saved. That is silent data loss. Committed data is never affected; only work not yet committed.

The same fallback makes any cursor operation on a lost session fail visibly with 400 "Cursor not found", because the rebuilt session has no cursors. The silent case is the commit alone.

Why the obvious fix is wrong

"Refuse an unknown connection_id" would break every oedialect client that uses a SQLAlchemy pool:

  • oedialect 0.1.1 opens once and keeps its id (engine.py:47-71).
  • SQLAlchemy's client-side pool sends rollback on every checkin.
  • SessionContext.rollback closes a session that has no open cursor.
  • The client's next call arrives with the same id, and the fallback rebuilds the session.

That flow is the normal one. Production's live access log on 2026-09-23 showed about one connection/open for every twenty rollback/commit calls. Over 30 days there were 9,159 opens against 136,532 rollbacks and only 563 explicit closes.

What a fix has to get right

  • Everything except commit keeps working through the fallback.
  • A commit on an id whose session is gone must not answer 200. The candidate is a refusal saying the connection was lost and its uncommitted work rolled back.
  • The trap: SQLAlchemy can commit an empty transaction, for example after engine.begin() with no statement. After a preceding rollback has closed the session, that commit would also arrive on an unknown id, and a naive refusal would report a failure where nothing was lost. Distinguishing the two probably means remembering recently closed ids and whether they carried uncommitted work.
  • Prove it against real oedialect, as #2491/#2516 was proved against the real engine. A mock cannot show which calls SQLAlchemy's pool actually sends.

The fallback is per process, like everything else in _SESSION_CONTEXTS. The /api/v0/advanced group runs one process, so this is not a multi-process issue, and making it one is out of scope.

Until then

A deploy drops every open session. Deploy in an announced window, and check live traffic on advanced/connection in the access log before restarting.

🤖 Generated with Claude Code

主要言語
Python
スター
65
フォーク
28
平均マージ
4日 7時間
マージ済み PR(30日)
25

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

OpenEnergyPlatform/oeplatform のほかの issue

OpenEnergyPlatform/oeplatform の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。