Skip to content

Commit 646949d

Browse files
authored
fix #480 (#484)
1 parent 3c09ad7 commit 646949d

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

runner/engine.go

-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ type Engine struct {
2727
watcherStopCh chan bool
2828
buildRunCh chan bool
2929
buildRunStopCh chan bool
30-
canExit chan bool
3130
binStopCh chan bool
3231
exitCh chan bool
3332

@@ -56,7 +55,6 @@ func NewEngineWithConfig(cfg *Config, debugMode bool) (*Engine, error) {
5655
watcherStopCh: make(chan bool, 10),
5756
buildRunCh: make(chan bool, 1),
5857
buildRunStopCh: make(chan bool, 1),
59-
canExit: make(chan bool, 1),
6058
binStopCh: make(chan bool),
6159
exitCh: make(chan bool),
6260
fileChecksums: &checksumMap{m: make(map[string]string)},
@@ -374,19 +372,16 @@ func (e *Engine) buildRun() {
374372
select {
375373
case <-e.buildRunStopCh:
376374
return
377-
case <-e.canExit:
378375
default:
379376
}
380377
var err error
381378
if err = e.runPreCmd(); err != nil {
382-
e.canExit <- true
383379
e.runnerLog("failed to execute pre_cmd: %s", err.Error())
384380
if e.config.Build.StopOnError {
385381
return
386382
}
387383
}
388384
if err = e.building(); err != nil {
389-
e.canExit <- true
390385
e.buildLog("failed to build, error: %s", err.Error())
391386
_ = e.writeBuildErrorLog(err.Error())
392387
if e.config.Build.StopOnError {
@@ -399,7 +394,6 @@ func (e *Engine) buildRun() {
399394
return
400395
case <-e.exitCh:
401396
e.mainDebug("exit in buildRun")
402-
close(e.canExit)
403397
return
404398
default:
405399
}
@@ -521,7 +515,6 @@ func (e *Engine) runBin() error {
521515
case <-e.exitCh:
522516
e.mainDebug("exit in runBin")
523517
wg.Wait()
524-
close(e.canExit)
525518
default:
526519
}
527520
}()
@@ -595,7 +588,6 @@ func (e *Engine) cleanup() {
595588

596589
e.mainDebug("waiting for exit...")
597590

598-
<-e.canExit
599591
e.running = false
600592
e.mainDebug("exited")
601593
}

runner/engine_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ func TestRebuild(t *testing.T) {
339339
engine.Stop()
340340
t.Logf("engine stopped")
341341
wg.Wait()
342+
time.Sleep(time.Second * 1)
342343
assert.True(t, checkPortConnectionRefused(port))
343344
}
344345

@@ -449,6 +450,41 @@ func TestCtrlCWhenREngineIsRunning(t *testing.T) {
449450
assert.False(t, engine.running)
450451
}
451452

453+
func TestCtrlCWithFailedBin(t *testing.T) {
454+
timeout := 5 * time.Second
455+
done := make(chan struct{})
456+
go func() {
457+
dir := initWithQuickExitGoCode(t)
458+
chdir(t, dir)
459+
engine, err := NewEngine("", true)
460+
assert.NoError(t, err)
461+
engine.config.Build.Bin = "<WRONGCOMAMND>"
462+
sigs := make(chan os.Signal, 1)
463+
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
464+
var wg sync.WaitGroup
465+
wg.Add(1)
466+
go func() {
467+
engine.Run()
468+
t.Logf("engine stopped")
469+
wg.Done()
470+
}()
471+
go func() {
472+
<-sigs
473+
engine.Stop()
474+
t.Logf("engine stopped")
475+
}()
476+
time.Sleep(time.Second * 1)
477+
sigs <- syscall.SIGINT
478+
wg.Wait()
479+
close(done)
480+
}()
481+
select {
482+
case <-done:
483+
case <-time.After(timeout):
484+
t.Error("Test timed out")
485+
}
486+
}
487+
452488
func TestFixCloseOfChannelAfterCtrlC(t *testing.T) {
453489
// fix https://github.com/cosmtrek/air/issues/294
454490
dir := initWithBuildFailedCode(t)

0 commit comments

Comments
 (0)