diff --git a/go.sum b/go.sum index f3364ccf..08a282f0 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= -github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= -github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg= diff --git a/pkg/lockfile/lockfile.go b/pkg/lockfile/lockfile.go index 643fbe58..0ea225e5 100644 --- a/pkg/lockfile/lockfile.go +++ b/pkg/lockfile/lockfile.go @@ -55,13 +55,27 @@ func Write(dirPath string, displayer loghelper.Displayer) func() { }) } -func CleanAndExitOnInterrupt(clean func()) { +// the returned function may be used to avoid goroutine leak +// (also avoid conflicting behavior with versionmanager/proxy.transmitIncreasingSignal). +func CleanAndExitOnInterrupt(clean func()) func() { signalChan := make(chan os.Signal) + endChan := make(chan struct{}) signal.Notify(signalChan, os.Interrupt) go func() { - for range signalChan { - clean() - os.Exit(1) + for { + select { + case <-signalChan: + clean() + os.Exit(1) + case <-endChan: + signal.Stop(signalChan) + + break + } } }() + + return sync.OnceFunc(func() { //nolint + endChan <- struct{}{} + }) } diff --git a/versionmanager/manager.go b/versionmanager/manager.go index 26f25ab0..ca39e93f 100644 --- a/versionmanager/manager.go +++ b/versionmanager/manager.go @@ -292,7 +292,8 @@ func (m VersionManager) Uninstall(requestedVersion string) error { } deleteLock := lockfile.Write(installPath, m.conf.Displayer) - lockfile.CleanAndExitOnInterrupt(deleteLock) + disableExit := lockfile.CleanAndExitOnInterrupt(deleteLock) + defer disableExit() defer deleteLock() cleanedVersion := parsedVersion.String() @@ -333,7 +334,8 @@ func (m VersionManager) installSpecificVersion(version string, proxyCall bool) e } deleteLock := lockfile.Write(installPath, m.conf.Displayer) - lockfile.CleanAndExitOnInterrupt(deleteLock) + disableExit := lockfile.CleanAndExitOnInterrupt(deleteLock) + defer disableExit() defer deleteLock() entries, err := os.ReadDir(installPath)