Skip to content

fix: never let a failed rollback skip the write callbacks' error handling - #57

Merged
tabedzki merged 4 commits into
mainfrom
t3/handle-rollback-failures
Dec 1, 2026
Merged

tabedzki merged 4 commits into
mainfrom
t3/handle-rollback-failures

Conversation

@tabedzki

@tabedzki tabedzki commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

🤖 AI text below 🤖

Fixes #55

Follows #51 (merged).

Problem

After #51, each of the five DB write callbacks (registerSurgery, createRecording, CreateNewJob, RerunJob, RegisterPreParamList) starts its catch block with conn.cancelTransaction. That runs a ROLLBACK query, which throws when the connection dropped and cannot reconnect. Then the rest of the catch never runs:

  • no error dialog and no Slack report
  • the button stays disabled and the GUI stays "Busy"
  • in registerSurgery, the surgery dialog is never closed, so createRecording stays blocked in addSurgeryData's uiwait

In createRecording this happens after the files were already moved to cup, so a failed registration would go unreported.

Fix

  • New repo-root helper cancelOpenTransaction(conn, err). It cancels an open transaction inside its own try/catch and never throws.
    • It returns the caught error unchanged when the rollback works or there is no open transaction.
    • When the rollback fails too, it returns an MException with the same identifier, the message <original message> (rollback also failed: <rollback message>), and both errors as causes, so the original stack is kept. The dialog and the Slack report therefore still show the original error first and mention the rollback failure.
  • Each callback now calls err = cancelOpenTransaction(conn, err); in place of the inline cancel. Four keep their exist('conn', 'var') guard; createRecording always has conn by then. The rest of each catch block is unchanged. (A failed or skipped surgery registration still moves and registers the recording #56 also edits the createRecording and registerSurgery catch blocks; this PR only touches the rollback lines there.)
  • Every error dialog that appends the error message now puts : before it. createRecording used to show "Recording was not createdLost connection ...", and RegisterPreParamList and registerSurgery ran their text into the error the same way; the others used a bare space.
  • Vendored toolboxes are untouched.

Screenshots

The error dialog of createRecording when the insert fails and the rollback fails too. Before, the failing rollback skipped this dialog entirely.

Recording was not created, rollback also failed

Rendered in MATLAB R2023b by calling the PR's code with a stand-in figure, not on a rig. cancelOpenTransaction was given a stand-in connection whose rollback fails.

Tests

  • New tests/TestCancelOpenTransaction.m, added to CI, README and AGENTS.md. It needs no DB: the new fixture djconnfixture.FakeConnection has a cancelTransaction that throws, the way dj.Connection/cancelTransaction does on a dropped connection.
    • A failing rollback doesn't skip the rest of a catch block. The original error is reported first, the rollback failure is appended, both are kept as causes, and it works for errors with no identifier or with % in the message.
    • An open transaction is rolled back and the error returned unchanged. With no open transaction nothing is cancelled. A real (offline) dj.Connection with no transaction sends no query.
    • All 8 tests fail on 8bd2fb5c (Undefined function 'cancelOpenTransaction').
  • All MATLAB suites pass (66 tests). uvx prek run --all-files passes.

Not tested: a dropped connection during a real registration through the GUI on a rig.

Assisted-by: ClaudeCode:claude-opus-5-5

Registering a recording with "Add surgery & insertion device if missing"
crashed in registerSurgery and createRecording with 'Output argument
"connObj" ... not assigned' from dj.conn.

connect_tech hands its credentials straight to dj.conn and never stores
them in dj.config, so dj.conn() only works while the +dj/conn.m MATLAB
resolves still holds its saved connection. On the rig it held none (its
persistent cleared, or a second DataJoint copy on the path, such as the
Add-On the ViRMEn startup loads), and the vendored copy returns without
an output. The tables kept working because each schema keeps its own
connection. createRecording had already moved the files to cup when it
crashed outside any try, so the recording was never registered.

The new tableConn(<table>) returns the connection of the table's schema.
Every callback that ran a transaction or raw query through dj.conn now
uses it, so the transaction is also on the connection the inserts use.
createRecording takes it before moving any files, and every write
callback gets the connection and starts the transaction inside its try
and cancels only an open one, so a failure is reported instead of
escaping.

Fixes #50

Assisted-by: ClaudeCode:claude-opus-5-5
…ling

The five DB write callbacks (registerSurgery, createRecording,
CreateNewJob, RerunJob, RegisterPreParamList) called
conn.cancelTransaction first thing in their catch block. That runs a
ROLLBACK query, which throws when the connection dropped and cannot
reconnect, and then the rest of the catch never ran: no error dialog, no
Slack report, the button stayed disabled and the GUI "Busy". In
registerSurgery the surgery dialog stayed open, so createRecording stayed
blocked in addSurgeryData's uiwait.

The new cancelOpenTransaction(conn, err) rolls back an open transaction
inside its own try/catch. It returns the caught error unchanged, or, if
the rollback failed too, an error with the same identifier whose message
appends "(rollback also failed: ...)" and keeps both errors as causes.
Each callback now calls it in place of the inline cancel; the rest of
each catch block is unchanged.

Fixes #55

Assisted-by: ClaudeCode:claude-opus-5-5
It grepped the five write callbacks for cancelTransaction, so it broke on
harmless rewording. The remaining tests cover cancelOpenTransaction itself.

Assisted-by: ClaudeCode:claude-opus-5-5
createRecording showed 'Recording was not createdLost connection ...', and
RegisterPreParamList and registerSurgery ran their message into the error
the same way. The others used a bare space. Every error dialog that appends
err.message now uses ': '.

Assisted-by: ClaudeCode:claude-opus-5-5
@tabedzki
tabedzki marked this pull request as ready for review December 1, 2026 21:58
@tabedzki
tabedzki merged commit 292c3cf into main Dec 1, 2026
7 checks passed
@tabedzki
tabedzki deleted the t3/handle-rollback-failures branch December 1, 2026 22:18
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.

A failing rollback skips the rest of the write callbacks' error handling

1 participant