From bab32c332a316b07d4578fb80c04ce5595ebecfa Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Wed, 4 Apr 2018 18:40:46 -0700 Subject: [PATCH] codec: notes on thread self dtor --- src/remote/codec.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/remote/codec.cpp b/src/remote/codec.cpp index 6656d9f..8efa03c 100644 --- a/src/remote/codec.cpp +++ b/src/remote/codec.cpp @@ -1126,13 +1126,23 @@ void BlockingTCPTransportCodec::start() { void BlockingTCPTransportCodec::receiveThread() { - Transport::shared_pointer ptr = this->shared_from_this(); + /* This innocuous ref. is an important hack. + * The code behind Transport::close() will cause + * channels and operations to drop references + * to this transport. This ref. keeps it from + * being destroyed way down the call stack, from + * which it is apparently not possible to return + * safely. Rather than try to untangle this + * knot, just keep this ref... + */ + Transport::shared_pointer ptr(this->shared_from_this()); while (this->isOpen()) { try { this->processRead(); } catch (std::exception &e) { + PRINT_EXCEPTION(e); LOG(logLevelError, "an exception caught while in receiveThread at %s:%d: %s", __FILE__, __LINE__, e.what()); @@ -1147,7 +1157,8 @@ void BlockingTCPTransportCodec::receiveThread() void BlockingTCPTransportCodec::sendThread() { - Transport::shared_pointer ptr = this->shared_from_this(); + // cf. the comment in receiveThread() + Transport::shared_pointer ptr(this->shared_from_this()); this->setSenderThread(); @@ -1158,6 +1169,7 @@ void BlockingTCPTransportCodec::sendThread() } catch (connection_closed_exception &cce) { // noop } catch (std::exception &e) { + PRINT_EXCEPTION(e); LOG(logLevelWarn, "an exception caught while in sendThread at %s:%d: %s", __FILE__, __LINE__, e.what());