Skip to main content

uqa_engine/transactions/
close.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Session closure owns rollback and a separately completed temporary-catalog cleanup transaction.
8
9use super::{Engine, SQLError, TransactionScope};
10use std::sync::atomic::{AtomicU8, Ordering};
11
12#[repr(u8)]
13enum ClosureState {
14    Open,
15    Closing,
16    Closed,
17}
18
19pub(crate) struct SessionClosure(AtomicU8);
20
21impl Default for SessionClosure {
22    fn default() -> Self {
23        Self(AtomicU8::new(ClosureState::Open as u8))
24    }
25}
26
27impl SessionClosure {
28    pub(crate) fn is_open(&self) -> bool {
29        self.0.load(Ordering::Acquire) == ClosureState::Open as u8
30    }
31
32    /// The statement gate serializes transitions. Automatic termination starts closure once; after a failed attempt only the explicit owner may resolve its receipt and retry.
33    fn begin(&self, explicit: bool) -> bool {
34        match self.0.load(Ordering::Acquire) {
35            state if state == ClosureState::Closed as u8 => false,
36            state if state == ClosureState::Closing as u8 && !explicit => false,
37            _ => {
38                self.0.store(ClosureState::Closing as u8, Ordering::Release);
39                true
40            }
41        }
42    }
43
44    fn finish(&self) {
45        self.0.store(ClosureState::Closed as u8, Ordering::Release);
46    }
47}
48
49impl Engine {
50    /// Roll back open transactions, remove this session's temporary objects and their dependents, and release its runtime resources. A failed completion retains the session and its transaction receipt for retry. Successful closure is idempotent; subsequent query use is unsupported.
51    pub fn close(&self) -> Result<(), SQLError> {
52        self.close_session(true)
53    }
54
55    pub(crate) fn close_terminated_session(&self) -> Result<(), SQLError> {
56        self.close_session(false)
57    }
58
59    fn close_session(&self, explicit: bool) -> Result<(), SQLError> {
60        let _statement = self.lock_statement_gate();
61        let admitted = self.session.closure.begin(explicit);
62        self.runtime.terminations.disarm();
63        if !admitted {
64            return Ok(());
65        }
66        self.runtime
67            .cancellation
68            .with_deferred_cancellation(|| self.finish_session_close())
69    }
70
71    fn finish_session_close(&self) -> Result<(), SQLError> {
72        while self.transaction_depth() != 0 {
73            self.rollback()?;
74        }
75        self.session.portals.lock().clear();
76        if self.temporary_namespace_allocated() {
77            let scope = TransactionScope::begin_cleanup(self)?;
78            self.run_transaction_scope(
79                scope,
80                Self::discard_temporary_objects,
81                std::convert::identity,
82            )?;
83        }
84        self.clear_notification_listener_without_transaction()?;
85        if let Some(cache) = self.session.regex_cache.lock().take() {
86            cache.clear();
87        }
88        self.row_locks.close_temporary_roles(self.session_id);
89        self.release_automatic_statistics_client();
90        self.session.closure.finish();
91        Ok(())
92    }
93}