diff --git a/README.md b/README.md
index 867f70a..4d60e39 100644
--- a/README.md
+++ b/README.md
@@ -244,7 +244,7 @@ try db.withWriteTransaction(options: TxOptions(chunkFacts: 10_000)) { tx in
it when the last reference goes, but end it yourself rather than rely on that.
- An open transaction holds the database's write lock until it finishes, so end every
transaction explicitly or through `withWriteTransaction`. On the thread that began it, database
- calls (`execute`, `query`, `prepare`, `factLog`, `checkpoint`, another `beginWrite`, prepared queries) fail with INT-001, and so do user functions its statements run. On
+ calls (`execute`, `query`, `prepare`, `factLog`, `checkpoint`, another `beginWrite`, prepared queries) fail with INT-001, and so do user functions its statements run. A user function that calls the transaction it runs in (`execute`, `commit`, `rollback`) gets INT-001 too. On
other threads, writes and begins wait until it ends; queries read the committed
state. Use a transaction on the thread that began it: a thread that uses one it did
not begin and then writes through the database waits on that transaction.
diff --git a/Tests/swift/main.swift b/Tests/swift/main.swift
index 8b44854..0338661 100644
--- a/Tests/swift/main.swift
+++ b/Tests/swift/main.swift
@@ -259,6 +259,21 @@ final class Throws: MiniGrafPredicate {
func test(value: MiniGrafValue) throws -> Bool { throw Own() }
}
+/// Calls the transaction whose statement runs it (minigraf#886) and counts
+/// the calls refused with INT-001.
+final class CallsItsTx: MiniGrafPredicate {
+ let tx: MiniGrafWriteTransaction
+ init(_ tx: MiniGrafWriteTransaction) { self.tx = tx }
+ func test(value: MiniGrafValue) throws -> Bool {
+ let refused = [
+ errorText { _ = try tx.execute(datalog: "(query [:find ?n :where [?e :person/name ?n]])") },
+ errorText { try tx.commit() },
+ errorText { try tx.rollback() },
+ ].allSatisfy { $0.hasPrefix("[INT-001]") }
+ return refused && tx.isOpen()
+ }
+}
+
final class BadRef: MiniGrafAggregate {
final class Acc: MiniGrafAccumulator {
func step(value: MiniGrafValue) throws {}
@@ -522,6 +537,17 @@ do {
}
let again = try MiniGrafDb.open(path: p)
check(namesOf(try again.execute(datalog: names)).count == 7, "commits survive reopen")
+
+ // minigraf#886: a user function calling its own transaction waited forever.
+ let own = try MiniGrafDb.openInMemory()
+ _ = try own.execute(datalog: "(transact [[:alice :person/name \"Alice\"] [:bob :person/name \"Bob\"]])")
+ let tx = try own.beginWrite()
+ try own.registerPredicate(name: "calls-tx?", predicate: CallsItsTx(tx))
+ // A row passes only if every call was refused with INT-001 and isOpen was true.
+ check(namesOf(try tx.execute(datalog: "(query [:find ?n :where [?e :person/name ?n] [(calls-tx? ?n)]])")) == ["Alice", "Bob"],
+ "INT-001 for a user function calling its own transaction")
+ check(tx.isOpen(), "the refused commit and rollback left it open")
+ try tx.commit()
}
try? FileManager.default.removeItem(at: dir)
diff --git a/src/lib.rs b/src/lib.rs
index 929a152..09ad559 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -995,6 +995,7 @@ impl MiniGrafDb {
let tx = self.db()?.begin_write_owned_with(options)?;
Ok(Arc::new(MiniGrafWriteTransaction {
inner: Mutex::new(Some(tx)),
+ running: Mutex::new(None),
}))
}
}
@@ -1012,10 +1013,51 @@ impl MiniGrafDb {
/// whole: one used from two threads at once runs their calls one after the
/// other. It may be used and finished on any thread; INT-001 stays on the
/// thread that began it. It keeps its database open until it is freed.
+///
+/// A user function cannot call the transaction whose statement it runs in:
+/// there `execute`, `commit` and `rollback` fail with INT-001 and change
+/// nothing, and `is_open` is true.
#[derive(uniffi::Object)]
pub struct MiniGrafWriteTransaction {
/// `None` once finished.
inner: Mutex