From 73d3af591adb14c60114f1057aa8f1b427daa3c8 Mon Sep 17 00:00:00 2001 From: Hongyi Jia Date: Tue, 24 Mar 2020 00:10:37 -0700 Subject: [PATCH] Catch exception from shm connection start() Summary: util::ringbuffer::shm::create(kDefaultSize) could throw system exception and cause connection destruction before initilization completes. If not catch the exception on connection level, destruction will clean up unregistered resources to cause unrelated errors. Differential Revision: D20616906 fbshipit-source-id: 3b91458e26cca2e6bd393264ad1ca6dea4fb7b29 --- tensorpipe/transport/shm/connection.cc | 14 ++++++++++++-- tensorpipe/transport/shm/connection.h | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tensorpipe/transport/shm/connection.cc b/tensorpipe/transport/shm/connection.cc index 8e6b23afd..b619cb7db 100644 --- a/tensorpipe/transport/shm/connection.cc +++ b/tensorpipe/transport/shm/connection.cc @@ -50,8 +50,15 @@ Connection::~Connection() { void Connection::start() { // Create ringbuffer for inbox. std::shared_ptr inboxRingBuffer; - std::tie(inboxHeaderFd_, inboxDataFd_, inboxRingBuffer) = - util::ringbuffer::shm::create(kDefaultSize); + try { + std::tie(inboxHeaderFd_, inboxDataFd_, inboxRingBuffer) = + util::ringbuffer::shm::create(kDefaultSize); + } catch (std::system_error& e) { + state_ = INITIALIZING_ERROR; + // Triggers destructor. + TP_THROW_SYSTEM(errno) << "Error while creating shm with " << kDefaultSize + << " bytes"; + } inbox_.emplace(std::move(inboxRingBuffer)); // Register method to be called when our peer writes to our inbox. @@ -429,6 +436,9 @@ void Connection::close() { // can't extend its lifetime by capturing a shared_ptr and increasing its // refcount. std::unique_lock guard(mutex_); + if (state_ == INITIALIZING_ERROR) { + return; + } closeHoldingMutex(); } diff --git a/tensorpipe/transport/shm/connection.h b/tensorpipe/transport/shm/connection.h index d012b276f..c06d43318 100644 --- a/tensorpipe/transport/shm/connection.h +++ b/tensorpipe/transport/shm/connection.h @@ -36,6 +36,7 @@ class Connection final : public transport::Connection, enum State { INITIALIZING = 1, + INITIALIZING_ERROR, SEND_FDS, RECV_FDS, ESTABLISHED,