/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.client.python;

import py4j.GatewayServer;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.nio.file.Files;

/**
 * The Py4j Gateway Server provides RPC service for user's python process.
 */
public class PythonGatewayServer {

	/**
	 * <p>
	 * Main method to start a local GatewayServer on a ephemeral port.
	 * It tells python side via a file.
	 *
	 * See: py4j.GatewayServer.main()
	 * </p>
	 */
	public static void main(String[] args) throws IOException {
		InetAddress localhost = InetAddress.getLoopbackAddress();
		GatewayServer gatewayServer = new GatewayServer.GatewayServerBuilder()
			.javaPort(0)
			.javaAddress(localhost)
			.build();
		gatewayServer.start();

		int boundPort = gatewayServer.getListeningPort();
		if (boundPort == -1) {
			System.out.println("GatewayServer failed to bind; exiting");
			System.exit(1);
		}

		// Tells python side the port of our java rpc server
		String handshakeFilePath = System.getenv("_PYFLINK_CONN_INFO_PATH");
		File handshakeFile = new File(handshakeFilePath);
		File tmpPath = Files.createTempFile(handshakeFile.getParentFile().toPath(),
			"connection", ".info").toFile();
		FileOutputStream fileOutputStream = new FileOutputStream(tmpPath);
		DataOutputStream stream = new DataOutputStream(fileOutputStream);
		stream.writeInt(boundPort);
		stream.close();
		fileOutputStream.close();

		if (!tmpPath.renameTo(handshakeFile)) {
			System.out.println("Unable to write connection information to handshake file: " + handshakeFilePath + ", now exit...");
			System.exit(1);
		}

		// Exit on EOF or broken pipe.  This ensures that the server dies
		// if its parent program dies.
		while (System.in.read() != -1) {
			// Do nothing
		}
		gatewayServer.shutdown();
	}
}

@@ -2464,24 +2464,33 @@
 it(1);%0A%09%09%7D%0A%0A
+%09%09try %7B%0A%09
 %09%09// Exit on
@@ -2547,16 +2547,17 @@
  dies%0A%09%09
+%09
 // if it
@@ -2579,16 +2579,17 @@
 m dies.%0A
+%09
 %09%09while 
@@ -2615,16 +2615,17 @@
 = -1) %7B%0A
+%09
 %09%09%09// Do
@@ -2635,18 +2635,20 @@
 thing%0A%09%09
+%09
 %7D%0A
+%09
 %09%09gatewa
@@ -2667,13 +2667,69 @@
 down();%0A
+%09%09%09System.exit(0);%0A%09%09%7D finally %7B%0A%09%09%09System.exit(1);%0A%09%09%7D%0A
 %09%7D%0A%7D%0A

Add System.exit() at the end of PythonGatewayServer to ensure the JVM will exit if its parent process dies.