Skip to content

Commit

Permalink
go: use deadlock.Mutex to check for deadlocks
Browse files Browse the repository at this point in the history
Signed-off-by: David Scott <dave@recoil.org>
  • Loading branch information
djs55 committed Feb 17, 2023
1 parent ecc9379 commit 156398d
Show file tree
Hide file tree
Showing 34 changed files with 1,590 additions and 13 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ require (
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/pelletier/go-toml v1.4.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ github.com/pelletier/go-toml v1.4.0 h1:u3Z1r+oOXJIkxqw34zVhyPgjBsm6X2wn21NWs/HfS
github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 h1:q2e307iGHPdTGp0hoxKjt1H5pDo6utceo3dQVK3I5XQ=
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5/go.mod h1:jvVRKCrJTQWu0XVbaOlby/2lO20uSCHEMzzplHXte1o=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1-0.20181023235946-059132a15dd0 h1:TVdhkEP0WKajbywS5TEDWwuzCl9EqOcQ6b1ymfmx/6E=
github.com/pkg/errors v0.8.1-0.20181023235946-059132a15dd0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -137,6 +139,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/sasha-s/go-deadlock v0.3.1 h1:sqv7fDNShgjcaxkO0JNcOAlr8B9+cV5Ey/OB71efZx0=
github.com/sasha-s/go-deadlock v0.3.1/go.mod h1:F73l+cr82YSh10GxyRI6qZiCgK64VaZjwesgfQ1/iLM=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down
4 changes: 3 additions & 1 deletion go/pkg/libproxy/loopbackconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net"
"sync"
"time"

"github.com/sasha-s/go-deadlock"
)

// io.Pipe is synchronous but we need to decouple the Read and Write calls
Expand All @@ -19,7 +21,7 @@ import (
type bufferedPipe struct {
bufs [][]byte
eof bool
m sync.Mutex
m deadlock.Mutex
c *sync.Cond
readDeadline time.Time
}
Expand Down
11 changes: 6 additions & 5 deletions go/pkg/libproxy/multiplexed.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"time"

"github.com/sasha-s/go-deadlock"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -39,7 +40,7 @@ func (w *windowState) advance() {
}

type channel struct {
m sync.Mutex
m deadlock.Mutex
c *sync.Cond
multiplexer *multiplexer
destination Destination
Expand Down Expand Up @@ -357,15 +358,15 @@ type multiplexer struct {
conn io.Closer
connR io.Reader // with buffering
connW *bufio.Writer
writeMutex sync.Mutex // hold when writing on the channel
writeMutex deadlock.Mutex // hold when writing on the channel
channels map[uint32]*channel
nextChannelID uint32
metadataMutex sync.Mutex // hold when reading/modifying this structure
pendingAccept []*channel // incoming connections
metadataMutex deadlock.Mutex // hold when reading/modifying this structure
pendingAccept []*channel // incoming connections
acceptCond *sync.Cond
isRunning bool
events *ring.Ring // log of packetEvents
eventsM sync.Mutex
eventsM deadlock.Mutex
allocateBackwards bool
}

Expand Down
3 changes: 2 additions & 1 deletion go/pkg/libproxy/multiplexed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"github.com/sasha-s/go-deadlock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -492,7 +493,7 @@ func TestMuxConcurrent(t *testing.T) {
serverReadSha := make(map[uint16]string)
clientWriteSha := make(map[uint16]string)
clientReadSha := make(map[uint16]string)
m := &sync.Mutex{}
m := &deadlock.Mutex{}
wg.Add(numConcurrent)
for i := 0; i < numConcurrent; i++ {
go func(i int) {
Expand Down
9 changes: 5 additions & 4 deletions go/pkg/libproxy/udp_encapsulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"errors"
"io"
"net"
"sync"
"time"

"github.com/sasha-s/go-deadlock"
)

// UDPListener defines a listener interface to read, write and close a UDP connection
Expand All @@ -28,9 +29,9 @@ type uDPEncapsulator interface {
// udpEncapsulator encapsulates a UDP connection and listener
type udpEncapsulator struct {
conn net.Conn
m sync.Mutex
r sync.Mutex
w sync.Mutex
m deadlock.Mutex
r deadlock.Mutex
w deadlock.Mutex
addr *net.UDPAddr
}

Expand Down
5 changes: 3 additions & 2 deletions go/pkg/libproxy/udp_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"encoding/binary"
"net"
"strings"
"sync"
"syscall"
"time"

"github.com/sasha-s/go-deadlock"
)

const (
Expand Down Expand Up @@ -50,7 +51,7 @@ type UDPProxy struct {
backendAddr *net.UDPAddr
dialer UDPDialer
connTrackTable connTrackMap
connTrackLock sync.Mutex
connTrackLock deadlock.Mutex
}

// UDPDialer creates UDP (pseudo-)connections to an address
Expand Down
4 changes: 4 additions & 0 deletions vendor/github.com/petermattis/goid/.gitignore

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

11 changes: 11 additions & 0 deletions vendor/github.com/petermattis/goid/.travis.yml

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

202 changes: 202 additions & 0 deletions vendor/github.com/petermattis/goid/LICENSE

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

5 changes: 5 additions & 0 deletions vendor/github.com/petermattis/goid/README.md

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

Loading

0 comments on commit 156398d

Please sign in to comment.