Skip to content

grass.temporal: Roll back a failed transaction - #8217

Open
Muhaddis-igis wants to merge 2 commits into
OSGeo:mainfrom
Muhaddis-igis:fix/8123-temporal-transactions
Open

Muhaddis-igis wants to merge 2 commits into
OSGeo:mainfrom
Muhaddis-igis:fix/8123-temporal-transactions

Conversation

@Muhaddis-igis

@Muhaddis-igis Muhaddis-igis commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

Follow-up to #8202 for DBConnection in grass.temporal.core: a failed execute_transaction() now saves nothing.

  • Before, when a statement failed, the transaction stayed open. On SQLite, the next execute_transaction() or the commit() in close() saved the statements that ran before the error. When the process exited instead, __del__() called close(), which committed them. On PostgreSQL, the connection stayed in the aborted transaction and the next execute_transaction() failed with InFailedSqlTransaction.
  • execute_transaction() now calls rollback() in finally. After a successful commit() there is nothing to roll back.
  • close() no longer commits, so a transaction that is still open is rolled back.
  • close() sets connected to False before it closes the cursor and the connection, and closes the connection even if closing the cursor raises. Before, an error there left connected at True, so a later close(), for example from __del__(), tried again on the broken connection, as noted in the review of grass.temporal: Close the SQLite connection in DBConnection.close() #8202 (grass.temporal: Close the SQLite connection in DBConnection.close() #8202 (review)). The error is still raised.
  • With psycopg2, the commit() in close() was what saved the writes made with execute() (insert(), update(), update_all() and delete() in base.py), because psycopg2 runs every statement in a transaction it does not commit. execute() now commits after the statement on PostgreSQL. SQLite connections are in autocommit mode (isolation_level = None), so execute() is unchanged there.

One behaviour change on PostgreSQL: writes made with execute() before a transaction are now saved even if the transaction fails, as on SQLite. For example, t.unregister updates each map's list of space time datasets with execute() before its transaction, so after a failed t.unregister the maps no longer list the STRDS, while the STRDS still lists them. Before this PR, PostgreSQL rolled these updates back with the failed transaction. I have not tested what this mismatch breaks later.

Five new tests in grass_temporal_core_db_connection_test.py check that a failed transaction saves nothing, also when the caller keeps the connection or an error other than a database error stops it, that close() rolls back an open transaction, and that close() leaves the object disconnected when closing fails.

Motivation and context

A failed transaction being saved in part was open point 1 in #8123 (comment). #8202 left it as it was.

How has this been tested?

Ubuntu 24.04 (WSL2), CMake build of main at 6ce29f8 and this branch, Python 3.13.18 with SQLite 3.53.1 in its sqlite3 module, PostgreSQL 16.15 with psycopg2 2.9.13.

  • grass_temporal_core_db_connection_test.py: 8 passed with this PR, 5 failed on main (output below).
  • nc_spm_full_v2beta1 on SQLite: 24 monthly maps for 2020 and 2021 computed from elevation, registered in several STRDS. On each STRDS, I tried to unregister June to November 2021 and made the attempt fail: t.unregister with its 4th DELETE failing (a trigger stands in for a real error), the same calls from a Python session that catches the error, and that session again where the commit fails with database is locked because another process is reading. On main, 3 or all 6 maps were gone afterwards although each attempt reported an error, t.info still counted 24, and the 2021 mean from t.rast.aggregate changed from 14.783 to 11.747 or 10.229. With this PR, all 24 maps were still there.
  • The same check on PostgreSQL: all 24 maps were still there with main and with this PR.
  • close() after the connection broke underneath it (close_raises_check.py, output below): on main, connected stays True in all three cases. With this PR it is False in all three. On PostgreSQL, close() no longer raises, because it no longer calls commit(). On SQLite, closing the cursor still raises ProgrammingError.
  • pytest steps of pytest.yml on SQLite: 2291 + 69 + 94 passed. python/grass/temporal/tests: 171 passed, 0 unclosed database warnings.
  • gunittest with nc_spm_full_v2beta1 on SQLite: temporal/ 45 of 46 files (test_t_register_raster.py fails in tearDownClass on main too), python/grass/temporal/ 13 of 13.
  • PostgreSQL: to run the existing tests there, I patched init() locally so that each new mapset gets its own database through t.connect driver=pg. I also had to work around a bug on main that is not part of this PR: t.create fails with TypeError: can only concatenate str (not "bytes") to str, because psycopg2's cursor.mogrify() returns bytes. These runs were on main at f3ea797 with grass.temporal: Close the SQLite connection in DBConnection.close() #8202, with the same transaction change (only the comments differ) and without the close() change. The pytest steps had the same 7 failures as without the change, gunittest on temporal/ the same 3 failing files of 47, and python/grass/temporal/ 13 of 13 files passed. Without the change, the algebra tests in python/grass/temporal/testsuite/ hung on a lock, because a connection stayed idle in transaction after an UPDATE made with execute().
  • pre-commit run on the changed files passes.
Test output

pytest -v python/grass/temporal/tests/grass_temporal_core_db_connection_test.py with core.py of main (6ce29f8) and of this PR:

===== main
PASSED test_close_closes_the_sqlite_connection
PASSED test_second_close_does_nothing
PASSED test_successful_transaction_is_saved
FAILED test_close_counts_a_broken_connection_as_closed
FAILED test_close_rolls_back_an_open_transaction
FAILED test_failed_transaction_saves_nothing
FAILED test_failed_transaction_is_not_saved_by_the_next_one
FAILED test_transaction_stopped_by_other_error_is_not_saved
--- test_close_counts_a_broken_connection_as_closed
E       assert not True
--- test_close_rolls_back_an_open_transaction
E       assert [1] == []
--- test_failed_transaction_saves_nothing
E       assert [1] == []
--- test_failed_transaction_is_not_saved_by_the_next_one
E       assert not True
--- test_transaction_stopped_by_other_error_is_not_saved
E       assert [1, 3] == [3]
=================== 5 failed, 3 passed, 7 warnings in 2.73s ====================
===== this PR
PASSED test_close_closes_the_sqlite_connection
PASSED test_second_close_does_nothing
PASSED test_close_counts_a_broken_connection_as_closed
PASSED test_close_rolls_back_an_open_transaction
PASSED test_successful_transaction_is_saved
PASSED test_failed_transaction_saves_nothing
PASSED test_failed_transaction_is_not_saved_by_the_next_one
PASSED test_transaction_stopped_by_other_error_is_not_saved
======================== 8 passed, 7 warnings in 1.08s =========================

close() after the connection broke underneath it. main is from the same check on 7e611ee, where close() was the same as on 6ce29f8:

===== main
SQLite, connection closed underneath: close() raised ProgrammingError: Cannot operate on a closed database.; connected afterwards: True
PostgreSQL, server ended the session: close() raised OperationalError: server closed the connection unexpectedly; connected afterwards: True
PostgreSQL, server ended the session with a write pending: close() raised OperationalError: server closed the connection unexpectedly; connected afterwards: True
===== this PR before the close() change
SQLite, connection closed underneath: close() raised ProgrammingError: Cannot operate on a closed database.; connected afterwards: True
PostgreSQL, server ended the session: close() returned; connected afterwards: False
PostgreSQL, server ended the session with a write pending: close() returned; connected afterwards: False
===== this PR
SQLite, connection closed underneath: close() raised ProgrammingError: Cannot operate on a closed database.; connected afterwards: False
PostgreSQL, server ended the session: close() returned; connected afterwards: False
PostgreSQL, server ended the session with a write pending: close() returned; connected afterwards: False

Failed unregister on nc_spm_full_v2beta1. unchanged and intended are references (nothing removed, six maps removed by a working t.unregister), tool, session and locked the failed attempts. "t.info" is number_of_maps from t.info -g, "listed" the maps in t.rast.list, "series avg" the mean of t.rast.series method=average, "2020" and "2021" the means of the t.rast.aggregate granularity="1 years" method=average maps, and "maps listing it" how many of the six maps still have the STRDS in their own list of datasets:

== SQLite
   version  dataset    t.info listed  series avg     2020    2021  maps listing it  removed
   main     unchanged      24     24      14.533   14.283  14.783           6 of 6  -
   main     intended       18     18      12.932   14.283  10.229           0 of 6  2021_06,2021_07,2021_08,2021_09,2021_10,2021_11
   main     tool           24     21      13.196   14.283  11.747           0 of 6  2021_06,2021_07,2021_08
   main     session        24     21      13.196   14.283  11.747           0 of 6  2021_06,2021_07,2021_08
   main     locked         24     18      12.932   14.283  10.229           0 of 6  2021_06,2021_07,2021_08,2021_09,2021_10,2021_11
   main     tool: t.unregister exit 1
   main     session: IntegrityError: simulated failure of one statement; open transaction afterwards: True
   main     locked: OperationalError: database is locked; open transaction afterwards: True
   this PR  unchanged      24     24      14.533   14.283  14.783           6 of 6  -
   this PR  intended       18     18      12.932   14.283  10.229           0 of 6  2021_06,2021_07,2021_08,2021_09,2021_10,2021_11
   this PR  tool           24     24      14.533   14.283  14.783           0 of 6  -
   this PR  session        24     24      14.533   14.283  14.783           0 of 6  -
   this PR  locked         24     24      14.533   14.283  14.783           0 of 6  -
   this PR  tool: t.unregister exit 1
   this PR  session: IntegrityError: simulated failure of one statement; open transaction afterwards: False
   this PR  locked: OperationalError: database is locked; open transaction afterwards: False

== PostgreSQL
   version  dataset    t.info listed  series avg     2020    2021  maps listing it  removed
   main     unchanged      24     24      14.533   14.283  14.783           6 of 6  -
   main     intended       18     18      12.932   14.283  10.229           0 of 6  2021_06,2021_07,2021_08,2021_09,2021_10,2021_11
   main     tool           24     24      14.533   14.283  14.783           6 of 6  -
   main     session        24     24      14.533   14.283  14.783           6 of 6  -
   main     tool: t.unregister exit 1
   main     session: RaiseException: simulated failure of one statement; open transaction afterwards: True
   this PR  unchanged      24     24      14.533   14.283  14.783           6 of 6  -
   this PR  intended       18     18      12.932   14.283  10.229           0 of 6  2021_06,2021_07,2021_08,2021_09,2021_10,2021_11
   this PR  tool           24     24      14.533   14.283  14.783           0 of 6  -
   this PR  session        24     24      14.533   14.283  14.783           0 of 6  -
   this PR  tool: t.unregister exit 1
   this PR  session: RaiseException: simulated failure of one statement; open transaction afterwards: False

pytest steps of pytest.yml on SQLite with this PR:

===== 2291 passed, 1 skipped, 1 xfailed, 103 warnings in 337.60s (0:05:37) =====
========== 69 passed, 4766 deselected, 2 warnings in 90.73s (0:01:30) ==========
======================== 94 passed, 2 xfailed in 2.20s =========================

I used an AI assistant to set up PostgreSQL, write the tests and run the test suites; I reviewed the code and the results myself.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 11:58

This comment was marked as off-topic.

@github-actions github-actions Bot added Python Related code is in Python libraries tests Related to Test Suite labels Nov 18, 2026
@Muhaddis-igis
Muhaddis-igis force-pushed the fix/8123-temporal-transactions branch 2 times, most recently from c31659a to ca908b2 Compare November 29, 2026 16:40
@Muhaddis-igis Muhaddis-igis changed the title grass.temporal: Close connections and roll back failed transactions grass.temporal: Roll back a failed transaction Nov 29, 2026
@Muhaddis-igis

Copy link
Copy Markdown
Contributor Author

The failed Ubuntu job is not related to this change. Its only failure is test_github_official_module_man_page_src_code_links_exists in scripts/g.extension/testsuite/test_addons_download.py, where github.com answered with a rate limit:

  File "scripts/g.extension/testsuite/test_addons_download.py", line 223, in test_github_official_module_man_page_src_code_links_exists
    self.assertEqual(response, 200)
AssertionError: 429 != 200

The same test fails the same way on main, for example on 7e611ee, the base of this PR. Could someone rerun the failed job?

When a statement in execute_transaction() failed, the transaction stayed
open. On SQLite, the next execute_transaction() or the commit() in
close() then saved the statements that ran before the error, also at
process exit through __del__(). On PostgreSQL, the connection stayed in
the aborted transaction and the next execute_transaction() failed.
Roll back in the finally block of execute_transaction().

close() no longer commits, so it rolls back a transaction that is still
open. With psycopg2, that commit() was what saved the writes made with
execute(), so execute() now commits after the statement on PostgreSQL.
SQLite connections are in autocommit mode and need no commit there.

Add tests that a failed transaction saves nothing, also when the caller
keeps the connection or an error other than a database error stops it.
If closing the cursor or the connection raised, close() stopped before
setting connected to False, so the object still counted as connected
and a later close() tried again on the broken connection. Set connected
to False first, and close the connection even if closing the cursor
raises. The error is still raised to the caller.

Add a test with an SQLite connection that was closed underneath.
@Muhaddis-igis
Muhaddis-igis force-pushed the fix/8123-temporal-transactions branch from ca908b2 to 5364aaf Compare December 4, 2026 06:17
@Muhaddis-igis

Copy link
Copy Markdown
Contributor Author

@echoix this is the follow-up you suggested in your review of #8202. It now also covers your point about close(): connected was left at True when closing raised. close() no longer calls commit(), and it sets connected to False before closing, so this no longer happens. I checked it on SQLite and PostgreSQL with a connection that broke underneath close(); the output is in the description.

I rebased onto the current main, which has #8307, so the g.extension test that failed earlier with 429 from GitHub no longer fetches the links.

Could you review it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libraries Python Related code is in Python tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants