From 532b8c01aa05334af612233137a836bfe45c3c78 Mon Sep 17 00:00:00 2001 From: Maxim Georgiev Date: Mon, 17 Apr 2017 12:04:17 -0700 Subject: [PATCH] Allow error message callback cancellation regardless of socket state. Summary: We should be able to reset error message callback in AsyncSocket evein if the socket is closed yet. It's common to keep callback installed while the socket is connected. Once the socket is closed, the deinitialization process starts. If the callback callee component gets deallocated before the socket object is deallocated, it should be able to cancel callbacks. Reviewed By: yfeldblum Differential Revision: D4897335 fbshipit-source-id: 8eee26f9ebcb78a01d55598be3aff6595a3ed852 --- folly/io/async/AsyncSocket.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/folly/io/async/AsyncSocket.cpp b/folly/io/async/AsyncSocket.cpp index 013813f9..17040bf9 100644 --- a/folly/io/async/AsyncSocket.cpp +++ b/folly/io/async/AsyncSocket.cpp @@ -612,7 +612,7 @@ void AsyncSocket::setErrMessageCB(ErrMessageCallback* callback) { << ", fd=" << fd_ << ", callback=" << callback << ", state=" << state_; - // Short circuit if callback is the same as the existing timestampCallback_. + // Short circuit if callback is the same as the existing errMessageCallback_. if (callback == errMessageCallback_) { return; } @@ -625,6 +625,14 @@ void AsyncSocket::setErrMessageCB(ErrMessageCallback* callback) { DestructorGuard dg(this); assert(eventBase_->isInEventBaseThread()); + if (callback == nullptr) { + // We should be able to reset the callback regardless of the + // socket state. It's important to have a reliable callback + // cancellation mechanism. + errMessageCallback_ = callback; + return; + } + switch ((StateEnum)state_) { case StateEnum::CONNECTING: case StateEnum::FAST_OPEN: -- 2.34.1