Skip to content

Count a probe that did not complete as a failure - #34

Open
drHouse-gif wants to merge 1 commit into
victronenergy:masterfrom
drHouse-gif:fix/probe-status-error
Open

drHouse-gif wants to merge 1 commit into
victronenergy:masterfrom
drHouse-gif:fix/probe-status-error

Conversation

@drHouse-gif

Copy link
Copy Markdown
Collaborator

One commit on master (d8cb65d, v2.21). discovery.py +12, mock/mock_shelly_server.py +11, two new test files: 141 insertions, no deletions.

The problem

_attempt_connect dispatches on the probe result, and ProbeStatus.ERROR is matched by no
branch at all. An endpoint whose probe returns it gets no mark_success, no mark_failure and
no stop_connect, so its ConnectMeta is left exactly as it was: attempts not incremented,
next_retry_at not moved. The endpoint therefore stays due and is re-probed on the next
heartbeat, and the one after that, without the attempt ever being counted.

Two routes lead to that status:

where how
explicit discovery.py:919 connect() returned True, but _shelly_device.connected is already False at the check immediately after it
implicit discovery.py:73 DeviceProbeResult.status defaults to ProbeStatus.ERROR, so any exception raised in _get_device_info before a status is assigned returns it

The implicit route is narrow in practice, because ShellyDevice.connect() wraps its body in
except Exception: return False. It does still leak out of the handler's own cleanup:
self._shelly_device.shutdown() and self._aiohttp_session.close() are awaited inside that
except and are not themselves guarded. Reachable, not reproduced, and I have labelled it as
such rather than claiming otherwise.

The change

One branch, matching on the status rather than on how it arose, so both routes are covered:

elif result.status == ProbeStatus.ERROR:
    ep_state.connect.mark_failure(now)
    logger.warning("Probe of shelly device %s at %s did not complete, retrying in %d seconds", ...)

UNREACHABLE keeps its own branch and its own message. The wording deliberately avoids saying
"connected and then dropped", even though that is the route I reproduced, because the implicit
route can arrive here without the device having answered.

The separate message is also the only way an operator can currently tell this case from a
device that never answered at all.

How the scenario is driven

Shelly.GetComponents is the last RPC of ShellyDevice.connect(). What follows it,
_get_channels_info_from_components, works off the config already embedded in that response
and makes no further call. So a websocket closed right after that answer leaves connect()
with everything it needs - it returns True - while the check on the very next line sees a
disconnected device.

The mock gains --ws-close-after-method for exactly that. Anchoring on the method name rather
than on a count of served frames is deliberate, and learned the hard way: a frame count
reproduced the window on one machine and silently stopped hitting it on another, where the
probe simply completed and the test passed without exercising anything.

Tests

File Tests With the change Change reverted
tests/test_probe_status_error.py 2 2 passed (36 s) 2 failed (217 s, both time out waiting for a line that is never written)
tests/test_probe_status_coverage.py 2 2 passed 1 failed

The first asserts the branch is entered and reported. The second asserts the delays it reports
are 10, 20, 40 - the doubling backoff - rather than the heartbeat interval.

test_probe_status_coverage.py reads the dispatch rather than the behaviour, on purpose: it
guards the wider class of bug, a ProbeStatus produced with no consumer, and keeps working if
the dispatch is restructured.

Full suite: 79 passed, 4 xfailed, 1 failed on aioshelly 12.0.0, against a pristine v2.21
baseline of 75 passed, 4 xfailed, 1 failed. The one failure,
test_timeouts.py::test_freeze_longer_than_reconnect_loop, is pre-existing: it fails on
unmodified v2.21 with aioshelly 12.0.0, which is the version the Venus OS recipe pins and a GX
actually runs, and it passes on 14.1.0, which CI installs unpinned.

Interaction with the other proposed change

fix/unsupported-device-not-listed touches a neighbouring branch of the same if/elif chain.
discovery.py merges cleanly between the two. They do overlap by one line in
mock/mock_shelly_server.py, where both add a constructor argument at the end of the same
parameter list; the resolution is to keep both lines. Neither change depends on the other.

ProbeStatus.ERROR was produced but matched by nothing in _attempt_connect,
so the endpoint's ConnectMeta was left untouched: no attempt counted and
next_retry_at unmoved. The endpoint stayed due and was re-probed on every
heartbeat instead of following the backoff.

Two routes reach that status. The explicit one is connect() reporting a
live link while _shelly_device.connected is already False by the check
immediately after it. The implicit one is DeviceProbeResult defaulting to
ERROR, so any exception raised before a status is assigned lands there as
well. Both mean the probe did not complete, so both are counted as a
failure, and reported separately from UNREACHABLE because that is a device
that never answered at all.

The explicit route is driven end to end. Shelly.GetComponents is the last
RPC of connect(): what follows it, _get_channels_info_from_components,
works off the config already embedded in that response and makes no
further call. A websocket closed right after it is answered therefore
leaves connect() with everything it needs, so it returns True, while the
check on the next line sees a disconnected device.

mock/mock_shelly_server.py gains --ws-close-after-method for that.
Anchoring on the method name rather than on a count of served frames is
deliberate: a count is timing dependent and silently stops hitting the
window, which it did on a second machine.

tests/test_probe_status_error.py asserts the branch is entered and that
the delays it then reports are 10, 20, 40 rather than the heartbeat
interval. Both of its tests fail with this change reverted.
tests/test_probe_status_coverage.py guards the wider class of bug, a
status produced with no consumer, by reading the dispatch; its second
test fails with this change reverted too.
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.

1 participant