Skip to content

KeyError: 'time_since_update' in diskio processing when Glances server has just restarted (v4 API) #57

Description

@whatwhen

Describe the bug

glances_api/__init__.py line 231 accesses disk['time_since_update'] directly with no
fallback, in the diskio-processing branch of get_ha_sensor_data():

if data := self.data.get("diskio"):
    sensor_data["diskio"] = {}
    for disk in data:
        time_since_update = disk["time_since_update"]

This crashes with KeyError: 'time_since_update' when a diskio entry lacks that key.

By contrast, the network-plugin branch a few lines earlier already handles this correctly by
version-gating the access:

if self.version <= 3:
    time_since_update = network["time_since_update"]

Root cause

Traced this to Glances' own GlancesPluginModel._manage_rate decorator: time_since_update is
only added to a plugin's stats once a previous sample exists for that plugin
(stats_previous is None on the very first poll after the Glances server process starts, in
which case the decorator returns early without adding the key to any entry). So any diskio poll
that lands in the brief window right after the Glances server (re)starts will omit
time_since_update from every disk entry, and this library crashes on it.

Because Home Assistant's DataUpdateCoordinator fails the entire update on any exception raised
while processing coordinator data, this single crash marks every sensor for that Glances
instance unavailable at once -- not just disk sensors, but network/CPU/etc. sensors too, even
though their own code paths are safe.

To Reproduce

  1. Point this library at a Glances v4 server (confirmed on Glances 4.3.1, API v4).
  2. Restart the Glances server process.
  3. Poll get_ha_sensor_data() at the exact moment of the first sample after restart (or shortly
    after, before Glances has computed a diskio delta) -- disk["time_since_update"] will be
    missing on that poll and raise KeyError.

Expected behavior

The diskio branch should handle a missing time_since_update the same way the network branch
already does -- either version-gate it, or use disk.get("time_since_update") with a sane
fallback (e.g. 0), rather than crashing the whole update.

Environment

  • glances_api (PyPI glances-api): 0.10.0 (pinned in Home Assistant Core's
    homeassistant/components/glances/manifest.json)
  • Home Assistant Core: 2026.7.1 (fully current)
  • Glances server: 4.3.1, API version 4
  • Reproduced independently on two separate Glances 4.3.1 hosts

Related

  • Error with Glances (v4) home-assistant/core#137363 -- reports the same KeyError: 'time_since_update' symptom against
    Home Assistant Core, closed "not planned" since the actual bug lives in this library, not in
    Home Assistant's own code. Filing here since I couldn't find an existing issue against this repo
    for it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions