diff --git a/lightning/cmd/tidb-lightning/main.go b/lightning/cmd/tidb-lightning/main.go index 53c4fe30bef32..d68d6033acc4f 100644 --- a/lightning/cmd/tidb-lightning/main.go +++ b/lightning/cmd/tidb-lightning/main.go @@ -99,7 +99,9 @@ func main() { finished := true if common.IsContextCanceledError(err) { err = nil - finished = false + if app.TaskCanceled() { + finished = false + } } if err != nil { logger.Error("tidb lightning encountered error stack info", zap.Error(err)) @@ -121,7 +123,7 @@ func main() { } } - if err != nil { + if err != nil || (globalCfg.App.ServerMode && !finished) { exit(1) } } diff --git a/lightning/pkg/server/lightning.go b/lightning/pkg/server/lightning.go index 6520e849fb6c8..3e4ed74f3913b 100644 --- a/lightning/pkg/server/lightning.go +++ b/lightning/pkg/server/lightning.go @@ -92,6 +92,8 @@ type Lightning struct { cancelLock sync.Mutex curTask *config.Config cancel context.CancelFunc // for per task context, which maybe different from lightning context + + taskCanceled bool } func initEnv(cfg *config.GlobalConfig) error { @@ -604,6 +606,7 @@ func (l *Lightning) run(taskCtx context.Context, taskCfg *config.Config, o *opti func (l *Lightning) Stop() { l.cancelLock.Lock() if l.cancel != nil { + l.taskCanceled = true l.cancel() } l.cancelLock.Unlock() @@ -613,6 +616,13 @@ func (l *Lightning) Stop() { l.shutdown() } +// TaskCanceled return whether the current task is canceled. +func (l *Lightning) TaskCanceled() bool { + l.cancelLock.Lock() + defer l.cancelLock.Unlock() + return l.taskCanceled +} + // Status return the sum size of file which has been imported to TiKV and the total size of source file. func (l *Lightning) Status() (finished int64, total int64) { finished = l.status.FinishedFileSize.Load()