Skip to content

Potential class loading deadlock when call static methods of Loader or Pointer #737

@tnatschl

Description

@tnatschl

We observed a problem when calling a utility function like Loader.isLoadLibraries (which calls Loader.load() when this is the first access) in one thread while running e.g. Pointer.maxBytes() in another thread:

// thread 2
public class PrintMaxBytes implements Runnable {
	@Override
	public void run() {
		long l = org.bytedeco.javacpp.Pointer.maxBytes();
		System.out.println(l);
	}
}
var t = new Thread(new PrintMaxBytes ());
t.start();

// thread 1(calls Loader.load())
Loader.isLoadLibraries();
  • let thread 2 start class loading / static init block in Pointer up until, BUT excluding Pointer line 521.
  • then let thread 1 run to execute the real Loader.load(); triggered by Loader.isLoadLibraries()
  • -> deadlock!

The reason seems to be that in the JNI code there is

jclass cls = (jclass)env->CallStaticObjectMethodA(JavaCPP_getClass(env, 0), putMemberOffsetMID, args);

which basically calls the static method Loader.putMemberOffset and by doing so triggers another call to Loader.load() for which the other thread already has a lock.

In our case we solved the issued by doing e.g. new Pointer(0) in an early part of the code where we are sure we still have only one thread.

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