Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove deadlock in multiPacketListener #211

Merged
merged 12 commits into from
Sep 18, 2024
39 changes: 22 additions & 17 deletions service/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,42 +128,40 @@ type readRequest struct {

type virtualPacketConn struct {
net.PacketConn
mu sync.Mutex // Mutex to protect access to the channels
readCh chan readRequest
readCh chan readRequest

mu sync.Mutex // Mutex to protect against race conditions when closing the connection.
closeCh chan struct{}
onCloseFunc OnCloseFunc
}

func (pc *virtualPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
func (pc *virtualPacketConn) ReadFrom(p []byte) (int, net.Addr, error) {
respCh := make(chan struct {
n int
addr net.Addr
err error
}, 1)

pc.mu.Lock()
if pc.readCh == nil {
pc.mu.Unlock()
return 0, nil, net.ErrClosed
}
pc.readCh <- readRequest{
select {
case pc.readCh <- readRequest{
fortuna marked this conversation as resolved.
Show resolved Hide resolved
buffer: p,
respCh: respCh,
}:
case <-pc.closeCh:
return 0, nil, net.ErrClosed
}
pc.mu.Unlock()

resp := <-respCh
return resp.n, resp.addr, resp.err
}

// Close closes the virtualPacketConn. It must be called once, and only once,
// per virtualPacketConn.
func (pc *virtualPacketConn) Close() error {
pc.mu.Lock()
defer pc.mu.Unlock()

if pc.readCh == nil {
return nil
}
pc.readCh = nil

close(pc.closeCh)
if pc.onCloseFunc != nil {
onCloseFunc := pc.onCloseFunc
sbruens marked this conversation as resolved.
Show resolved Hide resolved
pc.onCloseFunc = nil
Expand Down Expand Up @@ -287,10 +285,14 @@ func (m *multiPacketListener) Acquire() (net.PacketConn, error) {
m.readCh = make(chan readRequest)
m.doneCh = make(chan struct{})
go func() {
defer close(m.readCh)
buffer := make([]byte, serverUDPBufferSize)
for {
n, addr, err := m.pc.ReadFrom(buffer)
buffer = buffer[:n]
select {
case req := <-m.readCh:
n, addr, err := pc.ReadFrom(req.buffer)
n := copy(req.buffer, buffer)
req.respCh <- struct {
n int
addr net.Addr
Expand All @@ -299,6 +301,9 @@ func (m *multiPacketListener) Acquire() (net.PacketConn, error) {
case <-m.doneCh:
return
}
if errors.Is(err, net.ErrClosed) {
return
}
}
}()
}
Expand All @@ -307,14 +312,14 @@ func (m *multiPacketListener) Acquire() (net.PacketConn, error) {
return &virtualPacketConn{
PacketConn: m.pc,
readCh: m.readCh,
closeCh: make(chan struct{}),
onCloseFunc: func() error {
m.mu.Lock()
defer m.mu.Unlock()
m.count--
if m.count == 0 {
close(m.doneCh)
m.pc.Close()
m.pc = nil
if m.onCloseFunc != nil {
onCloseFunc := m.onCloseFunc
m.onCloseFunc = nil
Expand Down
Loading