summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Long <willcode4@gmail.com>2018-01-31 21:42:22 -0500
committerMartin Braun <martin.braun@ettus.com>2018-02-03 15:21:37 +0100
commit8d04b4a7aef188a25a07ed6f41a280aaacf328c3 (patch)
treec5a19514a8421c6605d8fc9ebc56df295d704184
parentb163b242e06c9f714b05f57f7180c760f021cbb6 (diff)
socket_pdu_impl: garbage collect closed tcp_connections
This patch addresses two problems. First tcp_connections were not being closed, leading to a bug report about socket exhaustion [1]. Second, socket_pdu_impl tracks tcp_connections in a vector so that it can broadcast outgoing data to clients. Items in this vector were never freed. This patch does a shutdown and close on the tcp_connection when there is a read failure (signaling a socket close). On each new accept, the tcp_connection list in socket_pdu_impl is scanned for closed connections, which are then removed. Possible improvements: - Make sure the read error is eof. - Check sends as well as reads. More direct signalling could be done between tcp_connection and socket_pdu_impl, but that would require an API change. No change is required, here. [1] https://github.com/gnuradio/gnuradio/issues/1568
-rw-r--r--gr-blocks/lib/socket_pdu_impl.cc9
-rw-r--r--gr-blocks/lib/tcp_connection.cc4
2 files changed, 13 insertions, 0 deletions
diff --git a/gr-blocks/lib/socket_pdu_impl.cc b/gr-blocks/lib/socket_pdu_impl.cc
index cc45557bca..efb778f404 100644
--- a/gr-blocks/lib/socket_pdu_impl.cc
+++ b/gr-blocks/lib/socket_pdu_impl.cc
@@ -184,6 +184,15 @@ namespace gr {
socket_pdu_impl::handle_tcp_accept(tcp_connection::sptr new_connection, const boost::system::error_code& error)
{
if (!error) {
+ // Garbage collect closed sockets
+ std::vector<tcp_connection::sptr>::iterator it = d_tcp_connections.begin();
+ while(it != d_tcp_connections.end()) {
+ if (! (**it).socket().is_open())
+ it = d_tcp_connections.erase(it);
+ else
+ ++it;
+ }
+
new_connection->start(this);
d_tcp_connections.push_back(new_connection);
start_tcp_accept();
diff --git a/gr-blocks/lib/tcp_connection.cc b/gr-blocks/lib/tcp_connection.cc
index b28accccf7..200e82b89f 100644
--- a/gr-blocks/lib/tcp_connection.cc
+++ b/gr-blocks/lib/tcp_connection.cc
@@ -102,6 +102,10 @@ namespace gr {
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
+ else {
+ d_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
+ d_socket.close();
+ }
}
} /* namespace blocks */
}/* namespace gr */