From 6cb85c405622dd54187091a6b653b7744fdd9eb8 Mon Sep 17 00:00:00 2001 From: westpacsailor <114281758+westpacsailor@users.noreply.github.com> Date: Sun, 22 Nov 2026 17:03:11 -0600 Subject: [PATCH] Fix crash when disconnecting with a rigctld client connected Disconnecting from the radio while WSJT-X was connected through wfview's rigctld crashed wfview (access violation in a rigCaps command lookup, called from a rigctld socket read). The rig owns its capabilities, and its destructor announces that they are gone with queue->setRigCaps(nullptr), but from the rig thread. Each rigctld client lives on the main thread, so it only got the queued "rig has gone away" call after disconnectRig() had stopped the rig thread and freed the rig. A rigctld request handled in between (WSJT-X polls about once a second) used the freed capabilities. Add releaseRigCapsBeforeRigDeletion(), called in disconnectRig() and in receivePortError()'s teardown before the rig is closed and its thread stopped. It drops the receivers' queued slot calls, clears the capabilities from the main thread so main-thread listeners (rigctld clients, MainController) drop them at once, and waits for the TCI server and USB controller threads to process the change. The rigctld client then closes its socket; WSJT-X reconnects with Retry. Co-Authored-By: Claude Opus 5.5 --- include/MainController.h | 1 + src/MainController.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/include/MainController.h b/include/MainController.h index f8ea17ec..ae5df1a3 100644 --- a/include/MainController.h +++ b/include/MainController.h @@ -536,6 +536,7 @@ private: int startDisconnectActions(); void setClosingActionsText(const QString& text); void disconnectRig(); + void releaseRigCapsBeforeRigDeletion(); funcType inputLevelCommand(inputTypes input) const; uchar currentDataMode() const; void receiveModInput(const rigInput& input, int dataMode); diff --git a/src/MainController.cpp b/src/MainController.cpp index 1e75285a..701900fd 100644 --- a/src/MainController.cpp +++ b/src/MainController.cpp @@ -4710,6 +4710,42 @@ void MainController::connectionHandler() emit connStatusChanged(); } +// Tells everything holding the rig's capabilities that they are going away, +// before the rig (which owns them) is deleted. The rig's destructor does this +// too, but from the rig thread: listeners on the main thread, such as each +// rigctld client, only got it as a queued call that ran after the rig was +// freed. A rigctld request handled in between (WSJT-X polls about once a +// second) looked up a command in the freed capabilities and crashed. +void MainController::releaseRigCapsBeforeRigDeletion() +{ + if (!queue || queue->getRigCaps() == nullptr) + return; + + // Receivers are deleted by receiveRigCaps(nullptr) below; drop their queued + // slot calls first so none runs after the rig has gone. + for (auto *r : std::as_const(receivers)) { + if (r) + QCoreApplication::removePostedEvents(r, QEvent::MetaCall); + } + + // Emitted from this (main) thread: main-thread listeners - rigctld clients, + // this controller - drop the pointer now. The rig's own setRigCaps(nullptr) + // then changes nothing and is not emitted again. + queue->setRigCaps(nullptr); + + // Listeners on their own threads get it queued; wait until each has + // processed it, so none can use the capabilities after the rig is deleted. + // Neither thread blocks on the main thread, so this cannot deadlock. + const auto flush = [](QObject *object) { + if (object && object->thread() != QThread::currentThread() && object->thread()->isRunning()) + QMetaObject::invokeMethod(object, []() {}, Qt::BlockingQueuedConnection); + }; + flush(tci); +#if defined(USB_CONTROLLER) + flush(usbControllerDev); +#endif +} + void MainController::disconnectRig() { setClosingActionsText(QString()); @@ -4742,6 +4778,8 @@ void MainController::disconnectRig() emit receiverCountChanged(); emit detachedChanged(); + releaseRigCapsBeforeRigDeletion(); + if (rig) { QMetaObject::invokeMethod(rig, &rigCommander::closeComm, rig->thread() == QThread::currentThread() @@ -5203,6 +5241,8 @@ void MainController::receivePortError(errorType err) resetTxAudioOwnership(); stopRadioServer(); + releaseRigCapsBeforeRigDeletion(); + if (rig) { QMetaObject::invokeMethod(rig, &rigCommander::closeComm, rig->thread() == QThread::currentThread() -- 2.56.0.windows.4