Skip to content

Commit

Permalink
Merge pull request #87 from mschneider82/master
Browse files Browse the repository at this point in the history
protocol: fixed deadline datarace #86
  • Loading branch information
pires authored Mar 2, 2022
2 parents 2e44d7a + b4887c1 commit db39a71
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
run: go vet

- name: Test
run: go test -v -covermode=count -coverprofile=coverage.out
run: go test -race -v -covermode=atomic -coverprofile=coverage.out

- name: actions-goveralls
uses: shogo82148/[email protected]
Expand Down
21 changes: 13 additions & 8 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net"
"sync"
"sync/atomic"
"time"
)

Expand Down Expand Up @@ -33,15 +34,15 @@ type Listener struct {
// return the address of the client instead of the proxy address. Each connection
// will have its own readHeaderTimeout and readDeadline set by the Accept() call.
type Conn struct {
bufReader *bufio.Reader
readDeadline atomic.Value // time.Time
once sync.Once
readErr error
conn net.Conn
Validate Validator
bufReader *bufio.Reader
header *Header
once sync.Once
ProxyHeaderPolicy Policy
Validate Validator
readErr error
readHeaderTimeout time.Duration
readDeadline time.Time
}

// Validator receives a header and decides whether it is a valid one
Expand Down Expand Up @@ -215,7 +216,7 @@ func (p *Conn) UDPConn() (conn *net.UDPConn, ok bool) {

// SetDeadline wraps original conn.SetDeadline
func (p *Conn) SetDeadline(t time.Time) error {
p.readDeadline = t
p.readDeadline.Store(t)
return p.conn.SetDeadline(t)
}

Expand All @@ -224,7 +225,7 @@ func (p *Conn) SetReadDeadline(t time.Time) error {
// Set a local var that tells us the desired deadline. This is
// needed in order to reset the read deadline to the one that is
// desired by the user, rather than an empty deadline.
p.readDeadline = t
p.readDeadline.Store(t)
return p.conn.SetReadDeadline(t)
}

Expand All @@ -250,7 +251,11 @@ func (p *Conn) readHeader() error {
// Therefore, we check whether the error is a net.Timeout and if it is, we decide
// the proxy proto does not exist and set the error accordingly.
if p.readHeaderTimeout > 0 {
p.conn.SetReadDeadline(p.readDeadline)
t := p.readDeadline.Load()
if t == nil {
t = time.Time{}
}
p.conn.SetReadDeadline(t.(time.Time))
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
err = ErrNoProxyProtocol
}
Expand Down

0 comments on commit db39a71

Please sign in to comment.