macOS client stops answering the server after failed connects: unblock pipe is stored on the wrong thread (fd leak, then SocketMultiplexer wedge)
Sanity checks
- I have done the sanity checks, and my issue persists
- These sanity checks are not relevant to the bug
Root-caused in source; current master (8cf0b71) has the same code.
Deskflow version info
Deskflow: 1.26.0.0 (v1.26.0 release)
Qt: 6.10.1
System: macOS 27.0.1 (26A434), Apple SiliconBuild type
Deskflow package (downloaded from Deskflow). Also reproduced with a local build of v1.26.0 against Qt 6.10.1.
Operating systems (OS)
- Windows
- macOS
- Linux (X11)
- Linux (Wayland)
- BSD-derived
- Other (please specify)
Deskflow configuration
- Windows server, macOS 27.0.1 client
- TLS off, Barrier protocol
- The bug is in the client's socket code; the server is not involved
What steps will reproduce the problem?
What happened:
- For about 13 hours after upgrading to macOS 27, the client retried once per second. macOS logged a Local Network check for it about once per second (
UserEventAgent: LocalNetwork: found bundle id org.deskflow.deskflow, roughly 595 per 10 minutes). With Local Network access not in effect (see #9451),connect()fails immediately withEHOSTUNREACH. TheHost is downretries in the logs on #8653 go down the same synchronous-failure path. - At some point, access went through again.
- From then on, the client connected, received the server's hello, and never replied. The server dropped the connection after its 30 s hello timeout, and the client reconnected 1 s later. This repeated until
deskflow-corewas restarted.
Deterministic repro with the stock 1.26.0 binary:
- Build a small
DYLD_INSERT_LIBRARIESshim. It interposesconnect()and returnsEHOSTUNREACHwhile a marker file exists, which is what a denied Local Network permission does. - Run a minimal fake server on 127.0.0.1:24899. It sends the Barrier 1.8 hello and times the reply.
- Run
deskflow-core client --new-instance -s test.confunderulimit -n 64, withremoteHost=127.0.0.1andport=24899. - Wait 40 s, then delete the marker file.
| stock 1.26.0 | with the fix below | |
|---|---|---|
| pipe fds while connects fail (40 s) | +2 per retry, until pipe() fails with EMFILE |
2 (one pair), flat |
| hello-back once connects succeed | never; the server times out | 0.8 ms |
| event thread | Client::handleHello → SocketMultiplexer::addSocket → waitCondVar |
idle in EventQueue::getEvent |
I can post the shim and the fake server if useful. Each is about 30 lines.
Log output
# the wedged client, after about 15 h
$ lsof -p $(pgrep -x deskflow-core) | grep -c PIPE
4863 # 2,430 pipe pairs; fds 0..4863 all in use; count flat across reconnects
# connection: rx 15 bytes (the hello), tx 0 bytes; reconnects every 31 s
# sample: event thread, in every sample
App::runEventsLoop → EventQueue::dispatchEvent → Client::handleHello()
→ ProtocolUtil::writef → PacketStreamFilter::write → TCPSocket::write
→ SocketMultiplexer::addSocket (+84, the inlined lockJobList() wait)
→ ArchMultithreadPosix::waitCondVar
# multiplexer thread, in every sample
SocketMultiplexer::serviceThread → ArchNetworkBSD::pollSocket → poll
# repro client log while connect() is failing (once per second)
WARNING: failed to connect to server: cannot connect socket: No route to hostAdditional information
Root cause. ArchNetworkBSD::getUnblockPipeForThread(thread) creates the unblock pipe on demand. It then stores it with setNetworkDataForCurrentThread(), which puts it on the calling thread, not on thread:
https://github.com/deskflow/deskflow/blob/v1.26.0/src/lib/arch/unix/ArchNetworkBSD.cpp#L697-L717
unblockPollSocket() calls it for another thread: SocketMultiplexer::addSocket() and removeSocket() pass the service thread. If the service thread has not polled yet, its slot is still empty. Each call then creates a new pipe, stores it on the caller (dropping the previous one), and writes the wakeup byte into a pipe that nothing polls.
This is usually harmless, because the service thread creates its own pipe on its first poll. But while connects fail synchronously, no job is ever added and the service thread never polls. (TCPSocket::connect() throws before setJob().) Every retry's TCPSocket::close() → removeSocket() then leaks another pipe pair, 2 fds per second, until the process runs out of descriptors.
Once connects succeed, the service thread's first pollSocket() cannot create its pipe (EMFILE). It then polls with no wakeup fd and an infinite timeout, while holding the job list. The event thread's next addSocket() is the hello-back write in Client::handleHello(), and it waits for the job list forever. Nothing can wake the poll until the peer sends data, and the server is waiting for our hello. The state is permanent until restart.
Each leaked pipe also leaks its new int[2].
Fix. Store the pipe on the target thread, and create it under a lock so the polling thread and an unblocking thread can't both create one. Close the pipe if setting it up throws. setNetworkDataForCurrentThread() becomes setNetworkDataForThread(). As a side effect, this also drops the find() reference the old function took and never released. The source change applies cleanly to master 8cf0b71.
--- a/src/lib/arch/unix/ArchNetworkBSD.cpp
+++ b/src/lib/arch/unix/ArchNetworkBSD.cpp
@@ const int *ArchNetworkBSD::getUnblockPipeForThread(ArchThread thread)
{
+ // unblockPollSocket() calls this for a thread other than the caller, possibly
+ // before that thread has polled for the first time. the pipe must be stored
+ // on the thread that polls it, otherwise the wakeup is written to a pipe that
+ // is never polled and a new pipe is leaked on every call. the lock stops the
+ // polling thread and an unblocking thread from each creating a pipe.
+ std::scoped_lock lock{m_unblockPipeMutex};
ArchMultithreadPosix *mt = ArchMultithreadPosix::getInstance();
auto *unblockPipe = static_cast<int *>(mt->getNetworkDataForThread(thread));
if (unblockPipe == nullptr) {
unblockPipe = new int[2];
if (pipe(unblockPipe) != -1) {
try {
setBlockingOnSocket(unblockPipe[0], false);
- mt->setNetworkDataForCurrentThread(unblockPipe);
+ mt->setNetworkDataForThread(thread, unblockPipe);
} catch (...) {
+ close(unblockPipe[0]);
+ close(unblockPipe[1]);
delete[] unblockPipe;
--- a/src/lib/arch/unix/ArchNetworkBSD.h
+++ b/src/lib/arch/unix/ArchNetworkBSD.h
std::mutex m_mutex;
+ std::mutex m_unblockPipeMutex;
--- a/src/lib/arch/unix/ArchMultithreadPosix.h
+++ b/src/lib/arch/unix/ArchMultithreadPosix.h
- void setNetworkDataForCurrentThread(void *);
+ void setNetworkDataForThread(ArchThread, void *);
--- a/src/lib/arch/unix/ArchMultithreadPosix.cpp
+++ b/src/lib/arch/unix/ArchMultithreadPosix.cpp
-void ArchMultithreadPosix::setNetworkDataForCurrentThread(void *data)
+void ArchMultithreadPosix::setNetworkDataForThread(ArchThread thread, void *data)
{
std::scoped_lock lock{m_threadMutex};
- ArchThreadImpl *thread = find(pthread_self());
thread->m_networkData = data;
}Test. A regression test in ArchNetworkBSDTests (v1.26.0 layout) unblocks a thread that has not polled yet. It then checks that the pipe landed on that thread, that the caller's slot is unchanged, and that the wakeup byte is readable. It fails on 1.26.0 and passes with the fix. The full legacy suite and all 23 unit-test binaries pass on macOS arm64 with Qt 6.10.1.
Related: #9451 (the Local Network failures that trigger this on macOS 26/27) and #8653 (same 1 Hz synchronous-failure retry loop in its logs). I can turn this into a PR if you'd like.
Source: deskflow/deskflow