25
25
package com .oracle .mxtool .compilerserver ;
26
26
27
27
import java .io .BufferedReader ;
28
+ import java .io .IOException ;
28
29
import java .io .InputStreamReader ;
29
30
import java .io .OutputStreamWriter ;
30
31
import java .net .ServerSocket ;
@@ -102,10 +103,14 @@ public Thread newThread(Runnable runnable) {
102
103
if (running ) {
103
104
e .printStackTrace ();
104
105
} else {
105
- // Socket was closed
106
+ // we're shutting down
106
107
}
107
108
}
108
109
}
110
+ threadPool .shutdown ();
111
+ while (!threadPool .isTerminated ()) {
112
+ threadPool .awaitTermination (50 , TimeUnit .MILLISECONDS );
113
+ }
109
114
}
110
115
111
116
private static void usage () {
@@ -152,14 +157,7 @@ public void run() {
152
157
String requestOrigin = connectionSocket .getInetAddress ().getHostAddress ();
153
158
String prefix = String .format ("[%s:%s] " , Instant .now (), requestOrigin );
154
159
if (request == null || request .equals (REQUEST_HEADER_SHUTDOWN )) {
155
- logf ("%sShutting down%n" , prefix );
156
- running = false ;
157
- while (threadPool .getActiveCount () > 1 ) {
158
- threadPool .awaitTermination (50 , TimeUnit .MILLISECONDS );
159
- }
160
- serverSocket .close ();
161
- // Just to be sure...
162
- System .exit (0 );
160
+ shutdown (prefix );
163
161
} else if (request .startsWith (REQUEST_HEADER_COMPILE )) {
164
162
String commandLine = request .substring (REQUEST_HEADER_COMPILE .length ());
165
163
String [] args = commandLine .split ("\u0000 " );
@@ -177,8 +175,7 @@ public void run() {
177
175
int unrecognizedRequestCount = unrecognizedRequests .incrementAndGet ();
178
176
System .err .printf ("%sUnrecognized request %d (len=%d): \" %s\" %n" , prefix , unrecognizedRequestCount , request .length (), printable (request ));
179
177
if (unrecognizedRequestCount > MAX_UNRECOGNIZED_REQUESTS ) {
180
- System .err .printf ("%sShutting down after receiving %d unrecognized requests%n" , prefix , unrecognizedRequestCount );
181
- System .exit (0 );
178
+ shutdown (String .format ("%sReceived %d unrecognized requests: " , prefix , unrecognizedRequestCount ));
182
179
}
183
180
output .write ("-1\n " );
184
181
}
@@ -188,9 +185,22 @@ public void run() {
188
185
input .close ();
189
186
connectionSocket .close ();
190
187
}
188
+ } catch (SocketException se ) {
189
+ // Lost connection to mx
190
+ shutdown ("" );
191
191
} catch (Exception ioe ) {
192
192
ioe .printStackTrace ();
193
193
}
194
194
}
195
195
}
196
+
197
+ private void shutdown (String prefix ) {
198
+ logf ("%sShutting down%n" , prefix );
199
+ running = false ;
200
+ try {
201
+ serverSocket .close ();
202
+ } catch (IOException e ) {
203
+ e .printStackTrace ();
204
+ }
205
+ }
196
206
}
0 commit comments