Skip to content

Don't serve cache-excluded fields from the subscription fast path - #6

Open
ardetu wants to merge 1 commit into
Snapmaker:mainfrom
ardetu:fix-subscription-cache-exclusions
Open

ardetu wants to merge 1 commit into
Snapmaker:mainfrom
ardetu:fix-subscription-cache-exclusions

Conversation

@ardetu

@ardetu ardetu commented Sep 13, 2026

Copy link
Copy Markdown

Fixes #5.

Problem

_request_subscripton() has a fast path that builds its response directly from subscription_cache:

if covered and not missing_cache:
    # No new objects or fields need to be subscribed on Klippy's
    # side.  Build the response directly from the cached status.
    ...
    return {'status': pruned_status, 'eventtime': self._last_eventtime}

But that cache is populated with CACHE_EXCLUSIONS stripped out:

CACHE_EXCLUSIONS = {
    "configfile": ["config", "settings"]
}

So subscription_cache["configfile"] only ever holds {warnings, save_config_pending, save_config_pending_items}. Upstream Moonraker strips those fields from the cache only — the response is always built from a real Klippy query, so the exclusion stays invisible to clients. Returning the cache as the response leaks the exclusion into the API.

Observed on a U1 (Klipper 1.6.0.267_20260815150420, Moonraker 1.6.0):

Request configfile keys returned
HTTP GET /printer/objects/query?configfile all 5, settings = 204 sections ✅
WS subscribe {configfile: null} warnings, save_config_pending, save_config_pending_items ❌
WS subscribe {configfile: ["settings","config"]} {} ❌

The last case is the same root cause: {k: v for k, v in cached.items() if k in fields} matches nothing.

User-visible effect: the Fluidd build shipped on the printer gates its Bed Mesh card on configfile.settings.bed_mesh != null, so the card vanishes from the Dashboard and Tune pages with no error shown, even though printer.bed_mesh is fully populated and correct.

Fix

Skip the fast path when the caller asked for a field the cache is known to omit, falling through to a real Klippy query.

This is deliberately minimal — the optimization is preserved for every other object, and for configfile requests that don't touch config/settings.

Verification

I reimplemented the fast-path logic standalone to confirm the behaviour change. Before the patch it reproduces the exact key sets observed on the printer, including the empty-object case:

Request before after
{configfile: None} cache → 3 keys ❌ Klippy → all 5 keys ✅
{configfile: ["settings","config"]} cache → {} ❌ Klippy → both ✅
{configfile: ["warnings"]} cache ✅ cache ✅ (unchanged)
{toolhead: None} cache ✅ cache ✅ (unchanged)

python -m py_compile passes; all added lines are ≤79 chars.

Note (not addressed here)

Upstream begins _request_subscripton() with:

# if the connection has an existing subscription pop it off
self.subscriptions.pop(conn, None)

That line isn't present in this fork, which is why a connection re-subscribing "covers itself" via existing_union and hits the fast path so readily. Restoring it also means a client narrowing its subscription no longer gets the union of its old and new field sets. I left it out to keep this PR minimal, but I'm happy to add it here or in a separate PR if you'd prefer.

The fast path in _request_subscripton() builds its response from
subscription_cache, which intentionally omits configfile.config and
configfile.settings (CACHE_EXCLUSIONS). Returning that cache directly as
the response leaks the exclusion into the API, so subscribers receive a
configfile with those two fields silently missing, while the equivalent
HTTP objects/query returns them correctly.

On a U1 running Moonraker 1.6.0 this makes printer.objects.subscribe with
{configfile: null} return only warnings, save_config_pending and
save_config_pending_items, and {configfile: ["settings","config"]} return
an empty object. The shipped Fluidd UI gates its Bed Mesh card on
configfile.settings.bed_mesh, so the card disappears entirely.

Skip the fast path when the caller requested a field that CACHE_EXCLUSIONS
removes from the cache, falling through to a real Klippy query. The
optimization still applies to every other object and to configfile
requests that do not touch the excluded fields.

Fixes Snapmaker#5

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

printer.objects.subscribe silently drops configfile.config and configfile.settings (hides Bed Mesh in Fluidd)

1 participant