]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
device: do not attach finalizer to non-returned object
authorJason A. Donenfeld <Jason@zx2c4.com>
Tue, 9 Feb 2021 14:09:50 +0000 (15:09 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 9 Feb 2021 14:37:04 +0000 (15:37 +0100)
Before, the code attached a finalizer to an object that wasn't returned,
resulting in immediate garbage collection. Instead return the actual
pointer.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
device/channels.go
device/peer.go
device/pools_test.go
device/receive.go
device/send.go

index 4bd60909792f2254d9e4e26ad95410e72715cba6..1e3e2062339e48004ced71dc89ff9289ba431971 100644 (file)
@@ -71,14 +71,15 @@ func newHandshakeQueue() *handshakeQueue {
        return q
 }
 
+type autodrainingInboundQueue struct {
+       c chan *QueueInboundElement
+}
+
 // newAutodrainingInboundQueue returns a channel that will be drained when it gets GC'd.
 // It is useful in cases in which is it hard to manage the lifetime of the channel.
 // The returned channel must not be closed. Senders should signal shutdown using
 // some other means, such as sending a sentinel nil values.
-func newAutodrainingInboundQueue(device *Device) chan *QueueInboundElement {
-       type autodrainingInboundQueue struct {
-               c chan *QueueInboundElement
-       }
+func newAutodrainingInboundQueue(device *Device) *autodrainingInboundQueue {
        q := &autodrainingInboundQueue{
                c: make(chan *QueueInboundElement, QueueInboundSize),
        }
@@ -97,7 +98,11 @@ func newAutodrainingInboundQueue(device *Device) chan *QueueInboundElement {
                        }
                }
        })
-       return q.c
+       return q
+}
+
+type autodrainingOutboundQueue struct {
+       c chan *QueueOutboundElement
 }
 
 // newAutodrainingOutboundQueue returns a channel that will be drained when it gets GC'd.
@@ -105,10 +110,7 @@ func newAutodrainingInboundQueue(device *Device) chan *QueueInboundElement {
 // The returned channel must not be closed. Senders should signal shutdown using
 // some other means, such as sending a sentinel nil values.
 // All sends to the channel must be best-effort, because there may be no receivers.
-func newAutodrainingOutboundQueue(device *Device) chan *QueueOutboundElement {
-       type autodrainingOutboundQueue struct {
-               c chan *QueueOutboundElement
-       }
+func newAutodrainingOutboundQueue(device *Device) *autodrainingOutboundQueue {
        q := &autodrainingOutboundQueue{
                c: make(chan *QueueOutboundElement, QueueOutboundSize),
        }
@@ -127,5 +129,5 @@ func newAutodrainingOutboundQueue(device *Device) chan *QueueOutboundElement {
                        }
                }
        })
-       return q.c
+       return q
 }
index 49b9acb6b73e2babed17a0071c17c4299d9bebd6..69238a67d1bb81f812106a2391b7ce8d6f787bbc 100644 (file)
@@ -57,8 +57,8 @@ type Peer struct {
 
        queue struct {
                staged   chan *QueueOutboundElement // staged packets before a handshake is available
-               outbound chan *QueueOutboundElement // sequential ordering of udp transmission
-               inbound  chan *QueueInboundElement  // sequential ordering of tun writing
+               outbound *autodrainingOutboundQueue // sequential ordering of udp transmission
+               inbound  *autodrainingInboundQueue  // sequential ordering of tun writing
        }
 
        cookieGenerator CookieGenerator
@@ -253,8 +253,8 @@ func (peer *Peer) Stop() {
 
        peer.timersStop()
        // Signal that RoutineSequentialSender and RoutineSequentialReceiver should exit.
-       peer.queue.inbound <- nil
-       peer.queue.outbound <- nil
+       peer.queue.inbound.c <- nil
+       peer.queue.outbound.c <- nil
        peer.stopping.Wait()
        peer.device.queue.encryption.wg.Done() // no more writes to encryption queue from us
 
index 6caf7e7f1df9ba7641dcbec15630320b2a5615aa..a27ccc0e0ff8707971ac5642542b3294c43a83e1 100644 (file)
@@ -80,4 +80,4 @@ func BenchmarkWaitPool(b *testing.B) {
                }()
        }
        wg.Wait()
-}
\ No newline at end of file
+}
index 7acb7d9f281184c7938f7870a7ec326067970876..3fc6831e8a7ae16c6ef5ad1fcef388307f5d4d43 100644 (file)
@@ -167,7 +167,7 @@ func (device *Device) RoutineReceiveIncoming(IP int, bind conn.Bind) {
 
                        // add to decryption queues
                        if peer.isRunning.Get() {
-                               peer.queue.inbound <- elem
+                               peer.queue.inbound.c <- elem
                                device.queue.decryption.c <- elem
                                buffer = device.GetMessageBuffer()
                        } else {
@@ -402,7 +402,7 @@ func (peer *Peer) RoutineSequentialReceiver() {
        }()
        device.log.Verbosef("%v - Routine: sequential receiver - started", peer)
 
-       for elem := range peer.queue.inbound {
+       for elem := range peer.queue.inbound.c {
                if elem == nil {
                        return
                }
@@ -477,7 +477,7 @@ func (peer *Peer) RoutineSequentialReceiver() {
                if err != nil && !device.isClosed() {
                        device.log.Errorf("Failed to write packet to TUN device: %v", err)
                }
-               if len(peer.queue.inbound) == 0 {
+               if len(peer.queue.inbound.c) == 0 {
                        err = device.tun.device.Flush()
                        if err != nil {
                                peer.device.log.Errorf("Unable to flush packets: %v", err)
index 911ee5caa388ec2ca2b129bcc11ff374bec8bb8a..783e5b9100c6faf3bc6fcd3dfa6ce07a4125bc5c 100644 (file)
@@ -317,7 +317,7 @@ top:
 
                        // add to parallel and sequential queue
                        if peer.isRunning.Get() {
-                               peer.queue.outbound <- elem
+                               peer.queue.outbound.c <- elem
                                peer.device.queue.encryption.c <- elem
                        } else {
                                peer.device.PutMessageBuffer(elem.buffer)
@@ -410,7 +410,7 @@ func (peer *Peer) RoutineSequentialSender() {
        }()
        device.log.Verbosef("%v - Routine: sequential sender - started", peer)
 
-       for elem := range peer.queue.outbound {
+       for elem := range peer.queue.outbound.c {
                if elem == nil {
                        return
                }