ReLinker icon indicating copy to clipboard operation
ReLinker copied to clipboard

Loaded Libraries Cache is not thread-safe

Open georgi-neykov-hub opened this issue 6 years ago • 2 comments

The HashSet-based loadedLibraries field inReLinkerInstance holding the names of already loaded library names is not thread-safe or guarded with a mutex. Relinker.loadLibrary() can be called from multiple threads either by user code or by loading asynchronously via a LoadListener, so it is a good idea to either use a lock on the Set, or Collections.synchonizedSet() or better, just pick something appropiate from java.util.concurrent like a ConcurrentHashMap wrapped with Collections.newSetFromMap()

georgi-neykov-hub avatar Jan 02 '19 10:01 georgi-neykov-hub

Thanks for bringing this up. Do you ever run into the issue where you load it from multiple threads? For us, we load libraries on app start from one place.

It would be great if you'd open a PR for this. I think there is no downside in making it thread safe.

philippb avatar Feb 07 '19 18:02 philippb

I will do a fork and open a PR. There one more thing in my mind for which I am not sure to open an issue or just merge it with the current change request. Currently I am using the library though a singleton-like wrapper which does not need an Context argument, but uses a non-exported ContentProvider which gets a reference to the application context on launch of the app. This way I very much have a drop-in replacement for System.loadLibray(). Do you interest in such a change, I can open a PR for it as well?

georgi-neykov-hub avatar Feb 07 '19 20:02 georgi-neykov-hub