fix(bot): connection.established race condition mit threading.Event (fixes #2)

TCPInterface-Konstruktor kehrt zurück bevor das Event aus dem Bibliotheks-Thread
feuert. _conn_event.wait(timeout=10s) wartet sauber auf _on_connection statt
sofort den Fallback auszulösen.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
ppfeiffer 2026-02-19 17:30:36 +01:00
parent cc392a125a
commit edfe73b8ca
3 changed files with 17 additions and 4 deletions

View file

@ -1,5 +1,14 @@
# Changelog
## [0.8.7] - 2026-02-19
### Fixed
- **Startup-Warning "connection.established missed"** (fixes #2): Der Fallback-Check
nach `TCPInterface()`-Konstruktor prüfte `_connected` sofort, obwohl das Event
`connection.established` erst kurz danach aus dem Bibliotheks-Thread feuert.
Fix: `threading.Event` wartet jetzt bis zu 10 Sekunden auf das Event; der Fallback
greift nur noch bei echtem Ausbleiben des Events.
## [0.8.6] - 2026-02-19
### Added

View file

@ -1,4 +1,4 @@
version: "0.8.6"
version: "0.8.7"
bot:
name: "MeshDD-Bot"

View file

@ -1,5 +1,6 @@
import asyncio
import logging
import threading
import time
import json
import urllib.request
@ -22,6 +23,7 @@ class MeshBot:
self.start_time = time.time()
self.ws_manager = None # set by main.py
self._connected = False
self._conn_event = threading.Event()
def connect(self):
host = config.get("meshtastic.host", "localhost")
@ -35,9 +37,10 @@ class MeshBot:
pub.subscribe(self._on_connection_lost, "meshtastic.connection.lost")
pub.subscribe(self._on_node_updated, "meshtastic.node.updated")
self.interface = TCPInterface(hostname=host, portNumber=port)
# Fallback: if connection.established fired before our subscription
# was registered, set the flag and sync initial nodes manually.
if not self._connected:
# Wait up to 10 s for connection.established to fire from the library thread.
# Fallback: if the event never arrives (e.g. library behaviour change),
# initialise state manually so the bot still works.
if not self._conn_event.wait(timeout=10.0):
logger.warning("connection.established missed applying fallback")
self._connected = True
if self.ws_manager:
@ -161,6 +164,7 @@ class MeshBot:
def _on_connection(self, interface, topic=pub.AUTO_TOPIC):
logger.info("Meshtastic connection established")
self._connected = True
self._conn_event.set()
if self.ws_manager:
self.loop.call_soon_threadsafe(asyncio.ensure_future, self._broadcast_bot_status())
if hasattr(interface, 'nodes') and interface.nodes: