From a288bb037aff0eaf90b758d34b28728b350e142f Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 30 Sep 2026 15:56:17 +0200 Subject: [PATCH] cultmesh-py: a peer's message that handling cannot serve ends that peer's session, never the RUDP thread The RUDP loop caught only ValueError, so a subscribe frame with `schemaIds: 5` raised TypeError in _handle_connection_message and ended the thread for every peer. Any exception from handling one peer's messages now ends that peer's session with a goodbye, as a refused packet does, and is logged by its type alone: the error's message can quote what the peer sent. Co-Authored-By: Claude Opus 5.5 --- .../cultmesh-py/src/cultmesh_py/server.py | 52 ++++++++++++------- packages/cultmesh-py/tests/test_cultmesh.py | 31 +++++++++++ 2 files changed, 65 insertions(+), 18 deletions(-) diff --git a/packages/cultmesh-py/src/cultmesh_py/server.py b/packages/cultmesh-py/src/cultmesh_py/server.py index be8cd903..413441c3 100644 --- a/packages/cultmesh-py/src/cultmesh_py/server.py +++ b/packages/cultmesh-py/src/cultmesh_py/server.py @@ -1,5 +1,6 @@ from __future__ import annotations +import logging import socket import threading import time @@ -32,6 +33,8 @@ from cultnet_py.cultmesh_contracts import ( CultMeshVerseCatalog, ) +_LOGGER = logging.getLogger(__name__) + @dataclass(frozen=True) class _DatabaseSubscription: @@ -278,24 +281,37 @@ class CultMeshLocalServer: if result.disconnected: peers.pop(remote_addr, None) continue - for delivered in result.delivered: - if delivered.channel_id != "schema": - continue - try: - message = msgpack.unpackb(delivered.payload, raw=False) - except (msgpack.ExtraData, msgpack.FormatError, msgpack.StackError, ValueError): - continue - if not isinstance(message, dict): - continue - responses = self._handle_connection_message(message, peer.subscriptions) - for response in responses: - if unsendable is None: - unsendable = self._send_rudp_schema_frame( - rudp_socket, - remote_addr, - peer.session, - msgpack.packb(response, use_bin_type=True), - ) + try: + for delivered in result.delivered: + if delivered.channel_id != "schema": + continue + try: + message = msgpack.unpackb(delivered.payload, raw=False) + except (msgpack.ExtraData, msgpack.FormatError, msgpack.StackError, ValueError): + continue + if not isinstance(message, dict): + continue + responses = self._handle_connection_message(message, peer.subscriptions) + for response in responses: + if unsendable is None: + unsendable = self._send_rudp_schema_frame( + rudp_socket, + remote_addr, + peer.session, + msgpack.packb(response, use_bin_type=True), + ) + except Exception as error: # noqa: BLE001 - one peer's message never ends the thread + # Whatever a peer's message makes the handling raise is that peer's failure. + # The session recorded the frame's sequence, so it cannot be kept: a retransmit + # would be acknowledged and never handled. The session ends, never the thread, + # and the peer is told. The log names the error type only, because the error's + # own message can quote what the peer sent. + _LOGGER.warning( + "CultMesh RUDP server could not handle a peer's message (%s); that peer's session ended.", + type(error).__name__, + ) + self._end_refused_peer(rudp_socket, peers, remote_addr) + continue if unsendable is None and ( packet.reliable or packet.packet_type == CultNetRudpPacketType.DATA or result.delivered ): diff --git a/packages/cultmesh-py/tests/test_cultmesh.py b/packages/cultmesh-py/tests/test_cultmesh.py index 7c26949d..9b936ca1 100644 --- a/packages/cultmesh-py/tests/test_cultmesh.py +++ b/packages/cultmesh-py/tests/test_cultmesh.py @@ -4823,6 +4823,37 @@ class CultMeshRudpSendFailureTests(unittest.TestCase): finally: server.stop() + def test_a_message_whose_handling_fails_ends_only_that_peers_session(self) -> None: + import msgpack # type: ignore + + canary = "canary-7f3e1c" + # Well-formed frames whose fields have the wrong type: handling raises TypeError. + frames = [ + {"schemaVersion": "cultnet.database_subscribe.v0", "subscriptionId": canary, "schemaIds": 5}, + {"schemaVersion": "cultnet.database_subscribe.v0", "subscriptionId": canary, "recordKeys": True}, + ] + server = CultMesh.serve_node(CultMesh.create_node(runtime_id="mesh-server")) + try: + y, _ = self._client(server, "peer-y") + for frame in frames: + with self.subTest(frame=frame): + x, _ = self._client(server, "peer-x") + with self.assertLogs("cultmesh_py.server", level="WARNING") as logs: + x.send("schema", msgpack.packb(frame, use_bin_type=True)) + for _ in range(200): + x.receive_once() + if x.disconnect_reason is not None: + break + time.sleep(0.005) + self.assertEqual(x.disconnect_reason, b"session refused a packet") + self.assertTrue(server._rudp_thread.is_alive()) + self.assertIsNotNone(self._served(y), "Y is still served") + self.assertEqual(len(logs.output), 1) + self.assertIn("TypeError", logs.output[0]) + self.assertNotIn(canary, logs.output[0]) + finally: + server.stop() + def _raw_peer(self, server: Any) -> tuple[socket.socket, Any]: """A peer driven packet by packet, to deliver frames in an order a client would not.""" from cultnet_py import CultNetRudpSession, CultNetRudpSessionOptions, encode_rudp_packet