Skip to content

Commit e42f892

Browse files
committed
fix: simplify to a single WARNING on early listen() exit, drop silence watchdog
sock_read=30 on the connect() request already bounds a connection that stops producing bytes (data or SSE keepalive) and routes into the existing ClientError handler, which already logs and reconnects - the added wait_for-based watchdog was redundant complexity for no new behavior. Restore sock_read=30 and drop the wait_for wrapper and streak logging. Keep only the early-exit change: listen() logs one WARNING line if it returns or raises before its first successful connect, and still re-raises rather than swallowing the failure.
1 parent 2620120 commit e42f892

6 files changed

Lines changed: 18 additions & 320 deletions

File tree

‎AGENTS.md‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Project-intrinsic knowledge that should travel with the code: build, test, relea
2424
- Internal reconnect paths (EOF, `ClientError`, unexpected exceptions) call `_close_response()`, **never** `close()` - `close()` additionally flips `active=False` and cancels the owned task, i.e. a real stop. `listen()`'s `finally` calls `_close_response()` unconditionally so cancellation still releases the connection.
2525
- Dispatch iterates a **sorted snapshot** of `_listeners.values()` with internal listeners first, never the live dict: a callback adding a listener mid-dispatch (e.g. `get_vehicle()` for an uncached VIN) must not raise `RuntimeError: dictionary changed size during iteration`, and a public callback must not mutate the event before an internal bookkeeping listener has cached from it. `_update_connection_listeners()` has the same mutation hazard and iterates a plain `list(...)` snapshot (no ordering requirement - connection listeners share no mutable event).
2626
- `__anext__` treats `ClientResponseError` with status 401/403 as terminal: sets `active = False` and raises `TeslemetryStreamAuthenticationError`. Every other `aiohttp.ClientError` keeps backoff-and-reconnect.
27-
- `__anext__` reads each SSE line under `asyncio.wait_for(..., timeout=_SILENCE_WINDOW)` - this is the one mechanism bounding a connection gone quiet (no data, no keepalive) without erroring; `connect()`'s `sock_read` is deliberately unbounded so the two don't race. A timeout closes the response and falls through to the outer loop's immediate reconnect, same as a clean server-side end. `_silence_streak` gates logging: the first silence-triggered reconnect after a real read logs WARNING, further ones with still no read in between log DEBUG - it resets only on a successfully read line (data or keepalive), not on a bare reconnect.
28-
- `listen()` tracks whether it has ever seen a successful connect (via a scoped internal connection listener). If it returns or raises before that - including cancellation - it logs ERROR before propagating; nothing before a first connect is swallowed.
27+
- `connect()`'s `sock_read=30` bounds a connection that stops producing bytes (no data, no SSE keepalive) - a read timeout raises `aiohttp.ClientError`, which the existing client-error handler in `__anext__` already logs and reconnects. There is no separate silence mechanism.
28+
- `listen()` tracks whether it has ever seen a successful connect (via a scoped internal connection listener). If it returns or raises before that - including cancellation - it logs one WARNING line before propagating; nothing before a first connect is swallowed.
2929
- `async_add_listener(..., internal=True)` marks a bookkeeping-only listener (the vehicle config-sync listener is the only user). Its `schedule_refresh` gate is unconditionally false, which is what makes construction-time registration safe outside a running event loop. Both the "first listener starts the task" and "last listener removed auto-closes" checks count only public listeners, so an internal listener can neither pin the connection open nor block a later public listener's zero-to-one restart. **Any stream stand-in (test doubles included) must implement `async_add_listener(callback, filters, internal=False)` and `async_add_connection_listener(callback)`.**
3030
- `topics=` is an optional exact SSE wire-event allowlist sent as the connection's `topics` query param. `SseTopic` is the closed server-recognized set; `SSE_VEHICLE_TOPICS`/`SSE_ENERGY_TOPICS`/`SSE_ALL_TOPICS` are client-side presets, flat per-product-kind lists deliberately not split by whether a topic has a connect-time snapshot (server behavior, not something this library encodes). Omitting it (`None`) is legacy-all forever. An explicitly empty iterable raises `ValueError` at construction - "no topics" must not mean "all topics", mirroring the server's 400. A bare `str`/`SseTopic` is accepted as a single topic, not iterated character-by-character; `topics: str | Iterable[str] | None` exists precisely because a lone string also satisfies `Iterable[str]`.
3131
- `ingest()` (and `TeslemetryStreamVehicle.ingest()`, same call with the VIN filled in) is the entry point for an observation not read off the SSE connection - a Bluetooth broadcast, today. It builds the native wire event (`vin`/`data`/`createdAt`) plus an open-ended `metadata` dict (`Metadata.SOURCE`/`Metadata.RAW`) and hands it to the same `_dispatch` `listen()` uses, so existing `listen_*` callbacks receive both sources with no translation and no second subscription. Dispatch is arrival-ordered and the stream holds no per-field value: **nothing is deduplicated, reordered, or dropped, and there is deliberately no source ranking or precedence** - which report to believe is the consumer's decision, made on `metadata`. Ingesting neither requires nor opens a connection. Neither library depends on the other: the BLE-side shim shaping a broadcast into this format lives in `tesla-fleet-api` and the consumer wires the two.
@@ -58,7 +58,6 @@ Project-intrinsic knowledge that should travel with the code: build, test, relea
5858
| Listen task ownership, dispatch snapshots/order, close races | `test_stream_lifecycle.py` |
5959
| 401/403 terminal vs transient retry | `test_auth_failure.py` |
6060
| Clean vs abrupt stream end logging | `test_clean_stream_end.py` |
61-
| Silence watchdog: timeout reconnect, keepalive reset, WARNING-once/DEBUG-repeat | `test_silence_watchdog.py` |
6261
| `listen()` early exit before first connect logs ERROR and re-raises | `test_listen_early_exit.py` |
6362
| `str` payload coercion against real telemetry | `test_field_type_coercion.py` |
6463
| `topics` URL construction, empty/bare-string handling, tariff listener | `test_sse_topics.py` |

‎teslemetry_stream/stream.py‎

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
LOGGER = logging.getLogger(__package__)
1818

19-
# The server sends an SSE keepalive comment every 10s. Three missed
20-
# keepalives (30s of total silence) bounds reconnecting a connection that
21-
# has gone quiet without erroring.
22-
_SILENCE_WINDOW = 30.0
23-
2419

2520
class TeslemetryStream:
2621
"""Teslemetry Stream Client"""
@@ -77,9 +72,6 @@ def __init__(
7772
] = {}
7873
self._connection_listeners: dict[Callable[..., Any], Callable[[bool], None]] = {}
7974
self._listen_task: asyncio.Task[None] | None = None
80-
# Consecutive silence-triggered reconnects with no successful read in
81-
# between - gates the WARNING-once/DEBUG-after-that logging split.
82-
self._silence_streak: int = 0
8375
# Created lazily in connect() - asyncio.Lock() requires a running
8476
# loop on Python 3.9, and streams are commonly built before one.
8577
self._connect_lock: asyncio.Lock | None = None
@@ -268,12 +260,7 @@ async def connect(self) -> None:
268260
headers=headers,
269261
params=params,
270262
raise_for_status=True,
271-
# sock_read is intentionally unbounded: the silence watchdog
272-
# in __anext__ is the one mechanism that bounds dead air on
273-
# an open connection, with logging this bare timeout can't do.
274-
timeout=aiohttp.ClientTimeout(
275-
connect=5, sock_connect=5, sock_read=None, total=None
276-
),
263+
timeout=aiohttp.ClientTimeout(connect=5, sock_connect=5, sock_read=30, total=None),
277264
chunked=True,
278265
)
279266
if not self.active:
@@ -345,33 +332,14 @@ async def __anext__(self) -> dict[str, Any]:
345332
# Connect to the stream
346333
await self.connect()
347334
assert self._response
348-
content_iter = self._response.content.__aiter__()
349-
while True:
350-
try:
351-
line_in_bytes = await asyncio.wait_for(
352-
content_iter.__anext__(), timeout=_SILENCE_WINDOW
353-
)
354-
except StopAsyncIteration:
355-
raise TeslemetryStreamEnded() from None
356-
except asyncio.TimeoutError:
357-
if self._silence_streak == 0:
358-
LOGGER.warning(
359-
"No data received for %ss, reconnecting", _SILENCE_WINDOW
360-
)
361-
else:
362-
LOGGER.debug(
363-
"No data received for %ss, reconnecting", _SILENCE_WINDOW
364-
)
365-
self._silence_streak += 1
366-
self._close_response()
367-
break
368-
self._silence_streak = 0
335+
async for line_in_bytes in self._response.content:
369336
field, _, value = line_in_bytes.decode("utf8").partition(": ")
370337
if field == "data":
371338
data = json.loads(value)
372339
if self.parse_timestamp:
373340
data["timestamp"] = _parse_created_at(data["createdAt"])
374341
return cast(dict[str, Any], data)
342+
raise TeslemetryStreamEnded()
375343
except StopAsyncIteration as e:
376344
# Re-raise explicitly so it isn't caught by the generic Exception handler below
377345
self.disconnect()
@@ -490,12 +458,10 @@ def _mark_connected(value: bool) -> None:
490458
self._dispatch(event)
491459
except BaseException as error:
492460
if not connected_once:
493-
LOGGER.error(
494-
"Listen task ended before its first connect: %r", error
495-
)
461+
LOGGER.warning("Listen task ended before its first connect: %r", error)
496462
raise
497463
if not connected_once:
498-
LOGGER.error("Listen task ended before its first connect")
464+
LOGGER.warning("Listen task ended before its first connect")
499465
finally:
500466
remove_connection_listener()
501467
self._close_response()

‎tests/test_clean_stream_end.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,8 @@ async def test_clean_end_logs_info_and_reconnects(results: list[bool]) -> None:
102102
)
103103

104104
session.responses[0].content.end()
105-
# asyncio.wait_for's pre-3.12 implementation adds a couple of extra
106-
# scheduling hops before a completed read is observable - enough
107-
# ticks to cover every supported Python version.
108-
for _ in range(6):
109-
await asyncio.sleep(0)
105+
await asyncio.sleep(0)
106+
await asyncio.sleep(0)
110107

111108
results.append(
112109
check(

‎tests/test_listen_early_exit.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
quietly: it is the failure mode behind the incident that motivated this -
55
a listen task that never logged a single connect attempt for 2.5+ days,
66
discoverable only by an absence in server-side logs. Any death before the
7-
first connect must say so loudly, at ERROR, in the caller's own logs, and
7+
first connect must say so loudly, at WARNING, in the caller's own logs, and
88
must not swallow the underlying exception.
99
"""
1010
from __future__ import annotations
@@ -100,13 +100,13 @@ def early_exit_errors(records: list[logging.LogRecord]) -> list[logging.LogRecor
100100
return [
101101
r
102102
for r in records
103-
if r.levelno == logging.ERROR and "before its first connect" in r.getMessage()
103+
if r.levelno == logging.WARNING and "before its first connect" in r.getMessage()
104104
]
105105

106106

107107
async def test_cancelled_before_first_connect_logs_error(results: list[bool]) -> None:
108108
"""A stuck connect that is cancelled before it ever succeeds must log
109-
ERROR and re-raise the CancelledError - never vanish silently."""
109+
WARNING and re-raise the CancelledError - never vanish silently."""
110110
session = BlockingSession()
111111
stream = make_stream(session)
112112

@@ -120,7 +120,7 @@ async def test_cancelled_before_first_connect_logs_error(results: list[bool]) ->
120120

121121
results.append(
122122
check(
123-
"cancellation before first connect logs at ERROR",
123+
"cancellation before first connect logs at WARNING",
124124
len(early_exit_errors(records)) == 1,
125125
f"got {early_exit_errors(records)}",
126126
)
@@ -156,7 +156,7 @@ async def test_auth_failure_before_first_connect_logs_error(results: list[bool])
156156
)
157157
results.append(
158158
check(
159-
"the failure before first connect logs at ERROR",
159+
"the failure before first connect logs at WARNING",
160160
len(early_exit_errors(records)) == 1,
161161
f"got {early_exit_errors(records)}",
162162
)

0 commit comments

Comments
 (0)