From 6cd2f5ce49301e2a4abece4e896fc556948d71bc Mon Sep 17 00:00:00 2001 From: Sjors Gielen Date: Sat, 17 Jun 2017 18:43:08 +0200 Subject: [PATCH] When uncaught exceptions occur in the networkd threads, handle them. --- userland/networkd/client.cpp | 5 ++++- userland/networkd/interface.cpp | 11 +++++++++-- userland/networkd/ip_socket.cpp | 14 +++++++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/userland/networkd/client.cpp b/userland/networkd/client.cpp index bfee897..3f59d39 100644 --- a/userland/networkd/client.cpp +++ b/userland/networkd/client.cpp @@ -75,7 +75,7 @@ bool client::send_error(std::string error) { } void client::run() { - while(1) { + while(1) try { char buf[200]; // TODO: set non-blocking flag once kernel supports it // this way, we can read until EOF instead of only 200 bytes @@ -472,5 +472,8 @@ void client::run() { return; } } + } catch(std::exception &e) { + dprintf(logfd, "*** Uncaught exception in client interface with fd %d\n", fd); + dprintf(logfd, "*** Error: \"%s\"\n", e.what()); } } diff --git a/userland/networkd/interface.cpp b/userland/networkd/interface.cpp index 4360bb0..2ca4ee5 100644 --- a/userland/networkd/interface.cpp +++ b/userland/networkd/interface.cpp @@ -34,8 +34,15 @@ interface::~interface() void interface::start() { assert(rawsock >= 0); - thr = std::thread([this](){ - run(); + auto that = shared_from_this(); + thr = std::thread([that](){ + try { + that->run(); + } catch(std::exception &e) { + dprintf(0, "*** Uncaught exception in handling incoming data on %s interface %s\n", + that->hwtype.c_str(), that->name.c_str()); + dprintf(0, "*** Error: \"%s\"\n", e.what()); + } }); } diff --git a/userland/networkd/ip_socket.cpp b/userland/networkd/ip_socket.cpp index 3fc6699..9b74a6f 100644 --- a/userland/networkd/ip_socket.cpp +++ b/userland/networkd/ip_socket.cpp @@ -1,4 +1,5 @@ #include "ip_socket.hpp" +#include "util.hpp" #include #include @@ -34,7 +35,18 @@ void ip_socket::start() void ip_socket::run() { - handle_requests(reversefd, this); + try { + handle_requests(reversefd, this); + } catch(std::exception &e) { + dprintf(0, "*** handle_requests threw an exception in ip_socket\n"); + dprintf(0, "*** error: \"%s\"\n", e.what()); + dprintf(0, "*** proto: %d\n", proto); + std::string ipd = ipv4_ntop(local_ip); + dprintf(0, "*** local bind: %s:%d\n", ipd.c_str(), local_port); + ipd = ipv4_ntop(peer_ip); + dprintf(0, "*** remote bind: %s:%d\n", ipd.c_str(), peer_port); + dprintf(0, "*** pseudo: %llu / reverse: %d\n", pseudofd, reversefd); + } } void ip_socket::stat_fget(cosix::pseudofd_t, cloudabi_filestat_t *buf)