terl / lazysodium-java

A Java implementation of the Libsodium crypto library. For the lazy dev.
https://github.com/terl/lazysodium-java/wiki
Mozilla Public License 2.0
135 stars 47 forks source link

Make sure that the libsodium shared library can be used by more than 1 process #53

Closed gurpreet- closed 5 years ago

gurpreet- commented 5 years ago

The problem is that if 2 processes are using Lazysodium, one of them will fail because not more than one process can access the shared library.

Caused by: java.io.FileNotFoundException: C:\Users\mark\AppData\Local\Temp\lazysodium\libsodium.dll (The process cannot access the file because it is being used by another process)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at com.goterl.lazycode.lazysodium.utils.LibraryLoader.copyFromJarToTemp(LibraryLoader.java:244)
    at com.goterl.lazycode.lazysodium.utils.LibraryLoader.loadBundledLibrary(LibraryLoader.java:155)

I have recently changed lazysodium to load from the lazysodium folder that is not unique - this is highly likely where the error comes from. There are two ways we can solve this:

  1. If we can copy the Lazysodium shared library into a special unique temp folder, then this bug will be resolved.
  2. If we can make the Java loading code atomic across processes, this bug can be resolved.
gurpreet- commented 5 years ago

Resource loading out of a JAR is so terrible that I've created a dedicated library called Resource Loader to solve this problem and abstract it in such a way that everyone can use it.

gurpreet- commented 5 years ago

Changes now incorporated in release 4.1.0.