device: convert runtime.SetFinalizer to AddCleanup (#71)

In PR #66, we tried to address a memory leak by avoiding
runtime.SetFinalizer for autodrainingInboundQueue and
autodrainingOutboundQueue unless there was something to do.

However, when there is work to be done, these finalizers still leak
memory because they’re still holding on to a cyclical reference to q.
This applies to any platform that relies on a bounded device.WaitPool,
like Android and iOS which both declare PreallocatedBuffersPerPool.

This patch converts this logic to runtime.AddCleanup which is designed
to avoid this problem.

Updates tailscale/corp#42776

Signed-off-by: Simon Law <sfllaw@tailscale.com>
This commit is contained in:
Simon Law 2026-06-18 18:02:42 -07:00 committed by 世界
parent 2ad9837e6c
commit cd7ac13b86
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 8 additions and 8 deletions

View file

@ -244,8 +244,8 @@ func (peer *Peer) Start() {
peer.timersStart()
device.flushInboundQueue(peer.queue.inbound)
device.flushOutboundQueue(peer.queue.outbound)
device.flushInboundQueue(peer.queue.inbound.c)
device.flushOutboundQueue(peer.queue.outbound.c)
// Use the device batch size, not the bind batch size, as the device size is
// the size of the batch pools.