Skip to content

Commit 64b6266

Browse files
authored
Lock by flock to prevent parallel runs (#812)
1 parent 9ba730e commit 64b6266

File tree

15 files changed

+700
-1
lines changed

15 files changed

+700
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/fatih/color v1.7.0
99
github.com/go-critic/go-critic v0.3.5-0.20190904082202-d79a9f0c64db
1010
github.com/go-lintpack/lintpack v0.5.2
11+
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b
1112
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
1213
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
1314
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ github.com/go-toolsmith/typep v1.0.0 h1:zKymWyA1TRYvqYrYDrfEMZULyrhcnGY3x7LDKU2X
6666
github.com/go-toolsmith/typep v1.0.0/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU=
6767
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
6868
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
69+
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b h1:ekuhfTjngPhisSjOJ0QWKpPQE8/rbknHaes6WVJj5Hw=
70+
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
6971
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
7072
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
7173
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=

pkg/commands/executor.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package commands
22

33
import (
44
"bytes"
5+
"context"
56
"crypto/sha256"
67
"encoding/json"
78
"fmt"
89
"io"
910
"os"
11+
"time"
1012

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

@@ -15,6 +17,8 @@ import (
1517
"github.com/spf13/cobra"
1618
"github.com/spf13/pflag"
1719

20+
"github.com/gofrs/flock"
21+
1822
"github.com/golangci/golangci-lint/internal/pkgcache"
1923
"github.com/golangci/golangci-lint/pkg/config"
2024
"github.com/golangci/golangci-lint/pkg/fsutils"
@@ -48,6 +52,7 @@ type Executor struct {
4852
sw *timeutils.Stopwatch
4953

5054
loadGuard *load.Guard
55+
flock *flock.Flock
5156
}
5257

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

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

6674
// to setup log level early we need to parse config from command line extra time to
6775
// find `-v` option
@@ -195,3 +203,24 @@ func computeConfigSalt(cfg *config.Config) ([]byte, error) {
195203
}
196204
return h.Sum(nil), nil
197205
}
206+
207+
func (e *Executor) acquireFileLock() bool {
208+
lockFile := os.TempDir() + "/golangci-lint.lock"
209+
e.debugf("Locking on file %s...", lockFile)
210+
f := flock.New(lockFile)
211+
ctx, finish := context.WithTimeout(context.Background(), time.Minute)
212+
defer finish()
213+
214+
if ok, _ := f.TryLockContext(ctx, time.Second*3); !ok {
215+
return false
216+
}
217+
218+
e.flock = f
219+
return true
220+
}
221+
222+
func (e *Executor) releaseFileLock() {
223+
if err := e.flock.Unlock(); err != nil {
224+
e.debugf("Failed to unlock on file: %s", err)
225+
}
226+
}

pkg/commands/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func (e *Executor) persistentPostRun(_ *cobra.Command, _ []string) {
7373
trace.Stop()
7474
}
7575

76+
e.releaseFileLock()
7677
os.Exit(e.exitCode)
7778
}
7879

pkg/commands/run.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ func watchResources(ctx context.Context, done chan struct{}, logger logutils.Log
453453
const MB = 1024 * 1024
454454

455455
track := func() {
456-
debugf("Starting memory tracing iteration ...")
457456
var m runtime.MemStats
458457
runtime.ReadMemStats(&m)
459458

vendor/github.com/gofrs/flock/.gitignore

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gofrs/flock/.travis.yml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gofrs/flock/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gofrs/flock/README.md

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gofrs/flock/appveyor.yml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gofrs/flock/flock.go

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)