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

Abierto
#2,519 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
postgresql, python, sqlalchemy

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
Python
Estrellas
65
Forks
28
Merge medio
4 d 7 h
PR fusionados (30 d)
25

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de OpenEnergyPlatform/oeplatform

Todos los issues de OpenEnergyPlatform/oeplatform

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.