Walkthrough: the alarm that wouldn’t clear
This walkthrough follows a timing bug that depends on how packets arrive from
a server. You can’t see it in a screenshot, and it is hard to reproduce on
demand. The agent finds it by drawing its own picture of the data inside the
running application: a QML window, opened with ui_open, that plots how
old each network packet is when the application handles it.
What it demonstrates:
ui_open: an agent-made window bound to a live application object;- correlating an application’s behaviour with what a server sent;
- testing a theory in the running process (
object_invoke,signal_watch) before changing any code; sondefor building and restarting, and checking the fix with the same visualization.
Who it’s for: anyone curious what the QML window tools are for. It reads best after the first walkthrough, which introduces the basic tools. You need the project built and about 15 minutes, mostly waiting for the bug to build up.
Try it yourself first? The application,
examples/pump-monitor, contains the bug. Stop after Set up if you want to hunt for it yourself, and don’t read itsmain.cpp.
All outputs are real, trimmed to the relevant lines. They come from more than one run, so object ids, times and packet numbers don’t always line up between steps; yours will differ too.
The report
“The Pump 2 pressure high alarm on the monitor sometimes stays red after the pressure is back to normal. Sometimes it clears straight away. It seems worse when the monitor has been running for a while. Restarting it helps, for a bit.”
The monitor is a Qt Widgets application. It connects over TCP to a hub that forwards readings from the pumps, one JSON packet per line:
{"seq": 12, "ts": 1764400000123, "sensor": "pump-2", "value": 4.3}
Here seq counts packets on the connection, and ts is the time the hub sent
the packet, in milliseconds. Above 8.0 bar, pump 2 raises the alarm.
Set up
Start the hub. It is a small Python script that sends a fixed 10-second cycle of readings, and prints every packet it sends:
python3 examples/pump-monitor/hub.py > hub.log &
The example has a .sonde/config.json, so the agent can build and start the
monitor with sonde:
cd examples/pump-monitor && ../../build/sonde start && cd ../..
export SONDE_URL=http://127.0.0.1:8799/mcp
mcp() { build/sonde "$@"; }
built in 0.0 s
started pid 70652
ready: local socket pump-monitor-mcp, http://127.0.0.1:8799/mcp
Then leave it running for a minute: the report says it gets worse over time.
Step 1: look at it
mcp --image monitor.png window_screenshot

Caught at a bad moment: the alarm is red and pump-2 reads 9.6. But the hub’s log says otherwise:
sent #74 pump-2 9.6
sent #75 pump-2 4.3
sent #76 pump-1 5.4
sent #77 hub 0
sent #78 hub 0
The hub sent the recovery to 4.3 bar several packets ago. The screen is showing old news. How old?
Step 2: what has the monitor handled?
The window’s code isn’t the place to start. The data arrives somewhere, so
look for the object that receives it. In object_tree, under the
application, there is a HubConnection#hub:
mcp object_inspect '{"object": "hub"}'
@0x16f52dce8 HubConnection#hub
inherits: HubConnection > QObject
parent: @0x16f52dd78 QApplication
children: 1
properties:
objectName: QString = "hub"
packetsReceived: int = 25 (read-only)
lastSequence: int = 25 (read-only)
connected: bool = true (read-only)
signals:
void packetReceived(QVariantMap)
void connectedChanged()
slots:
void readPending()
At that moment the hub had sent packet #29, and the monitor had handled 25. It is four packets behind, after only 40 seconds. Check again a minute later, and it’s further behind still.
Step 3: draw a picture
“Behind” could mean many things: slow handling, a busy event loop, lost
packets. What would tell them apart is when each packet was handled,
compared with when it was sent. Every packet carries ts, and the hub object
announces each one with packetReceived. So the agent writes a small QML
window that listens to that signal and plots, for each packet, how long ago
the hub sent it:
import QtQuick
Window {
width: 560; height: 260; color: "white"
property var ages: []
Connections {
target: hub
function onPacketReceived(p) {
const a = ages.slice()
a.push({ seq: p.seq, age: Date.now() - p.ts, alarm: p.sensor === "pump-2" && p.value > 8 })
if (a.length > 20) a.shift()
ages = a
plot.requestPaint()
}
}
Text { x: 8; y: 6; font.pixelSize: 13; text: "Age of each packet when the monitor handled it (ms)" }
Canvas {
id: plot
anchors.fill: parent; anchors.topMargin: 30; anchors.bottomMargin: 26
onPaint: {
const ctx = getContext("2d")
ctx.reset()
let max = 1
for (const a of ages) max = Math.max(max, a.age)
const w = width / 20
for (let i = 0; i < ages.length; ++i) {
const h = (height - 30) * ages[i].age / max
ctx.fillStyle = ages[i].alarm ? "#c62828" : "#1565c0"
ctx.fillRect(i * w + 3, height - h, w - 6, h)
ctx.fillStyle = "black"
ctx.fillText("#" + ages[i].seq, i * w + 3, height - h - 14)
ctx.fillText((ages[i].age / 1000).toFixed(1) + "s", i * w + 3, height - h - 3)
}
}
}
Text {
anchors.bottom: parent.bottom; anchors.bottomMargin: 6; x: 8; font.pixelSize: 13
text: ages.length ? "last: #" + ages[ages.length - 1].seq + ", handled "
+ ages[ages.length - 1].age + " ms after the hub sent it" : "waiting for packets..."
}
}
It goes to the application with "objects": {"hub": "hub"}, which makes the
application’s HubConnection available to the QML as hub:
mcp ui_open '{"qml": "<the QML above>", "objects": {"hub": "hub"}, "title": "Packet age"}'
Opened @0xc9d02c100 QQuickWindowQmlImpl_QML_0 "Packet age" (560x260). Close it with ui_close {"window": "@0xc9d02c100"}; window_screenshot and object_tree work on it too.
Objects: hub = @0x16f52dce8 HubConnection#hub
After 45 seconds:
mcp --image age.png window_screenshot '{"window": "Packet age"}'

Three things stand out:
- Every packet is handled 8 to 14 seconds after it was sent. On the same machine, that should be a millisecond.
- It steps up, and never comes back down. 9 s, then 10, 12 and 14. Nothing ever catches up.
-
Each step comes right after a red bar, a pump-2 spike above 8 bar (#39, #46, #53). The hub’s log shows what those have in common:
sent #46 pump-2 9.6 sent #47 pump-2 4.3Every spike is followed at once by its recovery, and
hub.pysends the two in the same write. They are the only packets that travel together.
So every time two packets arrive together, the monitor falls one packet
further behind, permanently. That is exactly what you’d see if the monitor
handled one line per readyRead. Qt emits readyRead once for each
batch of new data, not once per line. A second line that arrived in the same
batch stays in the socket’s buffer until the next packet arrives, then the
one after that waits, and so on.
It also explains the report. After a spike, the monitor shows the 9.6 when the next packet arrives, and the recovery only when the one after that arrives. That is seconds later, and more seconds the longer it has been running. Restarting clears the backlog, “for a bit”.
Step 4: prove it before changing code
If that’s right, there are packets sitting in the socket buffer right now,
and calling the read slot by hand, with no new data from the network, will
hand them over one by one. signal_watch records each one:
mcp signal_watch '{"object": "hub", "signal": "packetReceived"}'
mcp object_invoke '{"object": "hub", "method": "readPending"}' # three times
mcp event_read '{"sources": ["signals"], "sinceSequence": 98}'
Watch 1: @0x16f52dce8 HubConnection#hub packetReceived(QVariantMap). ...
Invoked readPending()
...
#99 09:15:24.649 info signals[packetReceived]: @0x16f52dce8 HubConnection#hub packetReceived({"sensor":"hub","seq":70,"ts":1795943712688,"value":0}) ...
#101 09:15:24.687 info signals[packetReceived]: @0x16f52dce8 HubConnection#hub packetReceived({"sensor":"hub","seq":71,"ts":1795943714685,"value":0}) ...
#103 09:15:24.689 info signals[packetReceived]: @0x16f52dce8 HubConnection#hub packetReceived({"sensor":"pump-1","seq":72,"ts":1795943715684,"value":5.2}) ...
#105 09:15:24.729 info signals[packetReceived]: @0x16f52dce8 HubConnection#hub packetReceived({"sensor":"pump-2","seq":73,"ts":1795943716688,"value":4.1}) ...
The hub sent #70 to #73 about 4 seconds apart in all (look at ts),
yet the monitor handled them within 80 milliseconds of each other, three
of them in direct response to readPending(). They had been waiting in the
buffer all along. (The fourth came with a new packet from the hub.)
Calling it a dozen more times drains the backlog. lastSequence reaches 84,
the hub’s latest, and the plot shows the delays falling away:

Step 5: fix it
Now the code. The slot connected to readyRead:
void readPending()
{
if (!m_socket.canReadLine())
return;
const QJsonObject packet = QJsonDocument::fromJson(m_socket.readLine()).object();
...
Q_EMIT packetReceived(packet.toVariantMap());
}
One line per call, as predicted. The fix is to read every complete line that is there:
void readPending()
{
while (m_socket.canReadLine()) {
const QJsonObject packet = QJsonDocument::fromJson(m_socket.readLine()).object();
...
Q_EMIT packetReceived(packet.toVariantMap());
}
}
Step 6: check the fix the same way
cd examples/pump-monitor && ../../build/sonde restart && cd ../..
mcp ui_open '{"qml": "<the same QML>", "objects": {"hub": "hub"}, "title": "Packet age"}'
built in 1.5 s
started pid 70815
ready: local socket pump-monitor-mcp, http://127.0.0.1:8799/mcp
Opened @0xb8b35c100 QQuickWindowQmlImpl_QML_0 "Packet age" (560x260). ...
The restart closed the old window along with the old process, so it is opened again. After 45 seconds, two spikes and their recoveries later:

Every packet, spikes included, is handled 0 or 1 ms after it was sent. The
bars look tall because the plot scales to the largest value, which is now
1 ms; read the labels. And object_inspect agrees with the hub’s log:
packetsReceived: int = 31 (read-only)
lastSequence: int = 31 (read-only)
sent #31 pump-2 4.1
(The repository keeps the bug, so you can try it yourself.)
What made the difference
- The data was in the application, not on the screen. A screenshot
shows the current state; what mattered was when each packet was handled
compared with when it was sent. The agent could only get that from inside
the process, and
ui_openlet it look there without changing the application. - A picture showed the pattern. Reading 70 lines of events would have shown growing delays too. The plot showed in one screenshot that the delay steps up, never down, and only after the batched packets.
- The theory was tested before the code was read. Calling
readPending()by hand proved that the packets were sitting in the buffer, which ruled out a slow event loop, the network, and the hub. - The same window checked the fix, so “fixed” means the same thing as “broken” did.
Applying this to your own application
- Give the objects that move data around an
objectName, and expose what they handle as properties withNOTIFYsignals, or as signals with the data as arguments. That is what makes them easy to plot. - Put a timestamp in the data, if the protocol allows it. “How old is this when we handle it?” answers a lot of questions.
- Install
installQmlWindowTools()in debug builds. A Widgets application needsSonde::Quicklinked, as this one does.
Cleaning up
cd examples/pump-monitor && ../../build/sonde stop && cd ../..
kill %1 # the hub, if you started it from this shell