summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--net/tstun/wrap.go18
1 files changed, 12 insertions, 6 deletions
diff --git a/net/tstun/wrap.go b/net/tstun/wrap.go
index a5ca42afa..594192f03 100644
--- a/net/tstun/wrap.go
+++ b/net/tstun/wrap.go
@@ -290,12 +290,18 @@ func (t *Wrapper) poll() {
// This is the rationale behind the tun.Wrapper.{Read,Write} interfaces
// and the reason t.buffer has size MaxMessageSize and not MaxContentSize.
// In principle, read errors are not fatal (but wireguard-go disagrees).
- n, err := t.tdev.Read(buf, PacketStartOffset)
- // Wireguard will skip an empty read,
- // so we might as well do it here to avoid the send through t.outbound.
- if n == 0 && err == nil {
- t.bufferC <- buf
- continue
+ //
+ // We loop here until we get a non-empty (or failed) read.
+ // We don't need this loop for correctness,
+ // but wireguard-go will skip an empty read,
+ // so we might as well avoid the send through t.outbound.
+ var n int
+ var err error
+ for n == 0 && err == nil {
+ if t.closed() {
+ return
+ }
+ n, err = t.tdev.Read(buf, PacketStartOffset)
}
t.outbound <- tunReadResult{
data: buf[PacketStartOffset : PacketStartOffset+n],