Description
Following example fails with error message: "Context already injected ..." (sorry for using SCIFIO to demonstrate a SciJava Bug):
public static void main(String[] args) {
final SCIFIO scifio = new SCIFIO();
final Runnable simpleRunnable = new Runnable() {
@Override
public void run() {
new ImgOpener(scifio.getContext());
}
};
new Thread(simpleRunnable).start();
new Thread(simpleRunnable).start();
}
I was able to track the problem down to the caching of the annotated Field
s of Plugin
s. The problem is, that the caching in e.g. https://github.com/scijava/scijava-common/blob/master/src/main/java/org/scijava/util/ClassUtils.java#L369 seems not to be thread safe. The problem that occurs with that is that the annotated Field
s are added twice to the cached list (and therefore the Context
tries to inject itself twice (see: https://github.com/scijava/scijava-common/blob/master/src/main/java/org/scijava/util/ClassUtils.java#L369) I think an easy fix would be to synchronize the accesses to the Cache in ClassUtils
. However, you may want to be careful with that and only synchronize the really critical parts of the method (synchronize may harm performance).
Does this make sense?