liblinear-java icon indicating copy to clipboard operation
liblinear-java copied to clipboard

Thread safety problem in predict: flag_predict_probability shouldn't be static

Open Googulator opened this issue 4 years ago • 3 comments

The flag_predict_probability boolean in predict is declared static. This causes a problem when running 2 different predict jobs in the same Java process with different probability options: the setting from the second call will overwrite the one from the first call. This may cause a prediction using a non-probabilty-capable solver type to fail, despite being called with correct parameters, if a simultaneous job runs with probabilities enabled.

Googulator avatar Apr 12 '21 12:04 Googulator

Thanks for the bug report. I can see the thread-safety problem. It exists since the beginning and from my understanding also in predict.c of the original C++ implementation. Have you already reported an issue in https://github.com/cjlin1/liblinear?

This concurrency bug was probably not discovered so far because the more usual way to run predictions programmatically is to call Linear.predict(…) or Linear.predictProbability(…) which should both be thread-safe. So this would be the workaround until a fix is available in the next release.

bwaldvogel avatar Apr 14 '21 16:04 bwaldvogel

Unfortunately the workaround to directly call Linear.predict() isn't usable for us, as we're using an additional abstraction layer that can use either the separate liblinear-train / liblinear-predict native binaries (out-of-process), or liblinear-java if the native versions aren't available. Configuration for training and prediction is given in a config file in the form of a string representing the LibLinear command line, which needs to be parsed identically by both the Java and the native versions. The command line parsing function in liblinear-java is private, and even if it weren't, it uses the same static variable as main() (as well as calling System.exit(), which shoots down the caller on error, rather than throwing an exception or returning some invalid value).

This is not an issue in the C++ version, as that can never be loaded in-process as a library, but only executed as a separate process.

Googulator avatar Apr 14 '21 20:04 Googulator

This change is now released via version 2.44.

bwaldvogel avatar Mar 29 '22 06:03 bwaldvogel