Skip to main content

rusqlite/
busy.rs

1//! Busy handler (when the database is locked)
2use std::ffi::{c_int, c_void};
3use std::panic::catch_unwind;
4use std::time::Duration;
5
6use crate::ffi;
7use crate::{Connection, InnerConnection, Result};
8
9impl Connection {
10    /// Set a busy handler that sleeps for a specified amount of time when a
11    /// table is locked. The handler will sleep multiple times until at
12    /// least "ms" milliseconds of sleeping have accumulated.
13    ///
14    /// Calling this routine with an argument equal to zero turns off all busy
15    /// handlers.
16    ///
17    /// There can only be a single busy handler for a particular database
18    /// connection at any given moment. If another busy handler was defined
19    /// (using [`busy_handler`](Connection::busy_handler)) prior to calling this
20    /// routine, that other busy handler is cleared.
21    ///
22    /// Newly created connections currently have a default busy timeout of
23    /// 5000ms, but this may be subject to change.
24    pub fn busy_timeout(&self, timeout: Duration) -> Result<()> {
25        let ms: i32 = timeout
26            .as_secs()
27            .checked_mul(1000)
28            .and_then(|t| t.checked_add(timeout.subsec_millis().into()))
29            .and_then(|t| t.try_into().ok())
30            .expect("too big");
31        self.db.borrow_mut().busy_timeout(ms)
32    }
33
34    /// Register a callback to handle `SQLITE_BUSY` errors.
35    ///
36    /// If the busy callback is `None`, then `SQLITE_BUSY` is returned
37    /// immediately upon encountering the lock. The argument to the busy
38    /// handler callback is the number of times that the
39    /// busy handler has been invoked previously for the
40    /// same locking event. If the busy callback returns `false`, then no
41    /// additional attempts are made to access the
42    /// database and `SQLITE_BUSY` is returned to the
43    /// application. If the callback returns `true`, then another attempt
44    /// is made to access the database and the cycle repeats.
45    ///
46    /// There can only be a single busy handler defined for each database
47    /// connection. Setting a new busy handler clears any previously set
48    /// handler. Note that calling [`busy_timeout()`](Connection::busy_timeout)
49    /// or evaluating `PRAGMA busy_timeout=N` will change the busy handler
50    /// and thus clear any previously set busy handler.
51    ///
52    /// Newly created connections default to a
53    /// [`busy_timeout()`](Connection::busy_timeout) handler with a timeout
54    /// of 5000ms, although this is subject to change.
55    pub fn busy_handler<F>(&mut self, callback: Option<F>) -> Result<()>
56    where
57        F: FnMut(i32) -> bool + Send + 'static,
58    {
59        unsafe extern "C" fn busy_handler_callback<F>(p_arg: *mut c_void, count: c_int) -> c_int
60        where
61            F: FnMut(i32) -> bool,
62        {
63            unsafe {
64                c_int::from(
65                    catch_unwind(|| {
66                        let handler_fn = p_arg.cast::<F>();
67                        (*handler_fn)(count)
68                    })
69                    .unwrap_or_default(),
70                )
71            }
72        }
73        let x = callback.as_ref().map(|_| busy_handler_callback::<F> as _);
74        let mut c = self.db.borrow_mut();
75        c.set_clientdata(c"sqlite3_busy_handler", callback, |db, bh| unsafe {
76            if bh.is_null() {
77                ffi::sqlite3_busy_handler(db, None, bh)
78            } else {
79                ffi::sqlite3_busy_handler(db, x, bh)
80            }
81        })?;
82        Ok(())
83    }
84}
85
86impl InnerConnection {
87    #[inline]
88    fn busy_timeout(&mut self, timeout: c_int) -> Result<()> {
89        let r = unsafe { ffi::sqlite3_busy_timeout(self.db, timeout) };
90        let res = self.decode_result(r);
91        if res.is_ok() {
92            self.set_clientdata(c"sqlite3_busy_handler", None::<c_void>, |_, _| {
93                ffi::SQLITE_OK
94            })?;
95        }
96        res
97    }
98}
99
100#[cfg(all(test, not(miri)))]
101mod test {
102    #[cfg(all(target_family = "wasm", target_os = "unknown"))]
103    use wasm_bindgen_test::wasm_bindgen_test as test;
104
105    use crate::{Connection, ErrorCode, Result, TransactionBehavior};
106    use std::sync::atomic::{AtomicBool, Ordering};
107
108    #[cfg_attr(
109        all(target_family = "wasm", target_os = "unknown"),
110        ignore = "no filesystem on this platform"
111    )]
112    #[test]
113    fn test_default_busy() -> Result<()> {
114        let temp_dir = tempfile::tempdir().unwrap();
115        let path = temp_dir.path().join("test.db3");
116
117        let mut db1 = Connection::open(&path)?;
118        let tx1 = db1.transaction_with_behavior(TransactionBehavior::Exclusive)?;
119        let db2 = Connection::open(&path)?;
120        let r: Result<()> = db2.query_row("PRAGMA schema_version", [], |_| unreachable!());
121        assert_eq!(
122            r.unwrap_err().sqlite_error_code(),
123            Some(ErrorCode::DatabaseBusy)
124        );
125        tx1.rollback()
126    }
127
128    #[cfg_attr(
129        all(target_family = "wasm", target_os = "unknown"),
130        ignore = "no filesystem on this platform"
131    )]
132    #[test]
133    fn test_busy_handler() -> Result<()> {
134        static CALLED: AtomicBool = AtomicBool::new(false);
135        fn busy_handler(n: i32) -> bool {
136            if n > 2 {
137                false
138            } else {
139                CALLED.swap(true, Ordering::Relaxed)
140            }
141        }
142
143        let temp_dir = tempfile::tempdir().unwrap();
144        let path = temp_dir.path().join("busy-handler.db3");
145
146        let mut db1 = Connection::open(&path)?;
147        db1.execute_batch("CREATE TABLE IF NOT EXISTS t(a)")?;
148        let mut db2 = Connection::open(&path)?;
149        db2.busy_handler(Some(busy_handler))?;
150        db1.execute_batch("BEGIN EXCLUSIVE")?;
151        let err = db2.prepare("SELECT * FROM t").unwrap_err();
152        assert_eq!(err.sqlite_error_code(), Some(ErrorCode::DatabaseBusy));
153        assert!(CALLED.load(Ordering::Relaxed));
154        db2.busy_handler(None::<fn(i32) -> bool>)?;
155
156        db1.busy_handler(Some(busy_handler))?;
157        db1.busy_timeout(std::time::Duration::from_millis(5))?;
158        assert!(
159            unsafe { db1.get_clientdata::<std::ffi::c_void, _>(c"sqlite3_busy_handler")? }
160                .is_none()
161        );
162        Ok(())
163    }
164}