Panic on Option unwrap in get_proc_ptr
vthib opened this issue · 0 comments
vthib commented
Since the 0.4 update, we sometime (rarely, but happens from time to time) this panic:
panicked at 'called `Option::unwrap()` on a `None` value', /home/vthib/.cargo/registry/src/github.com-1ecc6299db9ec8
23/windows-dll-0.4.0/src/cache.rs:76:45
Reading the code, I can see a race condition which can make this happen. From what I can tell from the code:
DllCache::get
gets the dll handle, and if not loaded yet, loads it (DllCache::load_and_cache_lib
)DllCache::load_and_cache_lib
loads the dll handle, stores it, then initializes theprocs
once_cellDllCache::get_proc_ptr
callsDllCache::get
, then usesprocs
, which should have been initialized
This is racy:
- Thread 1 calls
get_proc_ptr
, which entersget
thenload_and_cache_lib
, and stores the handle - Thread 2 calls
get_proc_ptr
, which entersget
, which returns the loaded handle stored by thread 1. Thread 2 proceeds with accessingprocs
, which is not yet initialized by thread 1
One solution should be to only store the handle after the initialization of the procs once_cell in load_and_cache_lib
.