Skip to content

device: convert runtime.SetFinalizer to AddCleanup#71

Merged
sfllaw merged 1 commit into
tailscalefrom
sfllaw/replace-setfinalizer-with-addcleanup
Jun 19, 2026
Merged

device: convert runtime.SetFinalizer to AddCleanup#71
sfllaw merged 1 commit into
tailscalefrom
sfllaw/replace-setfinalizer-with-addcleanup

Conversation

@sfllaw

@sfllaw sfllaw commented Jun 17, 2026

Copy link
Copy Markdown

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 completely.

Updates tailscale/corp#42776

@sfllaw sfllaw requested review from bradfitz, jwhited and lkosewsk June 17, 2026 02:16
@sfllaw sfllaw self-assigned this Jun 17, 2026
@sfllaw sfllaw added the bug Something isn't working label Jun 17, 2026
@sfllaw sfllaw force-pushed the sfllaw/replace-setfinalizer-with-addcleanup branch from f74e38b to cea3f6c Compare June 17, 2026 02:27
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>
@sfllaw sfllaw force-pushed the sfllaw/replace-setfinalizer-with-addcleanup branch from cea3f6c to a214f8e Compare June 17, 2026 02:29

@illotum illotum left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM. I trust you ran a live test?

@lkosewsk lkosewsk left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

With the caveat that I don't know the code other than in a black box "I am a user" way, it LGTM.

@lkosewsk

lkosewsk commented Jun 18, 2026

Copy link
Copy Markdown

To testing, I want to say that I took this branch and rebased my original test from #65 that motivated the #66 fix on top. I can verify from the perspective of the original memory leak that this still fixes it, see https://github.com/lkosewsk/wireguard-go/tree/lk/test-sfllaw

@sfllaw sfllaw merged commit 493cf27 into tailscale Jun 19, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants