Record pathfinding context on flying disconnects
This commit is contained in:
1 parent
0b462f4a76
commit
d4eae5ee5f
5 files changed
+141
-2
No files matched your search
@@ -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
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import importlib.util
|
||||
from contextlib import redirect_stdout
|
||||
import io
|
||||
from pathlib import Path
|
||||
import time
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -38,6 +41,25 @@ class FakeConnection:
|
||||
|
||||
|
||||
class ConnectionShutdownTests(unittest.TestCase):
|
||||
def test_flying_disconnect_reports_active_pathfinding_duration(self):
|
||||
started_at = time.monotonic() - 0.5
|
||||
state = Munch(pathfinding_active={42: {
|
||||
'started_at': started_at, 'timeout': 2.0,
|
||||
'start': (0, 64, 0), 'goal': (5, 64, 0),
|
||||
'thread': 'mosfet-movement',
|
||||
}}, pathfinding_history=[])
|
||||
packet = Munch(reason={'translate': 'multiplayer.disconnect.flying'})
|
||||
output = io.StringIO()
|
||||
|
||||
with redirect_stdout(output):
|
||||
connect._log_flying_pathfinding(state, packet)
|
||||
|
||||
logged = output.getvalue()
|
||||
self.assertIn('[pathfinding] flying disconnect diagnostics', logged)
|
||||
self.assertIn('active for', logged)
|
||||
self.assertIn('(0, 64, 0) -> (5, 64, 0)', logged)
|
||||
self.assertIn('mosfet-movement', logged)
|
||||
|
||||
def test_ctrl_c_stops_connection_retry_loop(self):
|
||||
state = Munch(running=True)
|
||||
connections = []
|
||||
|
||||
@@ -87,6 +87,20 @@ class DoorNavigationTests(unittest.TestCase):
|
||||
with self.assertRaises(AStarTimeout):
|
||||
finder.neighbors((0, 64, 0))
|
||||
|
||||
def test_pathfinder_records_completed_search_duration(self):
|
||||
g = Munch(chunks=FakeChunks({}), crawling=False,
|
||||
unopenable_doors=set())
|
||||
route = Pathfinder(g).astar((0, 64, 0), (0, 64, 0))
|
||||
|
||||
self.assertEqual(route, [(0, 64, 0)])
|
||||
self.assertEqual(g.pathfinding_active, {})
|
||||
self.assertEqual(len(g.pathfinding_history), 1)
|
||||
record = g.pathfinding_history[0]
|
||||
self.assertEqual(record['start'], (0, 64, 0))
|
||||
self.assertEqual(record['goal'], (0, 64, 0))
|
||||
self.assertEqual(record['status'], 'found')
|
||||
self.assertGreaterEqual(record['duration'], 0.0)
|
||||
|
||||
def test_path_prefix_detects_new_obstacles_and_missing_support(self):
|
||||
class TerrainChunks:
|
||||
def __init__(self):
|
||||
|
||||
Reference in new issue
Block a user