Skip to content

Commit b9de5a3

Browse files
authored
Merge pull request #620 from djs55/deadlock-detect
go: use deadlock.Mutex to check for deadlocks
2 parents db339ee + 156398d commit b9de5a3

34 files changed

+1590
-13
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ require (
5252
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
5353
github.com/pelletier/go-toml v1.4.0 // indirect
5454
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
55+
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
5556
github.com/pmezard/go-difflib v1.0.0 // indirect
57+
github.com/sasha-s/go-deadlock v0.3.1 // indirect
5658
github.com/spf13/afero v1.2.2 // indirect
5759
github.com/spf13/cast v1.3.0 // indirect
5860
github.com/spf13/jwalterweatherman v1.1.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ github.com/pelletier/go-toml v1.4.0 h1:u3Z1r+oOXJIkxqw34zVhyPgjBsm6X2wn21NWs/HfS
122122
github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo=
123123
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
124124
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
125+
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 h1:q2e307iGHPdTGp0hoxKjt1H5pDo6utceo3dQVK3I5XQ=
126+
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5/go.mod h1:jvVRKCrJTQWu0XVbaOlby/2lO20uSCHEMzzplHXte1o=
125127
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
126128
github.com/pkg/errors v0.8.1-0.20181023235946-059132a15dd0 h1:TVdhkEP0WKajbywS5TEDWwuzCl9EqOcQ6b1ymfmx/6E=
127129
github.com/pkg/errors v0.8.1-0.20181023235946-059132a15dd0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -137,6 +139,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
137139
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
138140
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
139141
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
142+
github.com/sasha-s/go-deadlock v0.3.1 h1:sqv7fDNShgjcaxkO0JNcOAlr8B9+cV5Ey/OB71efZx0=
143+
github.com/sasha-s/go-deadlock v0.3.1/go.mod h1:F73l+cr82YSh10GxyRI6qZiCgK64VaZjwesgfQ1/iLM=
140144
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
141145
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
142146
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=

go/pkg/libproxy/loopbackconn.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"net"
66
"sync"
77
"time"
8+
9+
"github.com/sasha-s/go-deadlock"
810
)
911

1012
// io.Pipe is synchronous but we need to decouple the Read and Write calls
@@ -19,7 +21,7 @@ import (
1921
type bufferedPipe struct {
2022
bufs [][]byte
2123
eof bool
22-
m sync.Mutex
24+
m deadlock.Mutex
2325
c *sync.Cond
2426
readDeadline time.Time
2527
}

go/pkg/libproxy/multiplexed.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"sync"
1212
"time"
1313

14+
"github.com/sasha-s/go-deadlock"
1415
"golang.org/x/sync/errgroup"
1516
)
1617

@@ -39,7 +40,7 @@ func (w *windowState) advance() {
3940
}
4041

4142
type channel struct {
42-
m sync.Mutex
43+
m deadlock.Mutex
4344
c *sync.Cond
4445
multiplexer *multiplexer
4546
destination Destination
@@ -357,15 +358,15 @@ type multiplexer struct {
357358
conn io.Closer
358359
connR io.Reader // with buffering
359360
connW *bufio.Writer
360-
writeMutex sync.Mutex // hold when writing on the channel
361+
writeMutex deadlock.Mutex // hold when writing on the channel
361362
channels map[uint32]*channel
362363
nextChannelID uint32
363-
metadataMutex sync.Mutex // hold when reading/modifying this structure
364-
pendingAccept []*channel // incoming connections
364+
metadataMutex deadlock.Mutex // hold when reading/modifying this structure
365+
pendingAccept []*channel // incoming connections
365366
acceptCond *sync.Cond
366367
isRunning bool
367368
events *ring.Ring // log of packetEvents
368-
eventsM sync.Mutex
369+
eventsM deadlock.Mutex
369370
allocateBackwards bool
370371
}
371372

go/pkg/libproxy/multiplexed_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15+
"github.com/sasha-s/go-deadlock"
1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/require"
1718
"golang.org/x/sync/errgroup"
@@ -492,7 +493,7 @@ func TestMuxConcurrent(t *testing.T) {
492493
serverReadSha := make(map[uint16]string)
493494
clientWriteSha := make(map[uint16]string)
494495
clientReadSha := make(map[uint16]string)
495-
m := &sync.Mutex{}
496+
m := &deadlock.Mutex{}
496497
wg.Add(numConcurrent)
497498
for i := 0; i < numConcurrent; i++ {
498499
go func(i int) {

go/pkg/libproxy/udp_encapsulation.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
"errors"
77
"io"
88
"net"
9-
"sync"
109
"time"
10+
11+
"github.com/sasha-s/go-deadlock"
1112
)
1213

1314
// UDPListener defines a listener interface to read, write and close a UDP connection
@@ -28,9 +29,9 @@ type uDPEncapsulator interface {
2829
// udpEncapsulator encapsulates a UDP connection and listener
2930
type udpEncapsulator struct {
3031
conn net.Conn
31-
m sync.Mutex
32-
r sync.Mutex
33-
w sync.Mutex
32+
m deadlock.Mutex
33+
r deadlock.Mutex
34+
w deadlock.Mutex
3435
addr *net.UDPAddr
3536
}
3637

go/pkg/libproxy/udp_proxy.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"encoding/binary"
55
"net"
66
"strings"
7-
"sync"
87
"syscall"
98
"time"
9+
10+
"github.com/sasha-s/go-deadlock"
1011
)
1112

1213
const (
@@ -50,7 +51,7 @@ type UDPProxy struct {
5051
backendAddr *net.UDPAddr
5152
dialer UDPDialer
5253
connTrackTable connTrackMap
53-
connTrackLock sync.Mutex
54+
connTrackLock deadlock.Mutex
5455
}
5556

5657
// UDPDialer creates UDP (pseudo-)connections to an address

vendor/github.com/petermattis/goid/.gitignore

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

vendor/github.com/petermattis/goid/.travis.yml

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

vendor/github.com/petermattis/goid/LICENSE

Lines changed: 202 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)