fix(imessage): preserve sqlite conn across polling safely

This commit is contained in:
Chummy 2026-02-18 14:29:26 +08:00
parent 1ddcb0a573
commit 83b098d7ac

View file

@ -149,7 +149,7 @@ end tell"#
// Open a persistent read-only connection instead of creating // Open a persistent read-only connection instead of creating
// a new one on every 3-second poll cycle. // a new one on every 3-second poll cycle.
let path = db_path.to_path_buf(); let path = db_path.to_path_buf();
let mut conn = tokio::task::spawn_blocking(move || -> anyhow::Result<Connection> { let conn = tokio::task::spawn_blocking(move || -> anyhow::Result<Connection> {
Ok(Connection::open_with_flags( Ok(Connection::open_with_flags(
&path, &path,
OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX, OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
@ -158,23 +158,26 @@ end tell"#
.await??; .await??;
// Track the last ROWID we've seen (shuttle conn in and out) // Track the last ROWID we've seen (shuttle conn in and out)
let (c, initial_rowid) = let (mut conn, initial_rowid) =
tokio::task::spawn_blocking(move || -> anyhow::Result<(Connection, i64)> { tokio::task::spawn_blocking(move || -> anyhow::Result<(Connection, i64)> {
let rowid = {
let mut stmt = let mut stmt =
conn.prepare("SELECT MAX(ROWID) FROM message WHERE is_from_me = 0")?; conn.prepare("SELECT MAX(ROWID) FROM message WHERE is_from_me = 0")?;
let rowid: Option<i64> = stmt.query_row([], |row| row.get(0))?; let rowid: Option<i64> = stmt.query_row([], |row| row.get(0))?;
Ok((conn, rowid.unwrap_or(0))) rowid.unwrap_or(0)
};
Ok((conn, rowid))
}) })
.await??; .await??;
conn = c;
let mut last_rowid = initial_rowid; let mut last_rowid = initial_rowid;
loop { loop {
tokio::time::sleep(tokio::time::Duration::from_secs(self.poll_interval_secs)).await; tokio::time::sleep(tokio::time::Duration::from_secs(self.poll_interval_secs)).await;
let since = last_rowid; let since = last_rowid;
let poll_result = tokio::task::spawn_blocking( let (returned_conn, poll_result) = tokio::task::spawn_blocking(
move || -> anyhow::Result<(Connection, Vec<(i64, String, String)>)> { move || -> (Connection, anyhow::Result<Vec<(i64, String, String)>>) {
let result = (|| -> anyhow::Result<Vec<(i64, String, String)>> {
let mut stmt = conn.prepare( let mut stmt = conn.prepare(
"SELECT m.ROWID, h.id, m.text \ "SELECT m.ROWID, h.id, m.text \
FROM message m \ FROM message m \
@ -193,14 +196,18 @@ end tell"#
)) ))
})?; })?;
let results = rows.collect::<Result<Vec<_>, _>>()?; let results = rows.collect::<Result<Vec<_>, _>>()?;
Ok((conn, results)) Ok(results)
})();
(conn, result)
}, },
) )
.await?; .await
.map_err(|e| anyhow::anyhow!("iMessage poll worker join error: {e}"))?;
conn = returned_conn;
match poll_result { match poll_result {
Ok((c, messages)) => { Ok(messages) => {
conn = c;
for (rowid, sender, text) in messages { for (rowid, sender, text) in messages {
if rowid > last_rowid { if rowid > last_rowid {
last_rowid = rowid; last_rowid = rowid;