BlueGit

ict rusty-teams

chore(store): record slow store lock waits and holds in the diagnostics log

- Wait over 50 ms or hold over 100 ms, with the store call site and thread
- Reported through diagnostics::record, so it lands without the debug log setting

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-16 · 2 weeks ago

Hash
e63bb4e0dd4427945c04da5868f474c30beed986
Parent
ea2c96e7
3 files changed +79 −4
crates/app/src/main.rs +1 −0
@@ -202,6 +202,7 @@ fn main() {
202 202 .unwrap_or_else(|| data_path(diagnostics::DIAGNOSTICS_FILE)),
203 203 );
204 204 let _ = diagnostics::enable_from_override();
205 store::report_slow_locks_to(|message| diagnostics::record(message));
205 206 #[cfg(target_os = "linux")]
206 207 app_base::linux_fonts::hide_system_color_emoji();
207 208 let arguments = parse_arguments();
@@ -202,6 +202,7 @@ fn main() {
202 .unwrap_or_else(|| data_path(diagnostics::DIAGNOSTICS_FILE)),
202 .unwrap_or_else(|| data_path(diagnostics::DIAGNOSTICS_FILE)),
203 );
203 );
204 let _ = diagnostics::enable_from_override();
204 let _ = diagnostics::enable_from_override();
205 store::report_slow_locks_to(|message| diagnostics::record(message));
205 #[cfg(target_os = "linux")]
206 #[cfg(target_os = "linux")]
206 app_base::linux_fonts::hide_system_color_emoji();
207 app_base::linux_fonts::hide_system_color_emoji();
207 let arguments = parse_arguments();
208 let arguments = parse_arguments();
crates/store/src/lib.rs +1 −1
@@ -42,5 +42,5 @@ pub use models::{
42 42 SyncState, TeamLayoutRecord, TeamRecord, TeamRights, UNKNOWN_USER_NAME, is_bot_id,
43 43 };
44 44 pub use search::{HIGHLIGHT_END, HIGHLIGHT_START};
45 pub use store::{DATA_DIR_NAME, Store, default_database_path};
45 pub use store::{DATA_DIR_NAME, Store, default_database_path, report_slow_locks_to};
46 46 pub use text::plain_text;
@@ -42,5 +42,5 @@ pub use models::{
42 SyncState, TeamLayoutRecord, TeamRecord, TeamRights, UNKNOWN_USER_NAME, is_bot_id,
42 SyncState, TeamLayoutRecord, TeamRecord, TeamRights, UNKNOWN_USER_NAME, is_bot_id,
43};
43};
44pub use search::{HIGHLIGHT_END, HIGHLIGHT_START};
44pub use search::{HIGHLIGHT_END, HIGHLIGHT_START};
45pub use store::{DATA_DIR_NAME, Store, default_database_path};
45pub use store::{DATA_DIR_NAME, Store, default_database_path, report_slow_locks_to};
46pub use text::plain_text;
46pub use text::plain_text;
crates/store/src/store.rs +77 −3
@@ -1,5 +1,8 @@
1 use std::ops::{Deref, DerefMut};
2 use std::panic::Location;
1 3 use std::path::{Path, PathBuf};
2 use std::sync::{Mutex, MutexGuard};
4 use std::sync::{Mutex, MutexGuard, OnceLock};
5 use std::time::{Duration, Instant};
3 6
4 7 use directories::ProjectDirs;
5 8 use rusqlite::Connection;
@@ -9,6 +12,20 @@ use crate::migrations;
9 12
10 13 pub const DATA_DIR_NAME: &str = "ms-teams-linux";
11 14 const DATABASE_FILE: &str = "cache.sqlite3";
15 const SLOW_WAIT: Duration = Duration::from_millis(50);
16 const SLOW_HOLD: Duration = Duration::from_millis(100);
17
18 static SLOW_LOCK_REPORTER: OnceLock<fn(String)> = OnceLock::new();
19
20 pub fn report_slow_locks_to(reporter: fn(String)) {
21 let _ = SLOW_LOCK_REPORTER.set(reporter);
22 }
23
24 fn report_slow_lock(message: String) {
25 if let Some(reporter) = SLOW_LOCK_REPORTER.get() {
26 reporter(message);
27 }
28 }
12 29
13 30 pub fn default_database_path() -> Result<PathBuf> {
14 31 let directories = ProjectDirs::from("", "", DATA_DIR_NAME).ok_or(Error::NoDataDirectory)?;
@@ -58,11 +75,68 @@ impl Store {
58 75 .pragma_query_value(None, "user_version", |row| row.get(0))?)
59 76 }
60 77
61 pub(crate) fn lock(&self) -> Result<MutexGuard<'_, Connection>> {
62 self.connection.lock().map_err(|_| Error::Poisoned)
78 #[track_caller]
79 pub(crate) fn lock(&self) -> Result<StoreGuard<'_>> {
80 let location = Location::caller();
81 let asked = Instant::now();
82 let connection = self.connection.lock().map_err(|_| Error::Poisoned)?;
83 let waited = asked.elapsed();
84 if waited >= SLOW_WAIT {
85 report_slow_lock(format!(
86 "store lock waited {} ms at {location} on {}",
87 waited.as_millis(),
88 thread_label()
89 ));
90 }
91 Ok(StoreGuard {
92 connection,
93 location,
94 taken: Instant::now(),
95 })
63 96 }
64 97 }
65 98
99 pub(crate) struct StoreGuard<'a> {
100 connection: MutexGuard<'a, Connection>,
101 location: &'static Location<'static>,
102 taken: Instant,
103 }
104
105 impl Deref for StoreGuard<'_> {
106 type Target = Connection;
107
108 fn deref(&self) -> &Connection {
109 &self.connection
110 }
111 }
112
113 impl DerefMut for StoreGuard<'_> {
114 fn deref_mut(&mut self) -> &mut Connection {
115 &mut self.connection
116 }
117 }
118
119 impl Drop for StoreGuard<'_> {
120 fn drop(&mut self) {
121 let held = self.taken.elapsed();
122 if held >= SLOW_HOLD {
123 report_slow_lock(format!(
124 "store lock held {} ms at {} on {}",
125 held.as_millis(),
126 self.location,
127 thread_label()
128 ));
129 }
130 }
131 }
132
133 fn thread_label() -> String {
134 let thread = std::thread::current();
135 thread
136 .name()
137 .map_or_else(|| format!("{:?}", thread.id()), str::to_owned)
138 }
139
66 140 pub(crate) fn json_ids(ids: &[String]) -> String {
67 141 serde_json::to_string(ids).unwrap_or_else(|_| "[]".to_owned())
68 142 }
@@ -1,5 +1,8 @@
1use std::ops::{Deref, DerefMut};
2use std::panic::Location;
1use std::path::{Path, PathBuf};
3use std::path::{Path, PathBuf};
2use std::sync::{Mutex, MutexGuard};
4use std::sync::{Mutex, MutexGuard, OnceLock};
5use std::time::{Duration, Instant};
3
6
4use directories::ProjectDirs;
7use directories::ProjectDirs;
5use rusqlite::Connection;
8use rusqlite::Connection;
@@ -9,6 +12,20 @@ use crate::migrations;
9
12
10pub const DATA_DIR_NAME: &str = "ms-teams-linux";
13pub const DATA_DIR_NAME: &str = "ms-teams-linux";
11const DATABASE_FILE: &str = "cache.sqlite3";
14const DATABASE_FILE: &str = "cache.sqlite3";
15const SLOW_WAIT: Duration = Duration::from_millis(50);
16const SLOW_HOLD: Duration = Duration::from_millis(100);
17
18static SLOW_LOCK_REPORTER: OnceLock<fn(String)> = OnceLock::new();
19
20pub fn report_slow_locks_to(reporter: fn(String)) {
21 let _ = SLOW_LOCK_REPORTER.set(reporter);
22}
23
24fn report_slow_lock(message: String) {
25 if let Some(reporter) = SLOW_LOCK_REPORTER.get() {
26 reporter(message);
27 }
28}
12
29
13pub fn default_database_path() -> Result<PathBuf> {
30pub fn default_database_path() -> Result<PathBuf> {
14 let directories = ProjectDirs::from("", "", DATA_DIR_NAME).ok_or(Error::NoDataDirectory)?;
31 let directories = ProjectDirs::from("", "", DATA_DIR_NAME).ok_or(Error::NoDataDirectory)?;
@@ -58,11 +75,68 @@ impl Store {
58 .pragma_query_value(None, "user_version", |row| row.get(0))?)
75 .pragma_query_value(None, "user_version", |row| row.get(0))?)
59 }
76 }
60
77
61 pub(crate) fn lock(&self) -> Result<MutexGuard<'_, Connection>> {
78 #[track_caller]
62 self.connection.lock().map_err(|_| Error::Poisoned)
79 pub(crate) fn lock(&self) -> Result<StoreGuard<'_>> {
80 let location = Location::caller();
81 let asked = Instant::now();
82 let connection = self.connection.lock().map_err(|_| Error::Poisoned)?;
83 let waited = asked.elapsed();
84 if waited >= SLOW_WAIT {
85 report_slow_lock(format!(
86 "store lock waited {} ms at {location} on {}",
87 waited.as_millis(),
88 thread_label()
89 ));
90 }
91 Ok(StoreGuard {
92 connection,
93 location,
94 taken: Instant::now(),
95 })
63 }
96 }
64}
97}
65
98
99pub(crate) struct StoreGuard<'a> {
100 connection: MutexGuard<'a, Connection>,
101 location: &'static Location<'static>,
102 taken: Instant,
103}
104
105impl Deref for StoreGuard<'_> {
106 type Target = Connection;
107
108 fn deref(&self) -> &Connection {
109 &self.connection
110 }
111}
112
113impl DerefMut for StoreGuard<'_> {
114 fn deref_mut(&mut self) -> &mut Connection {
115 &mut self.connection
116 }
117}
118
119impl Drop for StoreGuard<'_> {
120 fn drop(&mut self) {
121 let held = self.taken.elapsed();
122 if held >= SLOW_HOLD {
123 report_slow_lock(format!(
124 "store lock held {} ms at {} on {}",
125 held.as_millis(),
126 self.location,
127 thread_label()
128 ));
129 }
130 }
131}
132
133fn thread_label() -> String {
134 let thread = std::thread::current();
135 thread
136 .name()
137 .map_or_else(|| format!("{:?}", thread.id()), str::to_owned)
138}
139
66pub(crate) fn json_ids(ids: &[String]) -> String {
140pub(crate) fn json_ids(ids: &[String]) -> String {
67 serde_json::to_string(ids).unwrap_or_else(|_| "[]".to_owned())
141 serde_json::to_string(ids).unwrap_or_else(|_| "[]".to_owned())
68}
142}