Skip to content

ClassUtils: Caching of annotated Fields is not thread safe #148

Open
@dietzc

Description

@dietzc

@hinerm @ctrueden

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 Fields of Plugins. 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 Fields 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions