Record pathfinding context on flying disconnects

This commit is contained in:
Tanner committed 2026-11-19 14:57:25 +00:00
1 parent 0b462f4a76
commit d4eae5ee5f
5 files changed
+141 -2

No files matched your search

+23
View File
@@ -648,6 +648,29 @@ def start_automation(global_state, initialize=True):
else:
with reload_lock:
bot.tick(g)
except OSError as exc:
connection = g.get('connection')
if (not connection or not connection.connected or
isinstance(exc, (BrokenPipeError,
ConnectionResetError,
ConnectionAbortedError))):
next_path = g.path[0] if g.path else None
position = (utils.pint(g.pos)
if g.get('pos') else None)
g.path = []
g.movement_status = 'disconnected'
print('[movement] connection closed during tick at {}; '
'on_ground={}, y_v={:.2f}, next_path={}'.format(
position, g.get('on_ground'),
g.get('y_v', 0.0), next_path), flush=True)
return
traceback.print_exc()
g.movement_error = traceback.format_exc()
g.path = []
g.movement_status = 'error'
print('[movement] stopped after navigation error',
flush=True)
return
except Exception:
traceback.print_exc()
g.movement_error = traceback.format_exc()
+44 -2
View File
@@ -4,10 +4,12 @@ Run with SERVER=... PLAYER_NAME=mosfet1 GAME_PASSWORD=... python -m mosfet.conne
"""
import os
import threading
import time
from minecraft.networking.connection import Connection
from minecraft.networking.modern import (
CancelPostjoinDialog, ChatCommand, PlayGameEventPacket, PlayShowDialog,
PlayLogin, SubmitPostjoinLoginDialog, SystemChat, TeleportPosition,
CancelPostjoinDialog, ChatCommand, PlayDisconnect, PlayGameEventPacket,
PlayShowDialog, PlayLogin, SubmitPostjoinLoginDialog, SystemChat,
TeleportPosition,
)
from mosfet.chat_log import append_chat_event
from mosfet.protocol.managers import WorldState
@@ -99,6 +101,41 @@ def _system_chat_log_entry(message):
return 'system', None, message
def _log_flying_pathfinding(global_state, packet):
reason = component_text(getattr(packet, 'reason', None))
if ('multiplayer.disconnect.flying' not in reason and
'flying' not in reason.lower()):
return
now = time.monotonic()
print('[pathfinding] flying disconnect diagnostics:', reason, flush=True)
active = global_state.get('pathfinding_active', {})
for search in sorted(active.values(),
key=lambda item: item.get('started_at', now)):
elapsed = max(0.0, now - search.get('started_at', now))
print('[pathfinding] active for {:.3f}s (budget {:.3f}s): '
'{} -> {} on {}'.format(
elapsed, search.get('timeout', 0.0),
search.get('start'), search.get('goal'),
search.get('thread', 'unknown')), flush=True)
history = global_state.get('pathfinding_history', ())
recent = [search for search in history
if now - search.get('finished_at', now) <= 10.0]
for search in recent[-5:]:
age = max(0.0, now - search.get('finished_at', now))
print('[pathfinding] {} {:.3f}s, finished {:.3f}s before kick: '
'{} -> {} (budget {:.3f}s, {})'.format(
search.get('status', 'unknown'),
search.get('duration', 0.0), age,
search.get('start'), search.get('goal'),
search.get('timeout', 0.0),
search.get('thread', 'unknown')), flush=True)
if not active and not recent:
print('[pathfinding] no A* search active or completed in the '
'10s before the kick', flush=True)
def main(global_state=None, *, server=None, port=None, username=None,
password=None):
server = server or os.environ.get('SERVER', '127.0.0.1')
@@ -225,6 +262,11 @@ def main(global_state=None, *, server=None, port=None, username=None,
'authenticate', 'authentication required')):
send_auth_command('login')
@connection.listener(PlayDisconnect)
def on_play_disconnect(packet):
if global_state is not None:
_log_flying_pathfinding(global_state, packet)
connection.connect()
print('Connected as {} to {}:{} using offline protocol 777.'.format(
username, server, port), flush=True)
+38
View File
@@ -1,5 +1,6 @@
import importlib
import functools
import threading
import time
from astar import AStar
@@ -125,6 +126,43 @@ class Pathfinder(AStar):
self.timeout = max(0.0, float(timeout))
self.start_time = time.monotonic()
def astar(self, start, goal, reversePath=False):
started_at = time.monotonic()
thread = threading.current_thread()
thread_id = threading.get_ident()
search = {
'start': tuple(start),
'goal': tuple(goal),
'started_at': started_at,
'timeout': self.timeout,
'thread': thread.name,
'thread_id': thread_id,
}
active = self.g.setdefault('pathfinding_active', {})
history = self.g.setdefault('pathfinding_history', [])
previous = active.get(thread_id)
active[thread_id] = search
status = 'error'
try:
result = super().astar(start, goal, reversePath)
status = 'found' if result else 'unreachable'
return result
except AStarTimeout:
status = 'timed out'
raise
finally:
finished_at = time.monotonic()
search['finished_at'] = finished_at
search['duration'] = finished_at - started_at
search['status'] = status
history.append(search.copy())
del history[:-32]
if active.get(thread_id) is search:
if previous is None:
del active[thread_id]
else:
active[thread_id] = previous
@functools.lru_cache(maxsize=BLOCK_CACHE_SIZE)
def bair(self, p):
state_id = self.chunks.get_block_at(*p)