From 979c8b746b56b8e68acf47b5af2eecf834043b50 Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Thu, 27 Apr 2017 16:21:46 +0300 Subject: QCFSocketNotifier: fix registering a source in the run loop Even if a callback type is not automatically re-enabled, callbacks are implicitly enabled when the source has been added to the run loop. In this case, calling CFSocketEnableCallBacks() could produce an extra notification if there is a pending event in the queue. The bug is quite unstable and completely depends on the internal OS delays. So, it can't be tested inside Qt. Task-number: QTBUG-59930 Change-Id: I751b8b8cf99cb86b80055f2214a42a638f01abe4 Reviewed-by: Timur Pocheptsov --- src/corelib/kernel/qcfsocketnotifier.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/corelib/kernel') diff --git a/src/corelib/kernel/qcfsocketnotifier.cpp b/src/corelib/kernel/qcfsocketnotifier.cpp index a079031e96..1fee2aa5fc 100644 --- a/src/corelib/kernel/qcfsocketnotifier.cpp +++ b/src/corelib/kernel/qcfsocketnotifier.cpp @@ -292,10 +292,19 @@ void QCFSocketNotifier::enableSocketNotifiers(CFRunLoopObserverRef ref, CFRunLoo continue; } - if (!socketInfo->readNotifier) + // Apple docs say: "If a callback is automatically re-enabled, + // it is called every time the condition becomes true ... If a + // callback is not automatically re-enabled, then it gets called + // exactly once, and is not called again until you manually + // re-enable that callback by calling CFSocketEnableCallBacks()". + // So, we don't need to enable callbacks on registering. + socketInfo->readEnabled = (socketInfo->readNotifier != nullptr); + if (!socketInfo->readEnabled) CFSocketDisableCallBacks(socketInfo->socket, kCFSocketReadCallBack); - if (!socketInfo->writeNotifier) + socketInfo->writeEnabled = (socketInfo->writeNotifier != nullptr); + if (!socketInfo->writeEnabled) CFSocketDisableCallBacks(socketInfo->socket, kCFSocketWriteCallBack); + continue; } if (socketInfo->readNotifier && !socketInfo->readEnabled) { -- cgit v1.2.3