Skip to content

Commit fba63bd

Browse files
committed
fix library
1 parent 3825082 commit fba63bd

File tree

3 files changed

+19
-35
lines changed

3 files changed

+19
-35
lines changed

crates/vm/src/stdlib/ctypes.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -625,17 +625,12 @@ pub(crate) mod _ctypes {
625625

626626
#[cfg(not(windows))]
627627
#[pyfunction]
628-
fn dlclose(handle: usize, vm: &VirtualMachine) -> PyResult<()> {
629-
let result = unsafe { libc::dlclose(handle as *mut libc::c_void) };
630-
if result != 0 {
631-
let err = unsafe { libc::dlerror() };
632-
let msg = if err.is_null() {
633-
"dlclose() error".to_string()
634-
} else {
635-
unsafe { std::ffi::CStr::from_ptr(err).to_string_lossy().into_owned() }
636-
};
637-
return Err(vm.new_os_error(msg));
638-
}
628+
fn dlclose(handle: usize, _vm: &VirtualMachine) -> PyResult<()> {
629+
// Remove from cache, which triggers SharedLibrary drop.
630+
// libloading::Library calls dlclose automatically on Drop.
631+
let cache = library::libcache();
632+
let mut cache_write = cache.write();
633+
cache_write.drop_lib(handle);
639634
Ok(())
640635
}
641636

crates/vm/src/stdlib/ctypes/function.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,10 @@ impl ArgumentType for PyTypeRef {
316316
return Ok(FfiArgValue::Pointer(0));
317317
}
318318

319-
// For pointer types (POINTER(T)), we need to pass the ADDRESS of the value's buffer
319+
// For pointer types (POINTER(T)), we need to pass the pointer VALUE stored in buffer
320320
if self.fast_issubclass(PyCPointer::static_type()) {
321-
if let Some(cdata) = converted.downcast_ref::<PyCData>() {
322-
let addr = cdata.buffer.read().as_ptr() as usize;
323-
return Ok(FfiArgValue::Pointer(addr));
321+
if let Some(pointer) = converted.downcast_ref::<PyCPointer>() {
322+
return Ok(FfiArgValue::Pointer(pointer.get_ptr_value()));
324323
}
325324
return convert_to_pointer(&converted, vm);
326325
}

crates/vm/src/stdlib/ctypes/library.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,6 @@ impl SharedLibrary {
6464
let lib_lock = self.lib.lock();
6565
lib_lock.is_none()
6666
}
67-
68-
fn close(&self) {
69-
*self.lib.lock() = None;
70-
}
71-
}
72-
73-
impl Drop for SharedLibrary {
74-
fn drop(&mut self) {
75-
self.close();
76-
}
7767
}
7868

7969
pub(super) struct ExternalLibs {
@@ -119,17 +109,17 @@ impl ExternalLibs {
119109
let new_lib = SharedLibrary::new_with_mode(library_path, mode)?;
120110
let key = new_lib.get_pointer();
121111

122-
match self.libraries.get(&key) {
123-
Some(l) => {
124-
if l.is_closed() {
125-
self.libraries.insert(key, new_lib);
126-
}
127-
}
128-
_ => {
129-
self.libraries.insert(key, new_lib);
130-
}
131-
};
112+
// Check if library already exists and is not closed
113+
let should_use_cached = self.libraries.get(&key).is_some_and(|l| !l.is_closed());
132114

115+
if should_use_cached {
116+
// new_lib will be dropped, calling dlclose (decrements refcount)
117+
// But library stays loaded because cached version maintains refcount
118+
drop(new_lib);
119+
return Ok((key, self.libraries.get(&key).expect("just checked")));
120+
}
121+
122+
self.libraries.insert(key, new_lib);
133123
Ok((key, self.libraries.get(&key).expect("just inserted")))
134124
}
135125

0 commit comments

Comments
 (0)