Skip to content

wireguard: close a device that has had no flow for five minutes - #354

Open
huzgnm wants to merge 1 commit into
aimalygin:mainfrom
huzgnm:perf/wireguard-idle-close
Open

huzgnm wants to merge 1 commit into
aimalygin:mainfrom
huzgnm:perf/wireguard-idle-close

Conversation

@huzgnm

@huzgnm huzgnm commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Problem

The vendored gotatun device runs handle_timers every 250 ms for as long as it lives (vendor/gotatun/src/device/mod.rs), and WireguardOutbound keeps its cached device until the core stops. So a profile that used a WireGuard outbound once — even for a single DNS query — wakes an idle phone four times a second for the rest of the session. On iOS/Android that shows up as background battery drain on a tunnel that carries no WireGuard traffic.

Change

  • xray-wireguard: Client::open_flows() — flows holding or waiting for a place (open TCP/UDP flows, unspent TcpSlots, queued TCP opens).
  • xray-core-rs WireGuard outbound: each started device gets a small task that closes it once it has had no open flow, and no caller has taken it, for 5 minutes (IDLE_CLOSE). The next flow starts a new device with a fresh handshake. The idle time counts from the last client() call, so a caller that just took the device is never handed a closed one. The task holds only a Weak<Owner> and a session number, and ends when the device is closed or replaced.
  • A device whose peers set keepAlive is never closed, since that asks for the path to stay open while idle.

Making gotatun's timer loop deadline-driven would also fix this, but it touches the vendored fork; this keeps the change in our own crates.

Tests

  • wireguard_device_without_flows_is_closed_after_idle_and_restarts — kept while a flow is open (15 min), closed after idle, next flow starts a new device.
  • wireguard_idle_time_counts_from_the_last_caller
  • wireguard_device_with_keepalive_is_kept_while_idle

The first two fail with the idle task disabled. cargo fmt --check, cargo clippy -p xray-core-rs -p xray-wireguard --all-targets --all-features -D warnings, and cargo test -p xray-wireguard -p xray-core-rs --all-targets pass.

A started WireGuard device runs its timers every 250 ms for as long as it
lives, and the outbound kept its device until the core stopped. A profile
that had used a WireGuard outbound once therefore woke an idle phone four
times a second.

The outbound now closes its device once no flow has been open and no caller
has taken it for five minutes; the next flow starts a new device with a
fresh handshake. A device whose peers set keepAlive is kept, since that asks
for the path to stay open while idle. Client::open_flows counts the flows
holding or waiting for a place, which the idle check reads.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant