Skip to content

Keep http.client errors and deep JSON from escaping the trace read - #262

Merged
dmccoystephenson merged 3 commits into
mainfrom
fix/fleetweather-read-escapes
Nov 16, 2026
Merged

dmccoystephenson merged 3 commits into
mainfrom
fix/fleetweather-read-escapes

Conversation

@dmccoystephenson

@dmccoystephenson dmccoystephenson commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Summary

  • fleetweather.fetch_window turned HTTPError, URLError, OSError and ValueError into a WeatherUnavailable reason, but urlopen passes http.client.HTTPException through unwrapped. A garbled status line (BadStatusLine), a truncated body (IncompleteRead) or a GARDENER_HUB_TRACE_URL with a non-numeric port (InvalidURL, which Config.problem does not reject) therefore escaped. json.loads on a deeply nested body can raise RecursionError rather than ValueError (it does on Python 3.10, the declared floor), so that escaped too. All four now become a short reason (trace unreachable (<type>) / trace's answer is not JSON), matching what hub.py's _request and UserAuthClient._call already catch after Report every unusable hub as a HubError instead of a traceback #244/Treat every unusable UserAuth answer as an outage #247. The Request construction was moved inside the try for the same reason _request gives.
  • Effect before the fix:
    • gardener hub city-still, which the gateway's digest timer runs, has no backstop around Forecaster.sample, so any of these printed a traceback and failed the still. The module docstring says a trace failure "never fails /api/city or the still".
    • On /api/city the route's except Exception kept the response at 200, but Forecaster.window only records its cache timestamp after a WeatherUnavailable. So every poll from every viewer re-read a misbehaving trace, against CACHE_SECONDS's "two viewers must not mean two reads", and the status shown was a bare exception type.
  • hub city-still --weather-sample given a file deep enough to raise RecursionError now exits 2 with its one-line error instead of a traceback.
  • docs/TESTING.md's test_fleetweather.py paragraph names the new cases.

The problem was not tracked in an issue; it was found during this cycle's triage scan. The four open issues (#248, #154, #100, #94) all wait on a maintainer decision.

Test plan

  • python3 -m compileall gardener tests
  • New test_http_client_failures_and_deep_nesting_are_reasons_not_crashes in tests/test_fleetweather.py: stub openers raising BadStatusLine, IncompleteRead and InvalidURL, each checked for its exact reason and for the credential's absence, plus json.loads patched to raise RecursionError. The first push used a real 100,000-deep body for the last case instead. It passed on 3.10 but failed on 3.15, which parses that depth without a RecursionError, so the case was made deterministic in cdda42e.
  • New test_a_deeply_nested_sample_file_is_exit_2_not_a_traceback: exit 2 on every interpreter. It reaches the new RecursionError branch only where the interpreter raises it (3.10); elsewhere parse_sample refuses the parsed list.
  • Verify by removal: the fix was committed first. With gardener/fleetweather.py and gardener/cli.py restored from origin/main, and the removal asserted (grep -c printed 0 for both files), all four cases escaped fetch_window as their raw exception types. Once the files were restored, all four came back as WeatherUnavailable and git status --porcelain was empty.
  • CI on cdda42e is the anchor of record: test (3.10), test (3.x) (both tests/ and the integration/ offline tier), city-still, fleetcity and garden3d all pass. Locally the sandbox python3 is 3.8, below requires-python, so the cases were exercised directly against gardener.fleetweather rather than through the test module.
  • No dispatch.py, dev_loop.py or prompt template is touched, so no manual dispatch verification is required, and the "Safety model impact" checklist does not apply.

This PR description was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).

🤖 Generated with Claude Code


drafted by Claude on behalf of Daniel Stephenson

dmccoystephenson and others added 3 commits November 16, 2026 01:38
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A 100,000-deep body raises it on 3.10 but parses on 3.15, so the
depth-based case was interpreter-dependent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dmccoystephenson

Copy link
Copy Markdown
Member Author

Self-review: no blocking issues found. The code-review skill (low) was run on the diff and reported no findings, and a hand pass against CLAUDE.md's conventions follows.

  • gardener/fleetweather.py:204 — the widened except keeps the reason to type(exc).__name__. http.client quotes a bad header value in its messages, and here that would be the Basic credential, so the message must stay a type name and never {exc}. The test asserts r3ad is absent for each new case, including an InvalidURL whose message carries the credential string.
  • gardener/fleetweather.py:204 — InvalidURL is an http.client.HTTPException subclass, so a non-numeric port in GARDENER_HUB_TRACE_URL now reads as trace unreachable (InvalidURL) instead of failing the still. A sharper option is a Config.problem check on the port, which would name the setting. It was left out as scope widening; the reason is already one line and non-fatal.
  • gardener/fleetweather.py:210 — the RecursionError catch matters on the 3.10 floor. CI showed 3.15 parsing a 100,000-deep body without raising, so the first push's depth-based test was replaced with a patched json.loads (cdda42e).
  • gardener/cli.py:2198 — RecursionError was added beside OSError, ValueError for --weather-sample. {exc} is safe there: the file is the operator's own and holds no credential.
  • tests/test_fleetweather.py — mock.patch.object(fw.json, "loads", ...) patches the shared json module for the duration of one fetch_window call. Nothing else in that window calls json.loads (_window uses dumps).
  • Safety invariants: dispatch.py, MODE_SPECS and tend_mode_spec() are untouched, so bypassPermissions reachability and the merge_eligible() gate are unchanged. No new dependency; the new import is the stdlib's http.client.
  • Docs: docs/TESTING.md was updated. docs/HUB.md's weather section makes no claim about specific failure reasons, so nothing there drifted.
  • CI on head cdda42e: all five checks pass.

This review was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

@dmccoystephenson
dmccoystephenson merged commit e672c1e into main Nov 16, 2026
5 checks passed
@dmccoystephenson
dmccoystephenson deleted the fix/fleetweather-read-escapes branch November 16, 2026 08:47
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