[GH-ISSUE #168] device: first transport packet can use stale S4 after UAPI configuration #83

Open
opened 2026-10-14 04:03:24 +07:00 by bsakaguchi · 3 comments
Owner

Originally created by @coremraz on GitHub (Jul 31, 2026).
Original GitHub issue: https://github.com/amnezia-vpn/amneziawg-go/issues/168

Suggested title

device: first transport packet can use stale S4 after UAPI configuration

Affected upstream source

  • Repository: https://github.com/amnezia-vpn/amneziawg-go
  • Version: v3.0.3
  • Immutable revision: cf9d2dd202821301f7039093b0a1b3d4b574c47c
  • At investigation time, upstream master was identical to that revision.
  • Linux test environment: golang:1.25.12, Docker with CAP_NET_ADMIN and
    /dev/net/tun.

Safe reproduction

This reproducer uses only upstream tests, localhost UDP and freshly generated
test keys. It contains no gateway address, production key, client config or
credential.

docker run --rm --cap-add=NET_ADMIN --device /dev/net/tun \
  -v $PWD:/src:ro -w /src golang:1.25.12 \
  go test ./device -run '^TestAWGDevicePing$' -race=false -count=10

Observed on the pinned revision: the AWG test fails after a successful
handshake with a ping/pong transit timeout. A profile bisect showed that
S4=25 is sufficient: seven of ten isolated runs failed; H1–H4 alone and
S1–S3 alone passed.

Causal evidence

Device.NewDevice starts RoutineReadFromTUN before the caller has applied
UAPI configuration. In device/send.go, that routine loads
device.paddings.transport, calculates offset, and then blocks in
tun.device.Read. If UAPI commits S4=25 while that read is waiting, the
first returned packet is still associated with the stale local S4=0.

The receiver has already applied S4 and therefore calls
DeterminePacketTypeAndPadding expecting the transport header at offset 25.
It rejects the packet before key lookup or AEAD decrypt.

Temporary instrumentation in a disposable source copy correlated one failing
run as follows:

  1. sender TUN reader started the blocking read with S4=0;
  2. UAPI committed S4=25 on both devices;
  3. sender returned the first packet with its original S4=0 snapshot;
  4. receiver reported s4=25, expected H4/type 4, observed type 4 at byte
    zero and random bytes at byte 25, then logged Received message with unknown type.

This is a stale snapshot across a blocking syscall, not a key exchange,
header-protection-key, routing or NAT failure.

Candidate direction for maintainer review

The following was validated only in an ephemeral Docker copy; it is not a
proposed final patch. Immediately after Read, re-load the current transport
padding. If it changed, move every just-read payload from the original offset
to the current offset using overlap-safe copy, then use that same current
padding for the outgoing element:

count, readErr = device.tun.device.Read(bufs, sizes, offset)
currentPadding := device.paddings.transport.Load()
if currentPadding != padding {
    currentOffset := MessageTransportHeaderSize + int(currentPadding)
    for i := 0; i < count; i++ {
        copy(bufs[i][currentOffset:currentOffset+sizes[i]],
            bufs[i][offset:offset+sizes[i]])
    }
    padding = currentPadding
    offset = currentOffset
}

With that source-copy change:

  • S4-only reproduction passed 10/10 with -race=false;
  • full TestAWGDevicePing passed 10/10 with -race=false;
  • full TestAWGDevicePing passed 10/10 with -race.

Please advise whether re-basing the packet buffer after Read is the intended
configuration-update behavior, or whether initial UAPI configuration should
instead gate the first TUN read. A final upstream patch should include a
deterministic regression test for this startup ordering.

Downstream safety posture

No production data plane has been enabled from this finding. The downstream
canary remains zero-peer with UDP closed and forwarding/NAT disabled. No local
fork or S4 workaround will be deployed before a reviewed upstream or otherwise
immutable-pinned fix, exact-artifact verification, and closed synthetic-peer
staging.

Originally created by @coremraz on GitHub (Jul 31, 2026). Original GitHub issue: https://github.com/amnezia-vpn/amneziawg-go/issues/168 ## Suggested title `device: first transport packet can use stale S4 after UAPI configuration` ## Affected upstream source - Repository: `https://github.com/amnezia-vpn/amneziawg-go` - Version: `v3.0.3` - Immutable revision: `cf9d2dd202821301f7039093b0a1b3d4b574c47c` - At investigation time, upstream `master` was identical to that revision. - Linux test environment: `golang:1.25.12`, Docker with `CAP_NET_ADMIN` and `/dev/net/tun`. ## Safe reproduction This reproducer uses only upstream tests, localhost UDP and freshly generated test keys. It contains no gateway address, production key, client config or credential. ```bash docker run --rm --cap-add=NET_ADMIN --device /dev/net/tun \ -v $PWD:/src:ro -w /src golang:1.25.12 \ go test ./device -run '^TestAWGDevicePing$' -race=false -count=10 ``` Observed on the pinned revision: the AWG test fails after a successful handshake with a ping/pong transit timeout. A profile bisect showed that `S4=25` is sufficient: seven of ten isolated runs failed; H1–H4 alone and S1–S3 alone passed. ## Causal evidence `Device.NewDevice` starts `RoutineReadFromTUN` before the caller has applied UAPI configuration. In `device/send.go`, that routine loads `device.paddings.transport`, calculates `offset`, and then blocks in `tun.device.Read`. If UAPI commits `S4=25` while that read is waiting, the first returned packet is still associated with the stale local `S4=0`. The receiver has already applied S4 and therefore calls `DeterminePacketTypeAndPadding` expecting the transport header at offset 25. It rejects the packet before key lookup or AEAD decrypt. Temporary instrumentation in a disposable source copy correlated one failing run as follows: 1. sender TUN reader started the blocking read with `S4=0`; 2. UAPI committed `S4=25` on both devices; 3. sender returned the first packet with its original `S4=0` snapshot; 4. receiver reported `s4=25`, expected H4/type `4`, observed type `4` at byte zero and random bytes at byte 25, then logged `Received message with unknown type`. This is a stale snapshot across a blocking syscall, not a key exchange, header-protection-key, routing or NAT failure. ## Candidate direction for maintainer review The following was validated **only** in an ephemeral Docker copy; it is not a proposed final patch. Immediately after `Read`, re-load the current transport padding. If it changed, move every just-read payload from the original offset to the current offset using overlap-safe `copy`, then use that same current padding for the outgoing element: ```go count, readErr = device.tun.device.Read(bufs, sizes, offset) currentPadding := device.paddings.transport.Load() if currentPadding != padding { currentOffset := MessageTransportHeaderSize + int(currentPadding) for i := 0; i < count; i++ { copy(bufs[i][currentOffset:currentOffset+sizes[i]], bufs[i][offset:offset+sizes[i]]) } padding = currentPadding offset = currentOffset } ``` With that source-copy change: - S4-only reproduction passed 10/10 with `-race=false`; - full `TestAWGDevicePing` passed 10/10 with `-race=false`; - full `TestAWGDevicePing` passed 10/10 with `-race`. Please advise whether re-basing the packet buffer after `Read` is the intended configuration-update behavior, or whether initial UAPI configuration should instead gate the first TUN read. A final upstream patch should include a deterministic regression test for this startup ordering. ## Downstream safety posture No production data plane has been enabled from this finding. The downstream canary remains zero-peer with UDP closed and forwarding/NAT disabled. No local fork or S4 workaround will be deployed before a reviewed upstream or otherwise immutable-pinned fix, exact-artifact verification, and closed synthetic-peer staging.
Author
Owner

@hoaxisr commented on GitHub (Aug 13, 2026):

Still present on 3.1. Confirming on a newer revision than the one in the report,
with failure rates and a small matrix that narrows what triggers it.

Revisions tested

  • 08271d00 (master, tag v3.1.20260813) - reproduces
  • 08d68cd (the commit right before the 3.1 feature merge) -reproduces at the same rate
go test -count=1 ./device -run '^TestScratchSxOnly$'
Configuration Failures
s1..s4 only, on 08271d00 (3.1) 4/20
s1..s4 only, on 08d68cd (pre-3.1) 6/20
jc/jmin/jmax + s1..s4 11/20
s1..s4 + random_trailers 9/20
jc/jmin/jmax + random_trailers, no Sx 0/20
random_trailers only 0/20

The last two rows are the useful part: the 3.1 features on their own are clean,
and junk packets on their own are clean. Sx is the single factor which lines
up with the stale-offset-across-Read explanation in the report rather than
with anything the 3.1 merge introduced.

Test used for the first two rows:

func TestScratchSxOnly(t *testing.T) {
	pair := genTestPair(t, true,
		"s1", "15",
		"s2", "18",
		"s3", "20",
		"s4", "25",
	)
	t.Run("ping 1.0.0.1", func(t *testing.T) {
		pair.Send(t, Ping, nil)
	})
}

Independent confirmation of the mechanism. Two temporary probes on a clean
checkout, caught on both sides of the same failing run:

ERROR: dev1: PROBE send elem.padding=0 devPadding=25 totalLen=80
ERROR: dev0: PROBE UNKNOWN size=64

The sender probe sits at the end of RoutineEncryption and shows the outbound
element carrying elem.padding == 0 while device.paddings.transport already
reads 25. The receiver probe sits right after DeterminePacketTypeAndPadding
and shows the resulting 64-byte datagram classified as MessageUnknownType.
Same picture as the report, on the 3.1 revision.

<!-- gh-comment-id:5285822317 --> @hoaxisr commented on GitHub (Aug 13, 2026): Still present on 3.1. Confirming on a newer revision than the one in the report, with failure rates and a small matrix that narrows what triggers it. **Revisions tested** * `08271d00` (`master`, tag `v3.1.20260813`) - reproduces * `08d68cd` (the commit right before the 3.1 feature merge) -reproduces at the same rate ``` go test -count=1 ./device -run '^TestScratchSxOnly$' ``` | Configuration | Failures | |---|---| | `s1..s4` only, on `08271d00` (3.1) | 4/20 | | `s1..s4` only, on `08d68cd` (pre-3.1) | 6/20 | | `jc/jmin/jmax` + `s1..s4` | 11/20 | | `s1..s4` + `random_trailers` | 9/20 | | `jc/jmin/jmax` + `random_trailers`, **no Sx** | 0/20 | | `random_trailers` only | 0/20 | The last two rows are the useful part: the 3.1 features on their own are clean, and junk packets on their own are clean. `Sx` is the single factor which lines up with the stale-`offset`-across-`Read` explanation in the report rather than with anything the 3.1 merge introduced. Test used for the first two rows: ```go func TestScratchSxOnly(t *testing.T) { pair := genTestPair(t, true, "s1", "15", "s2", "18", "s3", "20", "s4", "25", ) t.Run("ping 1.0.0.1", func(t *testing.T) { pair.Send(t, Ping, nil) }) } ``` **Independent confirmation of the mechanism.** Two temporary probes on a clean checkout, caught on both sides of the same failing run: ``` ERROR: dev1: PROBE send elem.padding=0 devPadding=25 totalLen=80 ERROR: dev0: PROBE UNKNOWN size=64 ``` The sender probe sits at the end of `RoutineEncryption` and shows the outbound element carrying `elem.padding == 0` while `device.paddings.transport` already reads `25`. The receiver probe sits right after `DeterminePacketTypeAndPadding` and shows the resulting 64-byte datagram classified as `MessageUnknownType`. Same picture as the report, on the 3.1 revision.
Author
Owner

@StarProxima commented on GitHub (Oct 9, 2026):

Still present on v3.1.20260828 (master b5928ef). Adding what we saw in an integration and a test that may help to pick one of the open PRs.

Where it shows. We use amneziawg-go in the WireGuard proxy of Xray-core, outbound and inbound, with a gVisor netstack TUN, in an app for iOS, Android and macOS. An idle outbound there closes its device and creates a new one on the next connection, so the bug hits every wake rather than once per start: the first TCP SYN arrives a second late after its retransmission, and the first UDP datagram, often a DNS query, is lost. On the inbound side it is the first answer to the first client. Our builds for devices carry a workaround: a TUN wrapper holds the first Read until IpcSet and Up are done, then returns zero packets once, so that RoutineReadFromTUN loads S4 again before it reads the first real packet.

The fix the open PRs share makes that workaround unnecessary. We turned the hold off and ran two tests of the integration: the first UDP answer of an inbound (S4=25, header protection, random trailers) and one UDP datagram after each of 5 wakes of an outbound (S4=25). UDP is not retried, so a lost packet fails the test.

amneziawg-go Workaround First UDP answer of an inbound One datagram after each of 5 wakes
v3.1.20260828 on 5 of 5 pass 5 of 5 pass
v3.1.20260828 off 0 of 5: the answer never arrives 0 of 5: lost on the first wake
master + #184 off 5 of 5 pass 5 of 5 pass
master + the same approach, our local branch off 5 of 5 pass 5 of 5 pass

(macOS 26 arm64, Go 1.26.4, go test -count=5 per row.)

A test that also covers a live change of S4. It configures each device only once its reader waits in Read, as #184 does, then changes S4 on the running devices while both readers wait, to a larger and then to a smaller value:

// readSignalTUN signals each time the device starts to wait for a packet.
// By then RoutineReadFromTUN has chosen the offset of that packet.
type readSignalTUN struct {
	tun.Device
	reading chan struct{}
}

func (t *readSignalTUN) Read(bufs [][]byte, sizes []int, offset int) (int, error) {
	select {
	case t.reading <- struct{}{}:
	default:
	}
	return t.Device.Read(bufs, sizes, offset)
}

func waitTUNRead(tb testing.TB, t *readSignalTUN) {
	tb.Helper()
	select {
	case <-t.reading:
	case <-time.After(5 * time.Second):
		tb.Fatal("the device did not start to read from its TUN")
	}
}

func TestTransportPaddingChangesWhileTUNReadWaits(t *testing.T) {
	goroutineLeakCheck(t)

	cfg, endpointCfg := genConfigs(t, "s4", "25")
	var pair testPair
	var tuns [2]*readSignalTUN
	for i := range pair {
		p := &pair[i]
		p.tun = tuntest.NewChannelTUN()
		p.ip = netip.AddrFrom4([4]byte{1, 0, 0, byte(i + 1)})
		tuns[i] = &readSignalTUN{Device: p.tun.TUN(), reading: make(chan struct{}, 1)}
		p.dev = NewDevice(tuns[i], conn.NewDefaultBind(), NewLogger(LogLevelError, fmt.Sprintf("dev%d: ", i)))
		t.Cleanup(p.dev.Close)
		waitTUNRead(t, tuns[i])
		if err := p.dev.IpcSet(cfg[i]); err != nil {
			t.Fatalf("failed to configure device %d: %v", i, err)
		}
		if err := p.dev.Up(); err != nil {
			t.Fatalf("failed to bring up device %d: %v", i, err)
		}
		endpointCfg[i^1] = fmt.Sprintf(endpointCfg[i^1], p.dev.net.port)
	}
	for i := range pair {
		if err := pair[i].dev.IpcSet(endpointCfg[i]); err != nil {
			t.Fatalf("failed to configure device endpoint %d: %v", i, err)
		}
	}

	t.Run("startup", func(t *testing.T) {
		pair.Send(t, Ping, nil)
		pair.Send(t, Pong, nil)
	})

	for _, s4 := range []string{"40", "0"} {
		t.Run("s4="+s4, func(t *testing.T) {
			for i := range pair {
				waitTUNRead(t, tuns[i])
			}
			for i := range pair {
				if err := pair[i].dev.IpcSet(uapiCfg("s4", s4)); err != nil {
					t.Fatalf("failed to set s4 on device %d: %v", i, err)
				}
			}
			pair.Send(t, Ping, nil)
			pair.Send(t, Pong, nil)
		})
	}
}

macOS 26 arm64, Go 1.26.4:

Code TestTransportPaddingChangesWhileTUNReadWaits existing TestAWGDevicePing
master (b5928ef) fails 3 of 3; ping and pong are lost in each of startup, s4=40, s4=0 fails 20 of 20, with and without -race
#184 passes 5 of 5
#169 passes 5 of 5
master with the same approach (load S4 again after Read, move the packets or drop what no longer fits) passes 20 of 20, and 10 of 10 under -race passes 20 of 20, with and without -race

One difference between the open PRs. A packet read behind the old offset can be as long as the buffer minus that offset. If S4 grew while Read waited, moving the packet behind the new offset needs a bounds check, or slicing the buffer panics. #169 and #184 drop such a packet; #160 and #170 do not check. The smallest buffer is on iOS, MaxSegmentSize = 1700 bytes (queueconstants_ios.go): there a packet longer than 1700 - 16 - S4 bytes would reach the check. With the usual TUN MTU of 1280-1420 it does not, so this is about a panic that should stay impossible rather than one we have seen.

<!-- gh-comment-id:6087029198 --> @StarProxima commented on GitHub (Oct 9, 2026): Still present on v3.1.20260828 (`master` b5928ef). Adding what we saw in an integration and a test that may help to pick one of the open PRs. **Where it shows.** We use amneziawg-go in the WireGuard proxy of Xray-core, outbound and inbound, with a gVisor netstack TUN, in an app for iOS, Android and macOS. An idle outbound there closes its device and creates a new one on the next connection, so the bug hits every wake rather than once per start: the first TCP SYN arrives a second late after its retransmission, and the first UDP datagram, often a DNS query, is lost. On the inbound side it is the first answer to the first client. Our builds for devices carry a workaround: a TUN wrapper holds the first `Read` until `IpcSet` and `Up` are done, then returns zero packets once, so that `RoutineReadFromTUN` loads S4 again before it reads the first real packet. **The fix the open PRs share makes that workaround unnecessary.** We turned the hold off and ran two tests of the integration: the first UDP answer of an inbound (S4=25, header protection, random trailers) and one UDP datagram after each of 5 wakes of an outbound (S4=25). UDP is not retried, so a lost packet fails the test. | amneziawg-go | Workaround | First UDP answer of an inbound | One datagram after each of 5 wakes | |---|---|---|---| | v3.1.20260828 | on | 5 of 5 pass | 5 of 5 pass | | v3.1.20260828 | off | 0 of 5: the answer never arrives | 0 of 5: lost on the first wake | | `master` + #184 | off | 5 of 5 pass | 5 of 5 pass | | `master` + the same approach, our local branch | off | 5 of 5 pass | 5 of 5 pass | (macOS 26 arm64, Go 1.26.4, `go test -count=5` per row.) **A test that also covers a live change of S4.** It configures each device only once its reader waits in `Read`, as #184 does, then changes S4 on the running devices while both readers wait, to a larger and then to a smaller value: ```go // readSignalTUN signals each time the device starts to wait for a packet. // By then RoutineReadFromTUN has chosen the offset of that packet. type readSignalTUN struct { tun.Device reading chan struct{} } func (t *readSignalTUN) Read(bufs [][]byte, sizes []int, offset int) (int, error) { select { case t.reading <- struct{}{}: default: } return t.Device.Read(bufs, sizes, offset) } func waitTUNRead(tb testing.TB, t *readSignalTUN) { tb.Helper() select { case <-t.reading: case <-time.After(5 * time.Second): tb.Fatal("the device did not start to read from its TUN") } } func TestTransportPaddingChangesWhileTUNReadWaits(t *testing.T) { goroutineLeakCheck(t) cfg, endpointCfg := genConfigs(t, "s4", "25") var pair testPair var tuns [2]*readSignalTUN for i := range pair { p := &pair[i] p.tun = tuntest.NewChannelTUN() p.ip = netip.AddrFrom4([4]byte{1, 0, 0, byte(i + 1)}) tuns[i] = &readSignalTUN{Device: p.tun.TUN(), reading: make(chan struct{}, 1)} p.dev = NewDevice(tuns[i], conn.NewDefaultBind(), NewLogger(LogLevelError, fmt.Sprintf("dev%d: ", i))) t.Cleanup(p.dev.Close) waitTUNRead(t, tuns[i]) if err := p.dev.IpcSet(cfg[i]); err != nil { t.Fatalf("failed to configure device %d: %v", i, err) } if err := p.dev.Up(); err != nil { t.Fatalf("failed to bring up device %d: %v", i, err) } endpointCfg[i^1] = fmt.Sprintf(endpointCfg[i^1], p.dev.net.port) } for i := range pair { if err := pair[i].dev.IpcSet(endpointCfg[i]); err != nil { t.Fatalf("failed to configure device endpoint %d: %v", i, err) } } t.Run("startup", func(t *testing.T) { pair.Send(t, Ping, nil) pair.Send(t, Pong, nil) }) for _, s4 := range []string{"40", "0"} { t.Run("s4="+s4, func(t *testing.T) { for i := range pair { waitTUNRead(t, tuns[i]) } for i := range pair { if err := pair[i].dev.IpcSet(uapiCfg("s4", s4)); err != nil { t.Fatalf("failed to set s4 on device %d: %v", i, err) } } pair.Send(t, Ping, nil) pair.Send(t, Pong, nil) }) } } ``` macOS 26 arm64, Go 1.26.4: | Code | `TestTransportPaddingChangesWhileTUNReadWaits` | existing `TestAWGDevicePing` | |---|---|---| | `master` (b5928ef) | fails 3 of 3; ping and pong are lost in each of `startup`, `s4=40`, `s4=0` | fails 20 of 20, with and without `-race` | | #184 | passes 5 of 5 | | | #169 | passes 5 of 5 | | | `master` with the same approach (load S4 again after `Read`, move the packets or drop what no longer fits) | passes 20 of 20, and 10 of 10 under `-race` | passes 20 of 20, with and without `-race` | **One difference between the open PRs.** A packet read behind the old offset can be as long as the buffer minus that offset. If S4 grew while `Read` waited, moving the packet behind the new offset needs a bounds check, or slicing the buffer panics. #169 and #184 drop such a packet; #160 and #170 do not check. The smallest buffer is on iOS, `MaxSegmentSize = 1700` bytes ([queueconstants_ios.go](https://github.com/amnezia-vpn/amneziawg-go/blob/v3.1.20260828/device/queueconstants_ios.go#L21)): there a packet longer than 1700 - 16 - S4 bytes would reach the check. With the usual TUN MTU of 1280-1420 it does not, so this is about a panic that should stay impossible rather than one we have seen.
Author
Owner

@roman-png commented on GitHub (Oct 14, 2026):

Still present on v3.1.20260828 (b5928ef). Adding a deterministic regression test, since the existing repros fail only 4–11 of 20 runs; it may help to pick one of the open PRs (#169 / #170 / #184 / #160).

The test parks both TUN readers in Read with S4=0 before IpcSet, so RoutineReadFromTUN is guaranteed to hold the stale padding when the first packet arrives:

// device/stale_padding_test.go
func TestTransportPaddingSetAfterReaderStarted(t *testing.T) {
	goroutineLeakCheck(t)
	cfg, endpointCfg := genConfigs(t,
		"jc", "3", "jmin", "40", "jmax", "70",
		"s1", "15", "s2", "18", "s3", "20", "s4", "25",
		"h1", "123456-123500", "h2", "67543-67550",
		"h3", "123123-123200", "h4", "32345-32350",
	)
	binds := bindtest.NewChannelBinds()
	var pair testPair
	for i := range pair {
		p := &pair[i]
		p.tun = tuntest.NewChannelTUN()
		p.ip = netip.AddrFrom4([4]byte{1, 0, 0, byte(i + 1)})
		p.dev = NewDevice(p.tun.TUN(), binds[i], NewLogger(LogLevelError, fmt.Sprintf("dev%d: ", i)))
	}
	time.Sleep(100 * time.Millisecond) // both readers load padding 0 and block in Read
	for i := range pair {
		p := &pair[i]
		if err := p.dev.IpcSet(cfg[i]); err != nil {
			t.Fatalf("configure dev%d: %v", i, err)
		}
		if err := p.dev.Up(); err != nil {
			t.Fatalf("up dev%d: %v", i, err)
		}
		endpointCfg[i^1] = fmt.Sprintf(endpointCfg[i^1], p.dev.net.port)
	}
	for i := range pair {
		p := &pair[i]
		if err := p.dev.IpcSet(endpointCfg[i]); err != nil {
			t.Fatalf("configure endpoint dev%d: %v", i, err)
		}
		t.Cleanup(p.dev.Close)
	}
	t.Run("first packet 1.0.0.1", func(t *testing.T) { pair.Send(t, Ping, nil) })
	t.Run("first packet 1.0.0.2", func(t *testing.T) { pair.Send(t, Pong, nil) })
}

Results (Go 1.26, linux/amd64 and windows/amd64):

tree TestTransportPaddingSetAfterReaderStarted TestAWGDevicePing
v3.1.20260828 fails 3/3 flaky (14/60 failed)
v3.1.20260828 + reload padding after Read (same approach as the open PRs; drop a packet that no longer fits) 20/20 pass 20/20 pass

We carry the fix as a patch in https://github.com/roman-png/vpnpool-engine (patches/amneziawg-go/0004-…) until one of the PRs lands. Happy to adapt the test into whichever PR you prefer.

<!-- gh-comment-id:6177348593 --> @roman-png commented on GitHub (Oct 14, 2026): Still present on v3.1.20260828 (b5928ef). Adding a **deterministic** regression test, since the existing repros fail only 4–11 of 20 runs; it may help to pick one of the open PRs (#169 / #170 / #184 / #160). The test parks both TUN readers in `Read` with `S4=0` *before* `IpcSet`, so `RoutineReadFromTUN` is guaranteed to hold the stale padding when the first packet arrives: ```go // device/stale_padding_test.go func TestTransportPaddingSetAfterReaderStarted(t *testing.T) { goroutineLeakCheck(t) cfg, endpointCfg := genConfigs(t, "jc", "3", "jmin", "40", "jmax", "70", "s1", "15", "s2", "18", "s3", "20", "s4", "25", "h1", "123456-123500", "h2", "67543-67550", "h3", "123123-123200", "h4", "32345-32350", ) binds := bindtest.NewChannelBinds() var pair testPair for i := range pair { p := &pair[i] p.tun = tuntest.NewChannelTUN() p.ip = netip.AddrFrom4([4]byte{1, 0, 0, byte(i + 1)}) p.dev = NewDevice(p.tun.TUN(), binds[i], NewLogger(LogLevelError, fmt.Sprintf("dev%d: ", i))) } time.Sleep(100 * time.Millisecond) // both readers load padding 0 and block in Read for i := range pair { p := &pair[i] if err := p.dev.IpcSet(cfg[i]); err != nil { t.Fatalf("configure dev%d: %v", i, err) } if err := p.dev.Up(); err != nil { t.Fatalf("up dev%d: %v", i, err) } endpointCfg[i^1] = fmt.Sprintf(endpointCfg[i^1], p.dev.net.port) } for i := range pair { p := &pair[i] if err := p.dev.IpcSet(endpointCfg[i]); err != nil { t.Fatalf("configure endpoint dev%d: %v", i, err) } t.Cleanup(p.dev.Close) } t.Run("first packet 1.0.0.1", func(t *testing.T) { pair.Send(t, Ping, nil) }) t.Run("first packet 1.0.0.2", func(t *testing.T) { pair.Send(t, Pong, nil) }) } ``` Results (Go 1.26, linux/amd64 and windows/amd64): | tree | `TestTransportPaddingSetAfterReaderStarted` | `TestAWGDevicePing` | |---|---|---| | v3.1.20260828 | fails 3/3 | flaky (14/60 failed) | | v3.1.20260828 + reload padding after `Read` (same approach as the open PRs; drop a packet that no longer fits) | 20/20 pass | 20/20 pass | We carry the fix as a patch in https://github.com/roman-png/vpnpool-engine (`patches/amneziawg-go/0004-…`) until one of the PRs lands. Happy to adapt the test into whichever PR you prefer.
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
amnezia-vpn/amneziawg-go#83
No description provided.