Skip to content

Commit

Permalink
Lock by flock to prevent parallel runs (#812)
Browse files Browse the repository at this point in the history
  • Loading branch information
jirfag committed Oct 13, 2019
1 parent 9ba730e commit 64b6266
Show file tree
Hide file tree
Showing 15 changed files with 700 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -8,6 +8,7 @@ require (
github.com/fatih/color v1.7.0
github.com/go-critic/go-critic v0.3.5-0.20190904082202-d79a9f0c64db
github.com/go-lintpack/lintpack v0.5.2
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -66,6 +66,8 @@ github.com/go-toolsmith/typep v1.0.0 h1:zKymWyA1TRYvqYrYDrfEMZULyrhcnGY3x7LDKU2X
github.com/go-toolsmith/typep v1.0.0/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b h1:ekuhfTjngPhisSjOJ0QWKpPQE8/rbknHaes6WVJj5Hw=
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
Expand Down
29 changes: 29 additions & 0 deletions pkg/commands/executor.go
Expand Up @@ -2,11 +2,13 @@ package commands

import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"os"
"time"

"github.com/golangci/golangci-lint/internal/cache"

Expand All @@ -15,6 +17,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/gofrs/flock"

"github.com/golangci/golangci-lint/internal/pkgcache"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/fsutils"
Expand Down Expand Up @@ -48,6 +52,7 @@ type Executor struct {
sw *timeutils.Stopwatch

loadGuard *load.Guard
flock *flock.Flock
}

func NewExecutor(version, commit, date string) *Executor {
Expand All @@ -62,6 +67,9 @@ func NewExecutor(version, commit, date string) *Executor {

e.debugf("Starting execution...")
e.log = report.NewLogWrapper(logutils.NewStderrLog(""), &e.reportData)
if ok := e.acquireFileLock(); !ok {
e.log.Fatalf("Parallel golangci-lint is running")
}

// to setup log level early we need to parse config from command line extra time to
// find `-v` option
Expand Down Expand Up @@ -195,3 +203,24 @@ func computeConfigSalt(cfg *config.Config) ([]byte, error) {
}
return h.Sum(nil), nil
}

func (e *Executor) acquireFileLock() bool {
lockFile := os.TempDir() + "/golangci-lint.lock"
e.debugf("Locking on file %s...", lockFile)
f := flock.New(lockFile)
ctx, finish := context.WithTimeout(context.Background(), time.Minute)
defer finish()

if ok, _ := f.TryLockContext(ctx, time.Second*3); !ok {
return false
}

e.flock = f
return true
}

func (e *Executor) releaseFileLock() {
if err := e.flock.Unlock(); err != nil {
e.debugf("Failed to unlock on file: %s", err)
}
}
1 change: 1 addition & 0 deletions pkg/commands/root.go
Expand Up @@ -73,6 +73,7 @@ func (e *Executor) persistentPostRun(_ *cobra.Command, _ []string) {
trace.Stop()
}

e.releaseFileLock()
os.Exit(e.exitCode)
}

Expand Down
1 change: 0 additions & 1 deletion pkg/commands/run.go
Expand Up @@ -453,7 +453,6 @@ func watchResources(ctx context.Context, done chan struct{}, logger logutils.Log
const MB = 1024 * 1024

track := func() {
debugf("Starting memory tracing iteration ...")
var m runtime.MemStats
runtime.ReadMemStats(&m)

Expand Down
24 changes: 24 additions & 0 deletions vendor/github.com/gofrs/flock/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/gofrs/flock/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/gofrs/flock/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions vendor/github.com/gofrs/flock/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/gofrs/flock/appveyor.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions vendor/github.com/gofrs/flock/flock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 64b6266

Please sign in to comment.