From 1ccf40d1e5228cc3601a9e82d7067c3448c98ea5 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Wed, 25 Apr 2018 11:55:07 -0700 Subject: [PATCH] Properly stop parallel work in mirror/cp when the user clicks on Ctrl-C (#2437) --- cmd/cp-main.go | 14 +++++++------- cmd/mirror-main.go | 8 ++++++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cmd/cp-main.go b/cmd/cp-main.go index afe2f495..6e3d320e 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -317,7 +317,11 @@ func doCopySession(session *sessionV8) error { var statusCh = make(chan URLs) - p, queueCh := newParallelManager(statusCh, pg) + parallel, queueCh := newParallelManager(statusCh, pg) + stopParallel := func() { + close(queueCh) + parallel.wait() + } go func() { // Loop through all urls. @@ -352,12 +356,7 @@ func doCopySession(session *sessionV8) error { } } - // URLs feeding finished - close(queueCh) - - // Wait for all tasks to be finished - p.wait() - + stopParallel() close(statusCh) }() @@ -368,6 +367,7 @@ loop: for { select { case <-trapCh: + stopParallel() cancelCopy() // Receive interrupt notification. if !globalQuiet && !globalJSON { diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index 98d9e540..c8a5d131 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -479,6 +479,10 @@ func (mj *mirrorJob) startMirror(ctx context.Context, cancelMirror context.Cance var totalObjects int64 parallel, queueCh := newParallelManager(mj.statusCh, mj.status) + stopParallel := func() { + close(queueCh) + parallel.wait() + } URLsCh := prepareMirrorURLs(mj.sourceURL, mj.targetURL, mj.isFake, mj.isOverwrite, mj.isRemove, mj.excludeOptions, mj.encKeyDB) @@ -486,8 +490,7 @@ func (mj *mirrorJob) startMirror(ctx context.Context, cancelMirror context.Cance select { case sURLs, ok := <-URLsCh: if !ok { - close(queueCh) - parallel.wait() + stopParallel() errCh <- nil return } @@ -531,6 +534,7 @@ func (mj *mirrorJob) startMirror(ctx context.Context, cancelMirror context.Cance } } case <-mj.trapCh: + stopParallel() cancelMirror() errCh <- nil return