8000 fix: INT-001 for a user function that calls its own transaction (minigraf#886) by adityamukho · Pull Request #21 · project-minigraf/minigraf-swift · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions Tests/swift/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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)
Expand Down
196 changes: 195 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}))
}
}
Expand All @@ -1012,31 +1013,78 @@ 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<Option<minigraf::OwnedWriteTransaction>>,
/// The thread running a statement of this transaction, which holds
/// `inner` meanwhile. A call from that thread comes from a user function
/// of the statement and would wait for `inner` forever (minigraf#886).
running: Mutex<Option<std::thread::ThreadId>>,
}

/// Clears `MiniGrafWriteTransaction::running` when the statement ends, also
/// by a panic.
struct Running<'a>(&'a Mutex<Option<std::thread::ThreadId>>);

impl Drop for Running<'_> {
fn drop(&mut self) {
*self.0.lock().unwrap_or_else(|e| e.into_inner()) = None;
}
}

impl MiniGrafWriteTransaction {
/// Whether this thread is running a statement of this transaction. Only
/// the thread that set `running` can read its own id there, so the answer
/// cannot change under the caller.
fn in_own_statement(&self) -> bool {
*self.running.lock().unwrap_or_else(|e| e.into_inner()) == Some(std::thread::current().id())
}

/// INT-001 for a call from a user function of this transaction's running
/// statement. Checked before `inner` is locked: another thread's call
/// passes and waits for the statement.
fn refuse_own_statement(&self) -> Result<(), MiniGrafError> {
if self.in_own_statement() {
return Err(MiniGrafError::Other {
msg: "[INT-001] a write transaction is already in progress on this thread; \
a user function cannot call the transaction it runs in"
.into(),
});
}
Ok(())
}
}

#[uniffi::export]
impl MiniGrafWriteTransaction {
/// Run one Datalog command inside the transaction. Returns the JSON
/// `MiniGrafDb.execute` returns for the same core result.
pub fn execute(&self, datalog: String) -> Result<String, MiniGrafError> {
self.refuse_own_statement()?;
let mut guard = lock(&self.inner)?;
let tx = guard.as_mut().ok_or_else(finished)?;
// Declared after `guard`, so cleared before `inner` is released.
*self.running.lock().unwrap_or_else(|e| e.into_inner()) = Some(std::thread::current().id());
let _running = Running(&self.running);
Ok(query_result_to_json(tx.execute(&datalog)?))
}

/// Commit every staged change as one transaction. A commit that fails
/// rolls the transaction back; either way it is finished.
pub fn commit(&self) -> Result<(), MiniGrafError> {
self.refuse_own_statement()?;
let tx = lock(&self.inner)?.take().ok_or_else(finished)?;
Ok(tx.commit()?)
}

/// Discard every staged change. Does nothing on a finished transaction.
pub fn rollback(&self) -> Result<(), MiniGrafError> {
self.refuse_own_statement()?;
if let Some(tx) = lock(&self.inner)?.take() {
tx.rollback();
}
Expand All @@ -1045,7 +1093,7 @@ impl MiniGrafWriteTransaction {

/// Whether the transaction is still open.
pub fn is_open(&self) -> bool {
self.inner.lock().is_ok_and(|g| g.is_some())
self.in_own_statement() || self.inner.lock().is_ok_and(|g| g.is_some())
}
}

Expand Down Expand Up @@ -2728,6 +2776,152 @@ mod tests {
assert_eq!(db_names(&db), ["Alice", "Bob", "Carol"]);
}

/// Calls the transaction it runs in (minigraf#886) and records what each
/// call returned.
#[derive(Default)]
struct CallsItsTx {
tx: Mutex<Option<Arc<MiniGrafWriteTransaction>>>,
seen: Mutex<Vec<(&'static str, String)>>,
fail: bool,
}
impl MiniGrafPredicate for CallsItsTx {
fn test(&self, _value: MiniGrafValue) -> Result<bool, MiniGrafUdfError> {
let tx = self.tx.lock().expect("tx").clone().expect("a transaction");
let calls: [(&'static str, Result<(), MiniGrafError>); 3] = [
("execute", tx.execute(NAMES.into()).map(|_| ())),
("commit", tx.commit()),
("rollback", tx.rollback()),
];
let mut first = None;
let mut seen = self.seen.lock().expect("seen");
for (name, result) in calls {
let text = result.err().map(|e| e.to_string()).unwrap_or_default();
first.get_or_insert_with(|| text.clone());
seen.push((name, text));
}
seen.push(("is_open", tx.is_open().to_string()));
if self.fail {
return Err(MiniGrafUdfError::Failed {
msg: first.unwrap_or_default(),
});
}
Ok(true)
}
}

const CALLS_TX: &str = "(query [:find ?n :where [?e :person/name ?n] [(calls-tx? ?n)]])";

/// A transaction on `db` with Carol staged and `calls-tx?` calling it.
fn tx_calling_itself(
db: &Arc<MiniGrafDb>,
fail: bool,
) -> (Arc<MiniGrafWriteTransaction>, Arc<CallsItsTx>) {
let udf = Arc::new(CallsItsTx {
fail,
..Default::default()
});
db.register_predicate("calls-tx?".into(), udf.clone())
.expect("register");
let tx = db.begin_write().expect("begin");
tx.execute(r#"(transact [[:carol :person/name "Carol"]])"#.into())
.expect("transact");
*udf.tx.lock().expect("tx") = Some(tx.clone());
(tx, udf)
}

#[test]
fn tx_refuses_a_call_from_its_own_statement() {
let db = tx_db();
let (tx, udf) = tx_calling_itself(&db, false);
let tx2 = tx.clone();
let (result, _recv) = within(STUCK, move || tx2.execute(CALLS_TX.into()));
let rows = result
.expect("the statement returns: no deadlock")
.expect("the function swallowed the refusals");
assert_eq!(names_of(&rows), ["Alice", "Bob", "Carol"]);
let seen = udf.seen.lock().expect("seen");
assert_eq!(seen.len(), 12, "four calls for each of three rows");
for (name, text) in seen.iter() {
if *name == "is_open" {
assert_eq!(text, "true", "open while its statement runs");
} else {
assert!(text.contains("[INT-001]"), "{name} is refused with INT-001");
}
}
drop(seen);
*udf.tx.lock().expect("tx") = None;
// Neither the refused commit nor the refused rollback finished it.
assert!(tx.is_open());
tx.commit().expect("the transaction still commits");
assert_eq!(db_names(&db), ["Alice", "Bob", "Carol"]);
}

#[test]
fn tx_refused_call_fails_the_query_when_passed_on() {
let db = tx_db();
let (tx, udf) = tx_calling_itself(&db, true);
let tx2 = tx.clone();
let (result, _recv) = within(STUCK, move || tx2.execute(CALLS_TX.into()));
let err = result
.expect("the statement returns: no deadlock")
.expect_err("the function passed the refusal on");
assert_eq!(code(&err), "QRY-014");
assert!(err.to_string().contains("INT-001"), "the cause is INT-001");
*udf.tx.lock().expect("tx") = None;
tx.rollback().expect("rollback");
assert_eq!(db_names(&db), ["Alice", "Bob"]);
}

/// Blocks inside a statement until told to go on.
struct Waits {
entered: Mutex<std::sync::mpsc::Sender<()>>,
go_on: Mutex<std::sync::mpsc::Receiver<()>>,
}
impl MiniGrafPredicate for Waits {
fn test(&self, _value: MiniGrafValue) -> Result<bool, MiniGrafUdfError> {
let _ = self.entered.lock().expect("entered").send(());
let _ = self.go_on.lock().expect("go_on").recv();
Ok(true)
}
}

#[test]
fn tx_call_from_another_thread_waits_for_the_statement() {
let db = tx_db();
let (entered, has_entered) = std::sync::mpsc::channel();
let (go_on, may_go_on) = std::sync::mpsc::channel();
db.register_predicate(
"waits?".into(),
Arc::new(Waits {
entered: Mutex::new(entered),
go_on: Mutex::new(may_go_on),
}),
)
.expect("register");
let tx = db.begin_write().expect("begin");
let tx2 = tx.clone();
let statement = std::thread::spawn(move || {
tx2.execute(
r#"(query [:find ?n :where [:alice :person/name ?n] [(waits? ?n)]])"#.into(),
)
});
has_entered.recv_timeout(STUCK).expect("the statement runs");
// This thread is not the one running the statement: it waits.
let tx3 = tx.clone();
let (early, later) = within(WAITS, move || {
tx3.execute(r#"(transact [[:carol :person/name "Carol"]])"#.into())
});
assert!(early.is_none(), "a call from another thread waits");
go_on.send(()).expect("go on");
statement.join().expect("join").expect("the statement");
later
.recv_timeout(STUCK)
.expect("the waiting call runs")
.expect("transact");
tx.commit().expect("commit");
assert_eq!(db_names(&db), ["Alice", "Bob", "Carol"]);
}

#[test]
fn tx_rules_stay_after_rollback() {
let db = tx_db();
Expand Down
Loading
0