Skip to content

Commit 0043f38

Browse files
committed
fix: release the native UDF cache's read guard before taking the write lock
`get_or_load` looked up the canonical path in an `if let` scrutinee and took the write lock inside its body to record the new path. The scrutinee's read guard lives until the end of that block, so a first lookup through a path that resolves to an already-loaded library, such as a symlink, waited on its own read lock forever. Two threads racing on a library's first load through a non-canonical path could reach the same branch. The lookup is now a statement of its own. The new test looks a loaded library up through a symlink on a separate thread, so a regression fails it after 30 seconds instead of hanging the run.
1 parent ba22ecd commit 0043f38

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

‎native/core/src/execution/c_udf/cache.rs‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ pub fn get_or_load(path: impl AsRef<Path>) -> Result<Arc<LoadedLibrary>, LoaderE
4545

4646
let canonical = raw.canonicalize().unwrap_or_else(|_| raw.clone());
4747
if canonical != raw {
48-
if let Some(lib) = cache().read().unwrap().get(&canonical).cloned() {
48+
// A statement of its own, so the read guard is dropped before the write lock is taken. In
49+
// an `if let` scrutinee it would live until the end of the block, and this thread would
50+
// wait on its own read lock forever.
51+
let hit = cache().read().unwrap().get(&canonical).cloned();
52+
if let Some(lib) = hit {
4953
cache().write().unwrap().insert(raw, Arc::clone(&lib));
5054
return Ok(lib);
5155
}
@@ -87,4 +91,29 @@ mod tests {
8791
let err = get_or_load("/no/such/file.dylib").unwrap_err();
8892
assert!(matches!(err, LoaderError::Open { .. }));
8993
}
94+
95+
/// A path seen for the first time that resolves to an already-loaded library, here a symlink,
96+
/// takes the branch that records the new path under the write lock. That branch used to take
97+
/// the lock while still holding its own read guard, and hung. The lookup runs on a thread of
98+
/// its own so a regression fails this test rather than hanging the run.
99+
#[cfg(unix)]
100+
#[test]
101+
fn new_path_to_a_loaded_library_returns_the_same_arc() {
102+
let canonical = test_udfs_path().canonicalize().expect(BUILD_HINT);
103+
let loaded = get_or_load(&canonical).expect(BUILD_HINT);
104+
105+
let dir = tempfile::tempdir().expect("tempdir");
106+
let alias = dir.path().join(canonical.file_name().expect("file name"));
107+
std::os::unix::fs::symlink(&canonical, &alias).expect("symlink");
108+
109+
let (tx, rx) = std::sync::mpsc::channel();
110+
std::thread::spawn(move || {
111+
let _ = tx.send(get_or_load(&alias));
112+
});
113+
let via_alias = rx
114+
.recv_timeout(std::time::Duration::from_secs(30))
115+
.expect("looking the library up through a symlink hung")
116+
.expect(BUILD_HINT);
117+
assert!(Arc::ptr_eq(&loaded, &via_alias));
118+
}
90119
}

0 commit comments

Comments
 (0)