nagisa / rust_libloading

Bindings around the platform's dynamic library loading primitives with greatly improved memory safety.
https://docs.rs/libloading
ISC License
1.22k stars 100 forks source link

loading dylib from rust host program with libloading, recompiling dylib, reloading dylib does not see the new plugin version until the host fully restarts #108

Closed klebs6 closed 2 years ago

klebs6 commented 2 years ago

Greetings folks! I have been enjoying working with the library so far! I am encountering an unexpected behavioral error, though...

It is possible that there is some simple, obvious mistake in my setup and I will find it first thing in the morning!

I figured I would post here just in case there is somebody who might already know what to do.

I can add more detail after getting some sleep, but the basic gist is that I am writing a plugin (in rust) for a host (in rust), and am using the libloading library to handle the symbol loading etc at the interface.

Most everything seems to be working quite well -- the plugin loads on demand and the code executes as I expect it to.

However, when I need to make a change to the plugin and recompile it with cargo, reloading the dylib from the host does not seem to see the new changes. If I exit the host, relaunch the host, and load the plugin once more on demand, it picks up the new changes.

My first attempt at a solution was to manually call close on the libloading::Library after I am done using the plugin, before reloading it. That didn't seem to work, though it is possible I made a mistake.

I have not had a chance to inspect the situation more deeply, but am also not quite sure yet where to start...

I think getting some sleep might help! I am off to take care of that now! 😁😴 Thanks in advance for any help!

klebs6 commented 2 years ago

ah, looks to be the same/similar issue to #59 will try the solution there in the morning! closing for now!

nagisa commented 2 years ago

Yeah, most likely reason is that the old library remains loaded for one reason or another and trying to load a new one just increments a reference count for the old one rather than loading a new one.

This can happen for various reasons depending on the platform being targeted so unfortunately there isn’t really a nice straightforward cross-platform solution to this.