Skip to content

Database.insert_many's per-row fallback opens a transaction with no rollback path, so a bad value leaves it open for the next caller #7882

Closed

Description

@rspereiratech

Summary

#7108 gave Database.run_in_transaction() in the Python bindings a BaseException handler that rolls back,
so an ordinary bug in the callable cannot leave an open transaction for the next caller to inherit. The same
class has a second begin()/commit() site that was not touched: the per-row fallback inside
Database.insert_many(). It calls begin() and then loops new_document / set / save / commit with no
try/finally and no rollback path at all.

That branch is entered precisely when json.dumps(rows) has already raised, i.e. when the rows contain values
the fast path could not serialise. Those are the rows most likely to make doc.set(k, v) raise in turn, so
the unprotected path is the one with the highest chance of throwing.

Where (HEAD 06b033d)

bindings/python/src/arcadedb_embedded/core.py:263-284:

        try:
            payload = _json.dumps(rows)
        except (TypeError, ValueError):
            # Non-JSON-representable values (note: numpy integer scalars land
            # here too, since json.dumps rejects np.int64): per-row fallback,
            # honoring commit_every batches like the fast path.
            n = 0
            was_active = self.is_transaction_active()
            if not was_active:
                self.begin()
            for row in rows:
                doc = self.new_document(type_name)
                for k, v in row.items():
                    doc.set(k, v)
                doc.save()
                n += 1
                if not was_active and commit_every > 0 and n % commit_every == 0:
                    self.commit()
                    self.begin()
            if not was_active:
                self.commit()
            return n

For contrast, the site #7108 fixed, core.py:152-180:

        for attempt in range(retries + 1):
            self.begin()
            try:
                result = fn()
                self.commit()
                return result
            except BaseException as e:
                # Any exit path other than a successful commit must roll back,
                # not just ArcadeDBError - matching LocalDatabase.transaction()'s
                # `catch (final Throwable e)` on the Java side (#7108). An
                # ordinary bug in fn() (TypeError, KeyError, ...) must not leave
                # an open transaction for the next caller to inherit, and neither
                # must KeyboardInterrupt/SystemExit, which `except Exception`
                # does not catch (code review on #7108).
                try:
                    if self.is_transaction_active():
                        self.rollback()
                except Exception:  # nosec B110 - best-effort rollback before retry
                    pass

Repro

db.insert_many("Doc", [{"a": datetime.now()}, {"b": SomethingUnsettable()}])
  1. json.dumps rejects the datetime, so the fallback branch runs.
  2. was_active is False, begin() opens a transaction.
  3. doc.set("b", ...) raises on the second row.
  4. The exception propagates out of insert_many with the transaction still open, holding the first row's
    write.

Whatever the caller does next runs inside that inherited transaction: a later commit() publishes a partial
batch the caller believes was never written, and a later begin() on an already-active transaction is a
second surprise. With the commit_every loop the window is worse, because the batches committed before the
failure are already durable while the caller sees only an exception.

Suggested fix

Wrap the loop so that any exit path other than the successful commit() rolls back when was_active is
False, mirroring run_in_transaction:

            if not was_active:
                self.begin()
            try:
                ...
                if not was_active:
                    self.commit()
                return n
            except BaseException:
                if not was_active:
                    try:
                        if self.is_transaction_active():
                            self.rollback()
                    except Exception:  # nosec B110 - best-effort rollback
                        pass
                raise

BaseException rather than Exception, for the reason the #7108 review gave: KeyboardInterrupt and
SystemExit must not leave a transaction open either.

Separately, bindings/python/src/arcadedb_embedded/graph_batch.py:139 catches only Exception around its own
begin()/commit() pair in GraphBatch.create_vertex(), so a KeyboardInterrupt between the begin() at
:125 and the commit() at :136 leaks the transaction it started. Same reasoning, same one-word change.

Scope / confidence

Checked at HEAD 06b033d: bindings/python/src/arcadedb_embedded/core.py is untouched since the #7108
fix, and these are the only two begin() sites in the file. The was_active guard means an
insert_many called from inside a caller's own transaction does not commit or roll back the caller's work,
which the suggested fix preserves. No issue in the last 1000 covers insert_many; #7108 is the sibling and
is closed.

What would falsify it: if doc.set() could not raise for a value that json.dumps rejected. It can, and the
branch's own comment names np.int64 as a value that reaches it, which is a type the Java side has to be
able to convert.

Activity

  1. added this to the 26.10.1 milestone on Sep 18, 2026
  2. added
    severity:major
    on Sep 19, 2026
  3. self-assigned this
    on Oct 1, 2026
  4. added
    in progress
    and removed
    in progress
    on Oct 1, 2026
  5. added a commit that references this issue on Oct 3, 2026
  6. added
    in progress
    on Oct 3, 2026
  7. added a commit that references this issue on Oct 3, 2026
  8. added a commit that references this issue on Oct 6, 2026
  9. robfrank commented on Oct 22, 2026

    @robfrank
    Collaborator

    Fixed by #8828. Database.insert_many() could leave a transaction open after a failure: its per-row fallback (used when the rows cannot be JSON-encoded, for example datetime or np.int64 values) had no rollback, and the JSON fast path had the same leak, for example on a mandatory-property validation failure. Both paths now roll back the transaction they opened on every failure, including KeyboardInterrupt, and GraphBatch.create_vertex() now rolls back on KeyboardInterrupt and SystemExit too, which still propagate unwrapped while ordinary failures are still wrapped in ArcadeDBError. One behaviour change: when insert_many runs inside a transaction you opened yourself, the fast path no longer commits it every commit_every rows, matching what the fallback already did.

    Still open: nothing, the fix is complete. With commit_every > 0 and no transaction of your own, batches committed before a failure stay committed; only the open batch is rolled back.


    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  10. removed
    in progress
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugpython-bindingsseverity:major

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions