@@ -7,6 +7,7 @@ import caseapp.core.help.HelpFormat
7
7
import java .io .File
8
8
import java .util .Locale
9
9
import java .util .concurrent .CompletableFuture
10
+ import java .util .concurrent .atomic .AtomicReference
10
11
11
12
import scala .build .EitherCps .{either , value }
12
13
import scala .build .*
@@ -229,18 +230,18 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
229
230
/** A handle to the Runner process, used to kill the process if it's still alive when a change
230
231
* occured and restarts are allowed or to wait for it if restarts are not allowed
231
232
*/
232
- var processOpt = Option .empty[(Process , CompletableFuture [_])]
233
+ val processOpt = AtomicReference ( Option .empty[(Process , CompletableFuture [_])])
233
234
234
235
/** shouldReadInput controls whether [[WatchUtil.waitForCtrlC ]](that's keeping the main thread
235
236
* alive) should try to read StdIn or just call wait()
236
237
*/
237
- var shouldReadInput = false
238
+ val shouldReadInput = AtomicReference ( false )
238
239
239
- /** a handle to the main thread to interrupt its operations when:
240
+ /** A handle to the main thread to interrupt its operations when:
240
241
* - it's blocked on reading StdIn, and it's no longer required
241
242
* - it's waiting and should start reading StdIn
242
243
*/
243
- var mainThreadOpt = Option .empty[Thread ]
244
+ val mainThreadOpt = AtomicReference ( Option .empty[Thread ])
244
245
245
246
val watcher = Build .watch(
246
247
inputs,
@@ -253,22 +254,22 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
253
254
partial = None ,
254
255
actionableDiagnostics = actionableDiagnostics,
255
256
postAction = () =>
256
- if (processOpt.exists(_._1.isAlive()))
257
+ if (processOpt.get(). exists(_._1.isAlive()))
257
258
WatchUtil .printWatchWhileRunningMessage()
258
259
else
259
260
WatchUtil .printWatchMessage()
260
261
) { res =>
261
- for ((process, onExitProcess) <- processOpt) {
262
+ for ((process, onExitProcess) <- processOpt.get() ) {
262
263
onExitProcess.cancel(true )
263
264
ProcUtil .interruptProcess(process, logger)
264
265
}
265
266
res.orReport(logger).map(_.main).foreach {
266
267
case s : Build .Successful =>
267
- for ((proc, _) <- processOpt if proc.isAlive)
268
+ for ((proc, _) <- processOpt.get() if proc.isAlive)
268
269
// If the process doesn't exit, send SIGKILL
269
270
ProcUtil .forceKillProcess(proc, logger)
270
- shouldReadInput = false
271
- mainThreadOpt.foreach(_.interrupt())
271
+ shouldReadInput.set( false )
272
+ mainThreadOpt.get(). foreach(_.interrupt())
272
273
val maybeProcess = maybeRun(
273
274
s,
274
275
allowTerminate = false ,
@@ -282,29 +283,36 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
282
283
case (proc, onExit) =>
283
284
if (options.sharedRun.watch.restart)
284
285
onExit.thenApply { _ =>
285
- shouldReadInput = true
286
- mainThreadOpt.foreach(_.interrupt())
286
+ shouldReadInput.set( true )
287
+ mainThreadOpt.get(). foreach(_.interrupt())
287
288
}
288
289
(proc, onExit)
289
290
}
290
291
s.copyOutput(options.shared)
291
292
if (options.sharedRun.watch.restart)
292
- processOpt = maybeProcess
293
+ processOpt.set( maybeProcess)
293
294
else {
294
295
for ((proc, onExit) <- maybeProcess)
295
296
ProcUtil .waitForProcess(proc, onExit)
296
- shouldReadInput = true
297
- mainThreadOpt.foreach(_.interrupt())
297
+ shouldReadInput.set( true )
298
+ mainThreadOpt.get(). foreach(_.interrupt())
298
299
}
299
300
case _ : Build .Failed =>
300
301
System .err.println(" Compilation failed" )
301
302
}
302
303
}
303
- mainThreadOpt = Some (Thread .currentThread())
304
-
305
- try WatchUtil .waitForCtrlC(() => watcher.schedule(), () => shouldReadInput)
304
+ mainThreadOpt.set(Some (Thread .currentThread()))
305
+
306
+ try
307
+ WatchUtil .waitForCtrlC(
308
+ { () =>
309
+ watcher.schedule()
310
+ shouldReadInput.set(false )
311
+ },
312
+ () => shouldReadInput.get()
313
+ )
306
314
finally {
307
- mainThreadOpt = None
315
+ mainThreadOpt.set( None )
308
316
watcher.dispose()
309
317
}
310
318
}
0 commit comments