You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2019/05/24 15:40:22 UTC

[GitHub] [flink] tillrohrmann commented on a change in pull request #8154: [Flink-12167] Reset context classloader in run and getOptimizedPlan methods

tillrohrmann commented on a change in pull request #8154: [Flink-12167] Reset context classloader in run and getOptimizedPlan methods
URL: https://github.com/apache/flink/pull/8154#discussion_r287414860
 
 

 ##########
 File path: flink-clients/src/main/java/org/apache/flink/client/program/ClusterClient.java
 ##########
 @@ -246,45 +253,51 @@ public static OptimizedPlan getOptimizedPlan(Optimizer compiler, Plan p, int par
 	 * @throws ProgramInvocationException
 	 */
 	public JobSubmissionResult run(PackagedProgram prog, int parallelism)
-			throws ProgramInvocationException, ProgramMissingJobException {
-		Thread.currentThread().setContextClassLoader(prog.getUserCodeClassLoader());
-		if (prog.isUsingProgramEntryPoint()) {
-
-			final JobWithJars jobWithJars = prog.getPlanWithJars();
-
-			return run(jobWithJars, parallelism, prog.getSavepointSettings());
-		}
-		else if (prog.isUsingInteractiveMode()) {
-			log.info("Starting program in interactive mode (detached: {})", isDetached());
-
-			final List<URL> libraries = prog.getAllLibraries();
-
-			ContextEnvironmentFactory factory = new ContextEnvironmentFactory(this, libraries,
-					prog.getClasspaths(), prog.getUserCodeClassLoader(), parallelism, isDetached(),
-					prog.getSavepointSettings());
-			ContextEnvironment.setAsContext(factory);
-
-			try {
-				// invoke main method
-				prog.invokeInteractiveModeForExecution();
-				if (lastJobExecutionResult == null && factory.getLastEnvCreated() == null) {
-					throw new ProgramMissingJobException("The program didn't contain a Flink job.");
-				}
-				if (isDetached()) {
-					// in detached mode, we execute the whole user code to extract the Flink job, afterwards we run it here
-					return ((DetachedEnvironment) factory.getLastEnvCreated()).finalizeExecute();
+		throws ProgramInvocationException, ProgramMissingJobException {
+		ClassLoader contextCl = Thread.currentThread().getContextClassLoader();
+		try {
+			Thread.currentThread().setContextClassLoader(prog.getUserCodeClassLoader());
+			if (prog.isUsingProgramEntryPoint()) {
+				final JobWithJars jobWithJars = prog.getPlanWithJars();
+				return run(jobWithJars, parallelism, prog.getSavepointSettings());
+			}
+			else if (prog.isUsingInteractiveMode()) {
+				log.info("Starting program in interactive mode (detached: {})", isDetached());
+
+				final List<URL> libraries = prog.getAllLibraries();
+
+				ContextEnvironmentFactory factory = new ContextEnvironmentFactory(this, libraries,
+				prog.getClasspaths(), prog.getUserCodeClassLoader(), parallelism, isDetached(),
+				prog.getSavepointSettings());
+				ContextEnvironment.setAsContext(factory);
+
+				try {
+					// invoke main method
+					prog.invokeInteractiveModeForExecution();
+					if (lastJobExecutionResult == null && factory.getLastEnvCreated() == null) {
+						throw new ProgramMissingJobException("The program didn't contain a Flink job.");
+					}
+					if (isDetached()) {
+						// in detached mode, we execute the whole user code to extract the Flink job, afterwards we run it here
+						return ((DetachedEnvironment) factory.getLastEnvCreated()).finalizeExecute();
+					}
+					else {
+						// in blocking mode, we execute all Flink jobs contained in the user code and then return here
+						return this.lastJobExecutionResult;
+					}
 				}
-				else {
-					// in blocking mode, we execute all Flink jobs contained in the user code and then return here
-					return this.lastJobExecutionResult;
+				finally {
+				ContextEnvironment.unsetContext();
 				}
 			}
-			finally {
-				ContextEnvironment.unsetContext();
+			else {
+				throw new ProgramInvocationException("PackagedProgram does not have a valid invocation mode.");
 			}
 		}
-		else {
-			throw new ProgramInvocationException("PackagedProgram does not have a valid invocation mode.");
+		finally {
+			if (contextCl != null) {
 
 Review comment:
   I think we don't need the null check here.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services