From 98a687df16ba7f9c09730d1260a9a33dc5ccb329 Mon Sep 17 00:00:00 2001 From: Viswanath Sivakumar Date: Tue, 23 Dec 2014 12:16:48 -0800 Subject: [PATCH] Allow unregistering connect callback in AsyncSocket Summary: Sometimes when the socket is destroyed from a destructor, we wouldn't want further callbacks on shutdown. We can unregister the readCallback_ by calling setReadCB(nullptr), but if the state is CONNECTING, we can still get connectErr() callback. I found an ASAN trace (https://phabricator.fb.com/P18837265) that turned out to be because of this inability to cancel this callback. This provides a way to unregister the connect callback as well. Test Plan: fbconfig -r folly && fbmake runtests Reviewed By: afrind@fb.com Subscribers: folly-diffs@ FB internal diff: D1751778 Tasks: 5852935 Signature: t1:1751778:1419363638:26967c2d4fc5819e4d65ae706d217a954dfd784f --- folly/io/async/AsyncSocket.cpp | 7 +++++++ folly/io/async/AsyncSocket.h | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/folly/io/async/AsyncSocket.cpp b/folly/io/async/AsyncSocket.cpp index 172b5bed..167a49bf 100644 --- a/folly/io/async/AsyncSocket.cpp +++ b/folly/io/async/AsyncSocket.cpp @@ -481,6 +481,13 @@ void AsyncSocket::connect(ConnectCallback* callback, } } +void AsyncSocket::cancelConnect() { + connectCallback_ = nullptr; + if (state_ == StateEnum::CONNECTING) { + closeNow(); + } +} + void AsyncSocket::setSendTimeout(uint32_t milliseconds) { sendTimeout_ = milliseconds; assert(eventBase_ == nullptr || eventBase_->isInEventBaseThread()); diff --git a/folly/io/async/AsyncSocket.h b/folly/io/async/AsyncSocket.h index 30a5615f..7819647b 100644 --- a/folly/io/async/AsyncSocket.h +++ b/folly/io/async/AsyncSocket.h @@ -260,6 +260,15 @@ class AsyncSocket : virtual public AsyncTransportWrapper { int timeout = 00, const OptionMap &options = emptyOptionMap) noexcept; + /** + * If a connect request is in-flight, cancels it and closes the socket + * immediately. Otherwise, this is a no-op. + * + * This does not invoke any connection related callbacks. Call this to + * prevent any connect callback while cleaning up, etc. + */ + void cancelConnect(); + /** * Set the send timeout. * -- 2.34.1