@@ -293,38 +293,30 @@ abstract class ForkedGrailsProcess {
293293 GrailsConsole . instance. updateStatus(" Running without daemon..." )
294294 }
295295
296- ServerSocket parentAvailabilityServer = startParentAvailabilityServer()
297-
298- try {
299- String classpathString = getBoostrapClasspath(executionContext)
300- List<String > cmd = buildProcessCommand(executionContext, classpathString)
296+ startParentAvailabilityServer()
301297
298+ String classpathString = getBoostrapClasspath(executionContext)
299+ List<String > cmd = buildProcessCommand(executionContext, classpathString)
302300
303- def processBuilder = new ProcessBuilder ()
304- processBuilder
305- .directory(executionContext. getBaseDir())
306- .redirectErrorStream(false )
307- .command(cmd)
308301
309- def process = processBuilder. start()
302+ def processBuilder = new ProcessBuilder ()
303+ processBuilder
304+ .directory(executionContext. getBaseDir())
305+ .redirectErrorStream(false )
306+ .command(cmd)
310307
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)
318- }
308+ def process = processBuilder. start()
319309
320- return attachOutputListener(process)
321- } finally {
322- try {
323- parentAvailabilityServer?. close()
324- } catch (e) {
325- // ignore
326- }
310+ if (isForkingReserveEnabled()) {
311+ List<String > reserveCmd = buildProcessCommand(executionContext, classpathString, true )
312+ forkReserveProcess(reserveCmd, executionContext)
327313 }
314+ else if (shouldRunWithDaemon()) {
315+ GrailsConsole . instance. updateStatus(" Starting daemon..." )
316+ forkDaemon(executionContext)
317+ }
318+
319+ return attachOutputListener(process)
328320 }
329321 else {
330322 return null
@@ -333,11 +325,19 @@ abstract class ForkedGrailsProcess {
333325 }
334326 }
335327
336- protected ServerSocket startParentAvailabilityServer () {
328+ protected void startParentAvailabilityServer () {
329+ if (System . getProperty(PARENT_PROCESS_PORT )) return
330+
337331 ServerSocket parentAvailabilityServer = new ServerSocket (0 )
338332 def parentPort = parentAvailabilityServer. localPort
339333 System . setProperty(PARENT_PROCESS_PORT , String . valueOf(parentPort))
340- parentAvailabilityServer
334+ Runtime . addShutdownHook {
335+ try {
336+ parentAvailabilityServer?. close()
337+ } catch (e) {
338+ // ignore
339+ }
340+ }
341341 }
342342
343343 @CompileStatic
@@ -394,6 +394,8 @@ abstract class ForkedGrailsProcess {
394394 discoverAndSetAgent(executionContext)
395395 }
396396
397+ startParentPortMonitor()
398+
397399 final console = GrailsConsole . instance
398400 console. updateStatus(" Stopping daemon..." )
399401 while (isDaemonRunning()) {
0 commit comments