Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions device/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,18 @@ func newAutodrainingInboundQueue(device *Device) *autodrainingInboundQueue {
q := &autodrainingInboundQueue{
c: make(chan *QueueInboundElementsContainer, QueueInboundSize),
}
runtime.SetFinalizer(q, device.flushInboundQueue)
if device.needsInboundQueueFinalizer() {
runtime.SetFinalizer(q, device.flushInboundQueue)
}
Comment on lines +86 to +88

@sfllaw sfllaw Jun 3, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Suggestion (non-blocking): Would it be possible to convert this so it uses runtime.AddCleanup, which might also be safer?

Suggested change
if device.needsInboundQueueFinalizer() {
runtime.SetFinalizer(q, device.flushInboundQueue)
}
if device.needsInboundQueueFinalizer() {
runtime.AddCleanup(q, device.flushInboundQueue, q.c)
}

(continued…)

return q
}

func (device *Device) needsInboundQueueFinalizer() bool {
return device.pool.messageBuffers.hasAccounting() ||
device.pool.inboundElements.hasAccounting() ||
device.pool.inboundElementsContainer.hasAccounting()
}

func (device *Device) flushInboundQueue(q *autodrainingInboundQueue) {
for {
select {
Expand Down Expand Up @@ -116,10 +124,18 @@ func newAutodrainingOutboundQueue(device *Device) *autodrainingOutboundQueue {
q := &autodrainingOutboundQueue{
c: make(chan *QueueOutboundElementsContainer, QueueOutboundSize),
}
runtime.SetFinalizer(q, device.flushOutboundQueue)
if device.needsOutboundQueueFinalizer() {
runtime.SetFinalizer(q, device.flushOutboundQueue)
}
return q
}

func (device *Device) needsOutboundQueueFinalizer() bool {
return device.pool.messageBuffers.hasAccounting() ||
device.pool.outboundElements.hasAccounting() ||
device.pool.outboundElementsContainer.hasAccounting()
}

func (device *Device) flushOutboundQueue(q *autodrainingOutboundQueue) {
for {
select {
Expand Down
53 changes: 53 additions & 0 deletions device/channels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
*/

package device

import "testing"

func TestAutodrainingQueueFinalizerNeedTracksPoolAccounting(t *testing.T) {
unbounded := func() *WaitPool { return NewWaitPool(0, func() any { return nil }) }
bounded := func() *WaitPool { return NewWaitPool(1, func() any { return nil }) }

device := &Device{}
device.pool.messageBuffers = unbounded()
device.pool.inboundElements = unbounded()
device.pool.inboundElementsContainer = unbounded()
device.pool.outboundElements = unbounded()
device.pool.outboundElementsContainer = unbounded()

if device.needsInboundQueueFinalizer() {
t.Fatal("unbounded inbound pools should not need queue finalizer")
}
if device.needsOutboundQueueFinalizer() {
t.Fatal("unbounded outbound pools should not need queue finalizer")
}

device.pool.inboundElementsContainer = bounded()
if !device.needsInboundQueueFinalizer() {
t.Fatal("bounded inbound pool should need queue finalizer")
}
if device.needsOutboundQueueFinalizer() {
t.Fatal("bounded inbound pool should not affect outbound queue finalizer")
}

device.pool.inboundElementsContainer = unbounded()
device.pool.outboundElementsContainer = bounded()
if device.needsInboundQueueFinalizer() {
t.Fatal("bounded outbound pool should not affect inbound queue finalizer")
}
if !device.needsOutboundQueueFinalizer() {
t.Fatal("bounded outbound pool should need queue finalizer")
}

device.pool.outboundElementsContainer = unbounded()
device.pool.messageBuffers = bounded()
if !device.needsInboundQueueFinalizer() {
t.Fatal("bounded message buffer pool should need inbound queue finalizer")
}
if !device.needsOutboundQueueFinalizer() {
t.Fatal("bounded message buffer pool should need outbound queue finalizer")
}
}
4 changes: 4 additions & 0 deletions device/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func NewWaitPool(max uint32, new func() any) *WaitPool {
return p
}

func (p *WaitPool) hasAccounting() bool {
return p != nil && p.max != 0
}

func (p *WaitPool) Get() any {
if p.max != 0 {
p.lock.Lock()
Expand Down
Loading