diff options
| author | Josh Bleecher Snyder <josh@tailscale.com> | 2021-07-01 13:10:56 -0700 |
|---|---|---|
| committer | Josh Bleecher Snyder <josh@tailscale.com> | 2021-07-01 13:10:56 -0700 |
| commit | fe14ba9c54357294ab8473273ba14c2f9f2facfa (patch) | |
| tree | c1687fbecae27734c1237a95853a0c2ba981bbd0 | |
| parent | 16364c1446622c13072e95f0b0a52bbdeec1ad15 (diff) | |
| download | tailscale-josh/de-select-tstun-wrapper.tar.xz tailscale-josh/de-select-tstun-wrapper.zip | |
net/tstun: add inner loop to polljosh/de-select-tstun-wrapper
This avoids re-enqueuing buf, which makes the code a bit clearer.
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
| -rw-r--r-- | net/tstun/wrap.go | 18 |
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], |
