@@ -557,13 +557,19 @@ private Env createInitialEnv() {
557557
558558 private CpsScript parseScript () throws IOException {
559559 // classloader hierarchy. See doc/classloader.md
560- trusted = new CpsGroovyShellFactory (this ).forTrusted ().build ();
561- shell = new CpsGroovyShellFactory (this ).withParent (trusted ).build ();
560+ CpsScript s ;
561+ try {
562+ trusted = new CpsGroovyShellFactory (this ).forTrusted ().build ();
563+ shell = new CpsGroovyShellFactory (this ).withParent (trusted ).build ();
562564
563- CpsScript s = (CpsScript ) shell .reparse ("WorkflowScript" ,script );
565+ s = (CpsScript ) shell .reparse ("WorkflowScript" ,script );
564566
565- for (Entry <String , String > e : loadedScripts .entrySet ()) {
566- shell .reparse (e .getKey (), e .getValue ());
567+ for (Entry <String , String > e : loadedScripts .entrySet ()) {
568+ shell .reparse (e .getKey (), e .getValue ());
569+ }
570+ } catch (RuntimeException | Error x ) {
571+ closeShells ();
572+ throw x ;
567573 }
568574
569575 s .execution = this ;
@@ -1304,20 +1310,26 @@ synchronized void onProgramEnd(Outcome outcome) {
13041310 this .persistedClean = Boolean .TRUE ;
13051311 }
13061312
1307- void cleanUpHeap () {
1308- LOGGER .log (Level .FINE , "cleanUpHeap on {0}" , owner );
1313+ private void closeShells () {
13091314 try {
13101315 if (shell != null ) {
1316+ LOGGER .fine (() -> "closing main class loader from " + owner );
13111317 shell .getClassLoader ().close ();
13121318 shell = null ;
13131319 }
13141320 if (trusted != null ) {
1321+ LOGGER .fine (() -> "closing trusted class loader from " + owner );
13151322 trusted .getClassLoader ().close ();
13161323 trusted = null ;
13171324 }
13181325 } catch (IOException x ) {
13191326 LOGGER .log (Level .WARNING , "failed to close class loaders from " + owner , x );
13201327 }
1328+ }
1329+
1330+ void cleanUpHeap () {
1331+ LOGGER .log (Level .FINE , "cleanUpHeap on {0}" , owner );
1332+ closeShells ();
13211333 if (scriptClass != null ) {
13221334 try {
13231335 cleanUpLoader (scriptClass .getClassLoader (), new HashSet <>(), new HashSet <>());
0 commit comments