From: Maxim Georgiev Date: Mon, 17 Apr 2017 19:04:17 +0000 (-0700) Subject: Allow error message callback cancellation regardless of socket state. X-Git-Tag: v2017.04.24.00~16 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=532b8c01aa05334af612233137a836bfe45c3c78;ds=sidebyside 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 --- 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: