Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutex around helper cleanups #1947

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cli/internal/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"os"
"sync"

"github.com/fatih/color"
"github.com/hashicorp/go-hclog"
Expand Down Expand Up @@ -52,18 +53,23 @@ type Helper struct {
// to allow overrides in tests
UserConfigPath turbopath.AbsolutePath

cleanups []io.Closer
cleanupsMu sync.Mutex
cleanups []io.Closer
}

// RegisterCleanup saves a function to be run after turbo execution,
// even if the command that runs returns an error
func (h *Helper) RegisterCleanup(cleanup io.Closer) {
h.cleanupsMu.Lock()
defer h.cleanupsMu.Unlock()
h.cleanups = append(h.cleanups, cleanup)
}

// Cleanup runs the register cleanup handlers. It requires the flags
// to the root command so that it can construct a UI if necessary
func (h *Helper) Cleanup(flags *pflag.FlagSet) {
h.cleanupsMu.Lock()
defer h.cleanupsMu.Unlock()
var ui cli.Ui
for _, cleanup := range h.cleanups {
if err := cleanup.Close(); err != nil {
Expand Down