@@ -294,40 +294,30 @@ abstract class ForkedGrailsProcess {
294294 GrailsConsole . instance. updateStatus(" Running without daemon..." )
295295 }
296296
297- ServerSocket parentAvailabilityServer = new ServerSocket (0 )
298- def parentPort = parentAvailabilityServer. localPort
299- System . setProperty(PARENT_PROCESS_PORT , String . valueOf(parentPort))
297+ startParentAvailabilityServer()
300298
301- try {
302- String classpathString = getBoostrapClasspath(executionContext)
303- List<String > cmd = buildProcessCommand(executionContext, classpathString)
299+ String classpathString = getBoostrapClasspath(executionContext)
300+ List<String > cmd = buildProcessCommand(executionContext, classpathString)
304301
305302
306- def processBuilder = new ProcessBuilder ()
307- processBuilder
308- .directory(executionContext. getBaseDir())
309- .redirectErrorStream(false )
310- .command(cmd)
303+ def processBuilder = new ProcessBuilder ()
304+ processBuilder
305+ .directory(executionContext. getBaseDir())
306+ .redirectErrorStream(false )
307+ .command(cmd)
311308
312- def process = processBuilder. start()
309+ def process = processBuilder. start()
313310
314- if (isForkingReserveEnabled()) {
315- List<String > reserveCmd = buildProcessCommand(executionContext, classpathString, true )
316- forkReserveProcess(reserveCmd, executionContext)
317- }
318- else if (shouldRunWithDaemon()) {
319- GrailsConsole . instance. updateStatus(" Starting daemon..." )
320- forkDaemon(executionContext)
321- }
322-
323- return attachOutputListener(process)
324- } finally {
325- try {
326- parentAvailabilityServer?. close()
327- } catch (e) {
328- // ignore
329- }
311+ if (isForkingReserveEnabled()) {
312+ List<String > reserveCmd = buildProcessCommand(executionContext, classpathString, true )
313+ forkReserveProcess(reserveCmd, executionContext)
314+ }
315+ else if (shouldRunWithDaemon()) {
316+ GrailsConsole . instance. updateStatus(" Starting daemon..." )
317+ forkDaemon(executionContext)
330318 }
319+
320+ return attachOutputListener(process)
331321 }
332322 else {
333323 return null
@@ -336,6 +326,21 @@ abstract class ForkedGrailsProcess {
336326 }
337327 }
338328
329+ protected void startParentAvailabilityServer () {
330+ if (System . getProperty(PARENT_PROCESS_PORT )) return
331+
332+ ServerSocket parentAvailabilityServer = new ServerSocket (0 )
333+ def parentPort = parentAvailabilityServer. localPort
334+ System . setProperty(PARENT_PROCESS_PORT , String . valueOf(parentPort))
335+ Runtime . addShutdownHook {
336+ try {
337+ parentAvailabilityServer?. close()
338+ } catch (e) {
339+ // ignore
340+ }
341+ }
342+ }
343+
339344 @CompileStatic
340345 protected void runDaemonCommand (String daemonCmd ) {
341346 def clientSocket = new Socket (" localhost" , daemonPort)
@@ -390,6 +395,8 @@ abstract class ForkedGrailsProcess {
390395 discoverAndSetAgent(executionContext)
391396 }
392397
398+ startParentPortMonitor()
399+
393400 final console = GrailsConsole . instance
394401 console. updateStatus(" Stopping daemon..." )
395402 while (isDaemonRunning()) {
@@ -518,7 +525,11 @@ abstract class ForkedGrailsProcess {
518525 if (! (System . getProperty(" java.version" ) =~ / 1.[89]./ )) {
519526 cmd. add(" -XX:MaxPermSize=${ maxPerm} m" . toString())
520527 }
521- cmd << " -D${ PARENT_PROCESS_PORT} =${ System.getProperty(PARENT_PROCESS_PORT)} " . toString()
528+ def parentPort = System . getProperty(PARENT_PROCESS_PORT )
529+ if (parentPort) {
530+ cmd << " -D${ PARENT_PROCESS_PORT} =${ parentPort} " . toString()
531+ }
532+
522533 cmd. addAll([" -Dgrails.fork.active=true" ,
523534 " -Dgrails.build.execution.context=${ tempFile.canonicalPath} " . toString(), " -cp" , classpathString])
524535
0 commit comments