shuiqiangchen commented on a change in pull request #12061: URL: https://github.com/apache/flink/pull/12061#discussion_r422764356
########## File path: flink-python/src/main/java/org/apache/flink/client/python/PythonEnvUtils.java ########## @@ -300,6 +294,48 @@ static GatewayServer startGatewayServer() throws ExecutionException, Interrupted return gatewayServerFuture.get(); } + /** + * Reset a daemon thread to the callback client thread pool so that the callback server can be terminated when gate + * way server is shutting down. We need to shut down the none-daemon thread firstly, then set a new thread created + * in a daemon thread to the ExecutorService. + * + * @param gatewayServer the gateway which creates the callback server. + * */ + private static void resetCallbackClientExecutorService(GatewayServer gatewayServer) throws NoSuchFieldException, + IllegalAccessException, NoSuchMethodException, InvocationTargetException { + CallbackClient callbackClient = (CallbackClient) gatewayServer.getCallbackClient(); + // The Java API of py4j does not provide approach to set "daemonize_connections" parameter. + // Use reflect to daemonize the connection thread. + Field executor = CallbackClient.class.getDeclaredField("executor"); + executor.setAccessible(true); + ((ScheduledExecutorService) executor.get(callbackClient)).shutdown(); + executor.set(callbackClient, Executors.newScheduledThreadPool(1, Thread::new)); + Method setupCleaner = CallbackClient.class.getDeclaredMethod("setupCleaner"); + setupCleaner.setAccessible(true); + setupCleaner.invoke(callbackClient); + } + + /** + * Reset the callback client of gatewayServer with the given callbackListeningAddress and callbackListeningPort + * after the callback server started. + * + * @param callbackListeningAddress the listening address of the callback server. + * @param callbackListeningPort the listening port of the callback server. + * */ + public static void resetCallbackClient(String callbackListeningAddress, int callbackListeningPort) throws + UnknownHostException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, + NoSuchFieldException { + + gatewayServer = getGatewayServer(); + if (gatewayServer == null){ Review comment: Yes, there is no need do this judgement I'll remove it. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org