Skip to content

A SQL DELETE that loses a race with a concurrent delete of the same record fails with RecordNotFoundException (VertexNotFoundException for a vertex, advising CHECK DATABASE FIX), which transaction(..., retries) does not retry, while UPDATE in the same race gets a retryable conflict #8984

Closed

Description

@tae898

Under the default READ_COMMITTED isolation, DELETE FROM Doc WHERE key = 1 selects the record, and if another transaction deletes that record and commits before the statement removes it, the statement fails with RecordNotFoundException. That is not a NeedRetryException, so database.transaction(block, false, retries) passes it to the caller after the first attempt. Every other shape in the repro survives the same race: UPDATE Doc SET v = 1 WHERE key = 1 gets a ConcurrentModificationException and its retry updates 0 rows, the same DELETE under REPEATABLE_READ is retried and deletes 0 rows, and openCypher MATCH (x:Vtx {key: 1}) DELETE x commits. For a vertex, the DELETE fails with VertexNotFoundException, whose message says the vertex "was reached through a reference to a record that is gone (a stale edge entry, or a RID held past the delete)" and asks for CHECK DATABASE FIX. Nothing in the database needs a repair: another transaction deleted the vertex a moment earlier.

For a document, the exception comes from LocalBucket.deleteRecordInternal, which loads the page with getPageToModify and finds the slot already freed in the newest committed version. For a vertex, it comes from GraphEngine.missingVertexOnDelete (#6586). An update in the same position is caught at commit and reported as a ConcurrentModificationException ("updated in transaction was deleted by a concurrent transaction", #4959). #6572 and #6586 made a missing vertex non-retryable on purpose, for a stale reference where "retrying cannot help, and no concurrent transaction was involved". In this race a concurrent transaction is involved, and a retry does help.

It happens without any forcing. In a separate run, 12 threads each ran 2,000 transactions of DELETE FROM K WHERE key = ? or INSERT INTO K SET key = ? (chosen at random) on 5 keys with a UNIQUE index, each inside db.transaction(..., false, 1000). Between 21 and 31 of the roughly 12,000 deletes per run ended in RecordNotFoundException from LocalBucket.deleteRecordInternal via DeleteStep, in 5 of 5 runs on Temurin 25 and 5 of 5 on Temurin 21, on 12 cores. The repro below uses a before-delete listener only to fix the timing, so it fails on every run.

Repro

javac -cp 'lib/*' -d cls DeleteRaceNotRetried.java
java -Xmx8g -XX:+UseCompactObjectHeaders --add-modules=jdk.incubator.vector --enable-native-access=ALL-UNNAMED -cp 'lib/*:cls' DeleteRaceNotRetried /tmp/DeleteRaceNotRetried-db

lib/ holds the engine jar and its dependencies. On Temurin 21, use the same command without -XX:+UseCompactObjectHeaders.

import com.arcadedb.database.*;
import com.arcadedb.event.*;
import com.arcadedb.query.sql.executor.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;

public class DeleteRaceNotRetried {
  public static void main(String[] args) throws Exception {
    DatabaseFactory factory = new DatabaseFactory(args[0]);
    if (factory.exists())
      factory.open().drop();
    Database db = factory.create();
    db.command("sql", "CREATE DOCUMENT TYPE Doc");
    db.command("sql", "CREATE PROPERTY Doc.key INTEGER");
    db.command("sql", "CREATE INDEX ON Doc (key) UNIQUE");
    db.command("sql", "CREATE VERTEX TYPE Vtx");
    db.command("sql", "CREATE PROPERTY Vtx.key INTEGER");
    db.command("sql", "CREATE INDEX ON Vtx (key) UNIQUE");

    // When the statement on the main thread is about to write the target record, another thread deletes that
    // record and commits. This only fixes the timing of a race that concurrent deletes of the same key hit on their own.
    ExecutorService otherThread = Executors.newSingleThreadExecutor();
    AtomicReference<RID> target = new AtomicReference<>();
    AtomicBoolean armed = new AtomicBoolean();
    Runnable concurrentDelete = () -> {
      if (armed.compareAndSet(true, false))
        try {
          otherThread.submit(() -> db.transaction(() -> db.deleteRecord(db.lookupByRID(target.get(), true)))).get();
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
    };
    db.getEvents().registerListener((BeforeRecordDeleteListener) record -> {
      if (record.getIdentity().equals(target.get()))
        concurrentDelete.run();
      return true;
    });
    db.getEvents().registerListener((BeforeRecordUpdateListener) record -> {
      if (record.getIdentity().equals(target.get()))
        concurrentDelete.run();
      return true;
    });

    run(db, target, armed, "Doc", "DELETE FROM Doc WHERE key = 1", Database.TRANSACTION_ISOLATION_LEVEL.READ_COMMITTED);
    run(db, target, armed, "Doc", "DELETE FROM Doc WHERE key = 1", Database.TRANSACTION_ISOLATION_LEVEL.REPEATABLE_READ);
    run(db, target, armed, "Doc", "UPDATE Doc SET v = 1 WHERE key = 1", Database.TRANSACTION_ISOLATION_LEVEL.READ_COMMITTED);
    run(db, target, armed, "Vtx", "DELETE FROM Vtx WHERE key = 1", Database.TRANSACTION_ISOLATION_LEVEL.READ_COMMITTED);
    run(db, target, armed, "Vtx", "MATCH (x:Vtx {key: 1}) DELETE x", Database.TRANSACTION_ISOLATION_LEVEL.READ_COMMITTED);
    otherThread.shutdown();
    db.close();
  }

  static void run(Database db, AtomicReference<RID> target, AtomicBoolean armed, String type, String sql,
      Database.TRANSACTION_ISOLATION_LEVEL isolation) {
    db.transaction(() -> target.set((type.equals("Doc") ? db.newDocument(type) : db.newVertex(type)).set("key", 1).save().getIdentity()));
    armed.set(true);
    db.setTransactionIsolationLevel(isolation);
    AtomicInteger attempts = new AtomicInteger();
    String language = sql.startsWith("MATCH") ? "opencypher" : "sql";
    long[] count = new long[1];
    String outcome;
    try {
      db.transaction(() -> {
        attempts.incrementAndGet();
        try (ResultSet rs = db.command(language, sql)) {
          count[0] = language.equals("sql") ? ((Number) rs.next().getProperty("count")).longValue() : rs.stream().count();
        }
      }, false, 3);
      outcome = "committed, " + (language.equals("sql") ? "count = " : "rows returned = ") + count[0];
    } catch (Exception e) {
      outcome = e.getClass().getSimpleName() + ": " + e.getMessage();
    }
    db.setTransactionIsolationLevel(Database.TRANSACTION_ISOLATION_LEVEL.READ_COMMITTED);
    System.out.println(isolation + ", " + sql + ", concurrent delete ran: " + !armed.get() + ", transaction(..., 3): attempts "
        + attempts + ", " + outcome);
  }
}

Output on main at 5e44bc7ab3, embedded, identical on Temurin 25 (with compact object headers) and Temurin 21:

READ_COMMITTED, DELETE FROM Doc WHERE key = 1, concurrent delete ran: true, transaction(..., 3): attempts 1, RecordNotFoundException: Record #1:0 not found
REPEATABLE_READ, DELETE FROM Doc WHERE key = 1, concurrent delete ran: true, transaction(..., 3): attempts 2, committed, count = 0
READ_COMMITTED, UPDATE Doc SET v = 1 WHERE key = 1, concurrent delete ran: true, transaction(..., 3): attempts 2, committed, count = 0
READ_COMMITTED, DELETE FROM Vtx WHERE key = 1, concurrent delete ran: true, transaction(..., 3): attempts 1, VertexNotFoundException: Vertex #3:0 does not exist, so it cannot be deleted: it was reached through a reference to a record that is gone (a stale edge entry, or a RID held past the delete). Retrying cannot make it exist: run `CHECK DATABASE FIX` to drop the adjacency entries that still point at it - the RECORD scope cannot help here, since the edge list it would rebuild belongs to the record that is gone, and the entry to drop lives on the vertex at the other end, which only a sweep can find
READ_COMMITTED, MATCH (x:Vtx {key: 1}) DELETE x, concurrent delete ran: true, transaction(..., 3): attempts 1, committed, rows returned = 0

Versions affected: main 5e44bc7ab3, main 417314c18 (2026-09-17), and 26.9.1 print the same lines, on both JDKs.

Also on main at 66541f57d9 (2026-10-03): the same output on Temurin 25 and Temurin 21.

Expected: the losing DELETE either deletes 0 rows, as openCypher's DELETE does, or raises a retryable ConcurrentModificationException, as UPDATE and REPEATABLE_READ do. Either way transaction(..., retries) completes. A vertex deleted by a concurrent transaction is not reported as a stale reference that needs CHECK DATABASE FIX.

Activity

  1. added
    concurrency
    engine
    severity:minor
    on Oct 3, 2026
  2. added
    in progress
    on Oct 3, 2026
  3. self-assigned this
    on Oct 3, 2026
  4. added this to the 26.10.1 milestone on Oct 3, 2026
  5. added 2 commits that reference this issue on Oct 3, 2026
  6. lvca commented on Oct 3, 2026

    @lvca
    Member

    Fixed in #8989 (26.10.1). A DELETE that lost a race with a concurrent delete of the same record failed with a non-retryable not-found error. It now raises a retryable ConcurrentModificationException, so database.transaction(..., retries) completes. References that were never read (stale edge entries) keep the previous error.

    ❤️ 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.

  7. removed
    in progress
    on Oct 3, 2026
  8. tae898 commented on Oct 3, 2026

    @tae898
    ContributorAuthor

    Verified on main at 3d67d5c5c1 (the #8989 merge, engine built from that commit) against 5e44bc7ab3, the build this was filed on, with the issue's reproduction, on Temurin 25 with -XX:+UseCompactObjectHeaders and on Temurin 21 (both print the same lines). The SQL rows are fixed; one openCypher row changed in a way that may not be intended:

    statement 5e44bc7ab3 3d67d5c5c1
    DELETE FROM Doc WHERE key = 1, READ_COMMITTED attempts 1, RecordNotFoundException attempts 2, committed, count = 0
    DELETE FROM Vtx WHERE key = 1, READ_COMMITTED attempts 1, VertexNotFoundException attempts 2, committed, count = 0
    MATCH (x:Vtx {key: 1}) DELETE x, READ_COMMITTED attempts 1, committed, rows returned = 0 attempts 1, CommandExecutionException: Error executing Cypher command: MATCH (x:Vtx {key: 1}) DELETE x

    The other two rows (REPEATABLE_READ and UPDATE) are unchanged. The openCypher statement went from a successful no-op to a failure that database.transaction(..., 3) does not retry. Printing the causes (by changing the outcome line of the issue's program to append each cause's class and message) shows why: CommandExecutionException <- ConcurrentModificationException: Record #3:0 was deleted by a concurrent transaction after it was read in this transaction. Please retry the operation <- VertexNotFoundException: Vertex #3:0 does not exist, so it cannot be deleted: it was reached through a reference to a record that is gone .... The ConcurrentModificationException is the retryable error this issue asked for, but the Cypher executor wraps it in a CommandExecutionException, which is not a NeedRetryException, so the retry loop sees a non-retryable exception.

    Either of the two outcomes the issue named would complete the retry loop for this statement as well: the Cypher path rethrows the NeedRetryException unwrapped, as the SQL path does, or a vertex that vanished between the match and the delete counts as 0 rows, as it did before this fix.

  9. added a commit that references this issue on Oct 3, 2026
  10. added 2 commits that reference this issue on Oct 6, 2026
  11. added 5 commits that reference this issue on Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugconcurrencyengineseverity:minorsql

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions