rksm / hot-lib-reloader-rs

Reload Rust code without app restarts. For faster feedback cycles.
MIT License
577 stars 21 forks source link

Q: Is copying the library required on *nix? #26

Open Dietr1ch opened 1 year ago

Dietr1ch commented 1 year ago

On *nix you can overwrite a file that's being read by another process.

https://stackoverflow.com/questions/2028874/what-happens-to-an-open-file-handle-on-linux-if-the-pointed-file-gets-moved-or-d

This should mean that the compiler can output a new dynamic library without hitting errors because there's programs still reading (soon to be older versions of) it. When Casey presents this copy-based workaround, he's just working around the Windows FS guarantees.

Can the copy become Windows-specific?

rksm commented 1 year ago

Hmm good question. So on macOS at least there is this problem:

Basically, unloading a library does not work and in order to get the changes we need to load from a new file.

On Linux it might be worth a try...

Imberflur commented 1 year ago

I'm fairly certain Linux has similar situations where the library isn't unloaded and this also prevents loading a new version with the same name.

E.g. when there are thread local destructors that haven't ran (due to the associated thread still being alive). See https://fasterthanli.me/articles/so-you-want-to-live-reload-rust.

It is also possible to overwrite the loaded library file in the sense of actually writing to the existing file and (i.e. not deleting it and creating a new one with the same name), however I have seen this cause issues/crashes (I assume due to the old thread locals still being loaded and/or other state associated with the loaded library that doesn't expect to be used with a new version of the code).