Skip to content

Fix app freeze on engine stop when rig disconnected - #83

Merged
punk-kaos merged 1 commit into
JS8Call-improved:masterfrom
tekstrand:rig-teardown-thread
Aug 27, 2026
Merged

Fix app freeze on engine stop when rig disconnected#83
punk-kaos merged 1 commit into
JS8Call-improved:masterfrom
tekstrand:rig-teardown-thread

Conversation

@tekstrand

Copy link
Copy Markdown
Contributor

What

  1. Moves rig teardown onto the TX handler
  2. Removes releaseRigPttForShutdown and the unbounded CountDownLatch.await() the main thread sat in, along with the ad-hoc Thread {} that disconnected the network client.
  3. Moves the transport construction out of onCreate into createRigTransports()

Why

Stopping the engine with the rig gone froze the app. The release waited on a latch with no timeout, and close() then ran in line behind it. Every HamlibRigControl method shares one monitor, so a wedged CAT command held both for hamlib's own timeout, and the USB bridge has the same shape where close takes a write lock a stuck read still holds. On my tablet with the radio pulled mid-transmission that was nine seconds of dead main thread, which is long enough for Android to call the app unresponsive.

Test

Need a rigctld to accepts the connection and never answer

For simulation without a real rig...

  1. On your machine: python3 -c "import socket;s=socket.socket();s.setsockopt(1,2,1);s.bind(('0.0.0.0',4532));s.listen(1);c,_=s.accept();input()"
  2. Settings, rig control on, type Network, host your machine's IP, port 4532.
  3. Start, wait for Connected to rigctld in logcat, then Stop.
  4. The UI should go back to Off right away, and Rig control torn down should turn up in logcat about five seconds later on the TX handler thread rather than the main one.
  5. Start again. Rig control should reconnect.

On the emulator, before and after:

master
18:43:59.103  5745 5745  Stopping engine
18:44:04.158  5745 5745  RigCtlClient: NetworkOnMainThreadException
18:44:04.164  5745 5745  Engine stopped                          <- main thread, +5.06s

this branch
18:45:08.474  5880 5880  Stopping engine
18:45:08.537  5880 5880  Engine stopped                          <- main thread, +0.06s
18:45:13.542  5880 5913  Unable to confirm PTT release during shutdown
18:45:13.542  5880 5913  Rig control torn down                   <- TX handler, +5.07s

…eleased PTT through a latch the main thread waited on with no timeout, and then closed the hamlib link, the serial bridges and the QMX and TruSDX sessions in line. With the radio gone those calls block: a wedged CAT command runs to hamlib's own timeout, and every HamlibRigControl method shares one monitor, so close() waits behind whatever is stuck. The USB bridge has the same shape, where close takes a write lock a stuck read still holds. Stopping now captures the rig references, hands them to the TX handler, which already owns rig I/O and queues them in order behind the wedged call, and returns. The transports are rebuilt in the same breath, so a restart never reaches a link that is still closing; that is why their construction moved out of onCreate and into createRigTransports. Unregistering the serial bridges stays on the main thread, because nativeUnregister clears one native global whichever instance calls it, and a deferred one could wipe the registration a restart had already made. The blocking release helper is gone, along with the ad-hoc thread that disconnected the network client.

Verified on the emulator against a rigctld that accepts the connection and never answers. Before, the main thread sat in stopEngine for 5.06 seconds and the retry threw NetworkOnMainThreadException. Now stopping returns in 63 ms, the PTT release gives up on the TX handler five seconds later, and the next start reconnects.
@punk-kaos
punk-kaos merged commit 08e5350 into JS8Call-improved:master Aug 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants