@@ -61,10 +61,15 @@ protected void launchScript(EC2Computer computer, TaskListener listener)
6161
6262 logger .println ("Creating tmp directory if it does not exist" );
6363 WindowsProcess mkdirProcess = connection .execute ("if not exist " + tmpDir + " mkdir " + tmpDir );
64- int exitCode = mkdirProcess .waitFor ();
65- if (exitCode != 0 ) {
66- logger .println ("Creating tmpdir failed=" + exitCode );
67- return ;
64+ try {
65+ int exitCode = mkdirProcess .waitFor ();
66+ if (exitCode != 0 ) {
67+ logger .println ("Creating tmpdir failed=" + exitCode );
68+ return ;
69+ }
70+ } catch (Exception e ) {
71+ mkdirProcess .destroy ();
72+ throw e ;
6873 }
6974
7075 if (initScript != null && !initScript .trim ().isEmpty () && !connection .exists (tmpDir + ".jenkins-init" )) {
@@ -74,12 +79,17 @@ protected void launchScript(EC2Computer computer, TaskListener listener)
7479 }
7580
7681 WindowsProcess initProcess = connection .execute ("cmd /c " + tmpDir + "init.bat" );
77- IOUtils .copy (initProcess .getStdout (), logger );
82+ try {
83+ IOUtils .copy (initProcess .getStdout (), logger );
7884
79- int exitStatus = initProcess .waitFor ();
80- if (exitStatus != 0 ) {
81- logger .println ("init script failed: exit code=" + exitStatus );
82- return ;
85+ int exitStatus = initProcess .waitFor ();
86+ if (exitStatus != 0 ) {
87+ logger .println ("init script failed: exit code=" + exitStatus );
88+ return ;
89+ }
90+ } catch (Exception e ) {
91+ initProcess .destroy ();
92+ throw e ;
8393 }
8494
8595 try (OutputStream initGuard = connection .putFile (tmpDir + ".jenkins-init" )) {
@@ -102,13 +112,18 @@ protected void launchScript(EC2Computer computer, TaskListener listener)
102112 + AGENT_JAR + " -workDir " + workDir ;
103113 logger .println ("Launching via WinRM:" + launchString );
104114 final WindowsProcess process = connection .execute (launchString , 86400 );
105- computer .setChannel (process .getStdout (), process .getStdin (), logger , new Listener () {
106- @ Override
107- public void onClosed (Channel channel , IOException cause ) {
108- process .destroy ();
109- connection .close ();
110- }
111- });
115+ try {
116+ computer .setChannel (process .getStdout (), process .getStdin (), logger , new Listener () {
117+ @ Override
118+ public void onClosed (Channel channel , IOException cause ) {
119+ process .destroy ();
120+ connection .close ();
121+ }
122+ });
123+ } catch (Exception e ) {
124+ process .destroy ();
125+ throw e ;
126+ }
112127 } catch (EOFException eof ) {
113128 // When we launch java with connection.execute(launchString... it keeps running, but if java is not
114129 // installed
0 commit comments