fix(critical): Event-Loop-Blockade — ThreadPool-Saturation + Pulse-Interval + Classifier-Timeout

Root-Cause (Rainer): Pulse-Thread + Adapter-Calls + DDP-Fallback-Lawine
sättigen den ThreadPool (asyncio.to_thread). Der Loop-Selector kann
Watchdog-Probes nicht ausführen → 3 Strikes → Kill.

Änderungen:
- adapter.py: classify_and_route in asyncio.wait_for(4s), Timeout → STAY
- adapter.py: ThreadPoolExecutor max_workers=32 in on_connected_cb
- presence_engine.py: pulse_interval 1.5→5.0, sync-default, 2-stufiger CB
- classifier.py: _llm_raw/_llm_classify/verify_confidence in wait_for(25s)
This commit is contained in:
Bernd (Hermes)
2026-07-27 16:01:55 +02:00
parent 82c2713929
commit 6990419bd0
3 changed files with 116 additions and 11 deletions
+56 -6
View File
@@ -162,7 +162,7 @@ class PresenceEngine:
rest=None,
enabled: bool = True,
coalesce_ms: int = 2000,
pulse_interval: float = 1.5,
pulse_interval: float = 5.0,
gateway_state_path: str = "",
_clamp_pulse: bool = True,
):
@@ -719,13 +719,13 @@ class PulseController:
def __init__(
self,
pulse_interval: float = 1.5,
pulse_interval: float = 5.0,
_clamp_pulse: bool = True,
):
"""Initialize PulseController.
Args:
pulse_interval: Pulse interval in seconds (default 1.5).
pulse_interval: Pulse interval in seconds (default 5.0).
_clamp_pulse: If True, clamp to 0.5-30s. Set False for testing.
"""
if _clamp_pulse:
@@ -745,6 +745,12 @@ class PulseController:
# P1-2 O-2: Async-await bridge
self._loop: Optional[asyncio.AbstractEventLoop] = None
self._await_timeout: float = 2.0
# P0-CRITICAL: Default sync — async-Pfad nur wenn explizit aktiviert.
# Verhindert ThreadPool-Saturation durch run_coroutine_threadsafe.
self._use_async_tick: bool = False
# P0-CRITICAL: Circuit-Breaker — zweistufiges Backoff bei Fehlern.
self._consecutive_failures: int = 0
self._current_interval: Optional[float] = None # None = use _pulse_interval
# ── Public API ───────────────────────────────────────────────────────
@@ -882,7 +888,10 @@ class PulseController:
self._pulse_tick()
# Sleep for pulse interval, checking running flag periodically
for _ in range(int(self._pulse_interval * 10)):
# P0-CRITICAL: Circuit-Breaker — _current_interval überschreibt
# _pulse_interval bei Fehlern (Stufe 1: 10s, Stufe 2: 30s).
effective_interval = self._current_interval if self._current_interval is not None else self._pulse_interval
for _ in range(int(effective_interval * 10)):
if not self._running.is_set():
break
if self._paused.is_set():
@@ -923,8 +932,10 @@ class PulseController:
)
return
# P1-2 O-2: Delegate to async path if loop is bound and running
if self._loop is not None and self._loop.is_running():
# P0-CRITICAL: Default sync — async-Pfad nur wenn explizit aktiviert.
# run_coroutine_threadsafe sättigt den ThreadPool und blockiert den
# Event-Loop-Selector → Watchdog-Probes schlagen fehl → Kill.
if self._use_async_tick and self._loop is not None and self._loop.is_running():
try:
future = asyncio.run_coroutine_threadsafe(
self._pulse_tick_async(),
@@ -959,10 +970,12 @@ class PulseController:
self._ddp.set_status(status)
self._last_sent_phase = self._phase
self._increment_telemetry()
self._record_success()
logger.debug("[pulse] _pulse_tick DDP set_status OK: %s", status)
return
except Exception as e:
logger.info("[pulse] _pulse_tick DDP set_status failed: %s", e)
self._record_failure()
# REST fallback for message text
if self._rest is not None and getattr(self._rest, 'auth_token', None):
@@ -980,9 +993,11 @@ class PulseController:
)
self._last_sent_phase = self._phase
self._increment_telemetry()
self._record_success()
logger.debug("[pulse] _pulse_tick REST setStatus OK: %s", status)
except Exception as e:
logger.info("[pulse] _pulse_tick REST setStatus failed: %s", e)
self._record_failure()
else:
logger.debug(
"[pulse] _pulse_tick skip: no DDP and no REST available (ddp=%s, rest=%s)",
@@ -1007,6 +1022,7 @@ class PulseController:
await self._ddp.set_status_async(status, timeout=self._await_timeout)
self._last_sent_phase = self._phase
self._increment_telemetry()
self._record_success()
logger.debug("[pulse] _pulse_tick_async DDP set_status_async OK: %s", status)
return
except Exception as e:
@@ -1015,6 +1031,7 @@ class PulseController:
"falling back to REST",
e,
)
self._record_failure()
# REST fallback for message text
if self._rest is not None and getattr(self._rest, 'auth_token', None):
@@ -1032,14 +1049,47 @@ class PulseController:
)
self._last_sent_phase = self._phase
self._increment_telemetry()
self._record_success()
logger.debug("[pulse] _pulse_tick_async REST setStatus OK: %s", status)
except Exception as e:
logger.info("[pulse] _pulse_tick_async REST setStatus failed: %s", e)
self._record_failure()
else:
logger.debug(
"[pulse] _pulse_tick_async skip: no DDP and no REST available",
)
def _record_failure(self) -> None:
"""P0-CRITICAL: Zweistufiger Circuit-Breaker.
Stufe 1: ab 2 consecutive_failures → _current_interval = 10.0s
Stufe 2: ab 4 consecutive_failures → _current_interval = 30.0s
Bei Erfolg (_record_success) wird zurückgesetzt.
"""
self._consecutive_failures += 1
if self._consecutive_failures >= 4:
self._current_interval = 30.0
logger.warning(
"[pulse] Circuit-Breaker Stufe 2: interval=30.0s (failures=%d)",
self._consecutive_failures,
)
elif self._consecutive_failures >= 2:
self._current_interval = 10.0
logger.warning(
"[pulse] Circuit-Breaker Stufe 1: interval=10.0s (failures=%d)",
self._consecutive_failures,
)
def _record_success(self) -> None:
"""Reset circuit breaker on successful pulse."""
if self._consecutive_failures > 0:
logger.info(
"[pulse] Circuit-Breaker reset: failures=%d → 0, interval restored",
self._consecutive_failures,
)
self._consecutive_failures = 0
self._current_interval = None
def _increment_telemetry(self) -> None:
"""Increment the heartbeat_pulse telemetry counter.