py4j / py4j

Py4J enables Python programs to dynamically access arbitrary Java objects

Home Page:https://www.py4j.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stuck when calling System.load on Windows

Fanoid opened this issue · comments

Hi, I found the program will stuck when calling System.load with Py4J on Windows.

After simplified, a code snippet for reproduction is as follows:

from py4j.java_gateway import launch_gateway, JavaGateway, GatewayParameters
port = launch_gateway(die_on_exit=True)
gateway = JavaGateway(gateway_parameters=GatewayParameters(port=port))
system_cls = gateway.jvm.System
system_cls.load("D:\\netlib-native_ref-win-i686-1.1.dll")

I use Py4J of version 0.10.8.1, and the dll file can be downloaded from here.

When I went deeper, I found die_on_exit is a crucial parameter. When die_on_exit is set with False, System.load won't stuck, but JavaGateway won't exit.

The possible causes are then narrowed down to the following lines in GatewayServer.java. But I don't understand how they relate.

if (dieOnBrokenPipe) {
	/* Exit on EOF or broken pipe.  This ensures that the server dies
		* if its parent program dies. */
	try {
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, Charset.forName("UTF-8")));
		stdin.readLine();
		System.exit(0);
	} catch (java.io.IOException e) {
		System.exit(1);
	}
}

Does anyone has an idea?
I googled and found some similar cases:

https://stackoverflow.com/questions/5262552/loadlibrary-freezes
https://forums.codeguru.com/showthread.php?244147-LoadLibrary-blocking

It seems reading blocking from stdin in another thread makes loadLibrary hang.