From 8a3a71e045c38e0a4e8cf70794900190ca9f9ce3 Mon Sep 17 00:00:00 2001
From: Ben Cartwright-Cox <ben@benjojo.co.uk>
Date: Fri, 24 Feb 2023 14:13:45 +0000
Subject: Ensure error PDUs are sent before the TCP socket closes

This was intro'd in 6e4c533e8a08fab823a30d236dee661a447c9952
Since I did not expect the sending PDU channel to be _slightly_
slower than just yeeting the socket closed instantly.

Regardless, TCP disconnection now happens when the sendloop is dead,
that should allow for Error PDUs etc to be sent out before

Tag: https://github.com/bgp/stayrtr/issues/90

diff --git a/lib/server.go b/lib/server.go
index 4739ffa..a9a6f08 100644
--- a/lib/server.go
+++ b/lib/server.go
@@ -807,6 +807,8 @@ func (c *Client) passSimpleHandler(pdu PDU) {
 }
 
 func (c *Client) sendLoop() {
+	defer c.tcpconn.Close()
+
 	for c.connected {
 		select {
 		case pdu := <-c.transmits:
@@ -1196,6 +1198,4 @@ func (c *Client) Disconnect() {
 	case c.quit <- true:
 	default:
 	}
-
-	c.tcpconn.Close()
 }
