From c45edf5858a4caebefbeb750a5dc4164a6d2386c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 30 Sep 2026 09:53:06 +0200 Subject: [PATCH] Ack Cut 1d batch 2 (Python): an Ack does not retire a Connect awaiting its Accept; the fresh attempt jumps by the receive window less one While a Connect awaits its Accept, only an Accept the client honours retires it. An Ack that names the Connect (a server's reply to a repeat or a stale copy) no longer wedges the client with nothing to resend. The Connect payload lives on the session, so a fresh attempt can always be built. The fresh attempt uses the abandoned sequence plus the receive window less one (serial), so a late copy of the abandoned Connect stays inside the new one's stale window; only a session's first Connect draws at random. Co-Authored-By: Claude Sonnet 5.5 --- .../cultnet-py/src/cultnet_py/transport.py | 45 +++++++----- packages/cultnet-py/tests/test_cultnet.py | 30 ++++++++ .../tests/test_rudp_connect_generation.py | 72 +++++++++++++++++++ 3 files changed, 128 insertions(+), 19 deletions(-) diff --git a/packages/cultnet-py/src/cultnet_py/transport.py b/packages/cultnet-py/src/cultnet_py/transport.py index fee21861..355faa51 100644 --- a/packages/cultnet-py/src/cultnet_py/transport.py +++ b/packages/cultnet-py/src/cultnet_py/transport.py @@ -300,16 +300,12 @@ class CultNetRudpReceiveResult: _RUDP_CONNECT_ATTEMPT_MS = 3_000 -def _draw_sequence(floor: int) -> int: - """A sequence drawn from a secure source in [floor, 2^31). The Connect's +def _draw_sequence() -> int: + """A sequence drawn from a secure source in [1, 2^31). The Connect's sequence is what tells a peer whether a Connect repeats one it already accepted or starts a new session, so two sessions must not share one by - default. A floor above the range draws nothing new: the caller keeps its own - sequence.""" - low = max(1, floor) - if low >= 2**31: - return low - return low + secrets.randbelow(2**31 - low) + default.""" + return 1 + secrets.randbelow(2**31 - 1) def _at_or_before(sequence: int, mark: int, window: int) -> bool: @@ -450,7 +446,7 @@ class CultNetRudpSession: raise ValueError("RUDP max_pending_reliable_packets must be greater than zero") self.max_pending_reliable_packets = options.max_pending_reliable_packets self._next_sequence = _uint32( - _draw_sequence(1) if options.initial_sequence is None else options.initial_sequence, + _draw_sequence() if options.initial_sequence is None else options.initial_sequence, "initial_sequence", ) if self._next_sequence == 0xFFFFFFFF: @@ -474,6 +470,9 @@ class CultNetRudpSession: # When the unanswered Connect first went out; `due_resends` abandons it # for a fresh attempt once _RUDP_CONNECT_ATTEMPT_MS has passed. self._connect_started_at_ms = 0 + # The payload of the Connect this side sent, kept so a fresh attempt can + # always be built whatever became of the pending packet. + self._connect_payload = b"" self._last_received_at_ms: int | None = None self._highest_received_sequence: int | None = None self._received_sequences: set[int] = set() @@ -574,6 +573,7 @@ class CultNetRudpSession: self._connect_sequence = packet.sequence self._awaiting_accept = True self._connect_started_at_ms = now_ms + self._connect_payload = packet.payload self._track_reliable(packet, now_ms) return packet @@ -725,7 +725,11 @@ class CultNetRudpSession: ) if packet.packet_type == CultNetRudpPacketType.ACCEPT and not honours_accept: return CultNetRudpReceiveResult() - self._apply_acknowledgements(packet) + # While this side's Connect awaits its Accept, only the Accept it + # honours retires it: an Ack that names the Connect (a server's reply to + # a repeat or a stale copy) says the server did not start a session. + if honours_accept or not self._awaiting_accept: + self._apply_acknowledgements(packet) ready_to_send = self._promote_queued_reliable(now_ms) self._last_received_at_ms = now_ms @@ -854,18 +858,21 @@ class CultNetRudpSession: def _abandon_unanswered_connect(self, now_ms: int) -> CultNetRudpPacket | None: """A Connect unanswered for _RUDP_CONNECT_ATTEMPT_MS is replaced, not - retransmitted further, by a Connect with a newly drawn sequence and the - same payload. Nothing was ever sent in an unanswered generation, so no - sequence issued so far is owed; the draw stays above them, so a frame the - peer still remembers from an earlier generation of this session stays at - or before the new Connect.""" + retransmitted further, by a Connect with the same payload and the + abandoned sequence plus the receive window less one. Nothing was ever + sent in an unanswered generation, so no sequence issued so far is owed. + The jump keeps a late copy of the abandoned Connect inside the new one's + stale window, so a server that took the new Connect answers the copy + with an Ack instead of restarting; and it leaves the stale window of + whatever generation the server holds, unless that generation sits + exactly at the jump, and then the next attempt leaves it. The first + Connect of a session is the only one drawn at random.""" if not self._awaiting_accept or now_ms - self._connect_started_at_ms < _RUDP_CONNECT_ATTEMPT_MS: return None - pending = self._pending_reliable.get(self._connect_sequence) if self._connect_sequence is not None else None - if pending is None: + if self._connect_sequence is None: return None - self._next_sequence = _draw_sequence(self._next_sequence) - return self.create_connect(now_ms, pending.packet.payload) + self._next_sequence = min(self._connect_sequence + self.RECEIVED_SEQUENCE_WINDOW - 1, 0xFFFFFFFE) + return self.create_connect(now_ms, self._connect_payload) def _create_packet( self, diff --git a/packages/cultnet-py/tests/test_cultnet.py b/packages/cultnet-py/tests/test_cultnet.py index bb7dfdcc..fb2eff54 100644 --- a/packages/cultnet-py/tests/test_cultnet.py +++ b/packages/cultnet-py/tests/test_cultnet.py @@ -1204,6 +1204,36 @@ class CultNetTests(unittest.TestCase): peer_socket.close() server.close() + def test_cultnet_rudp_server_mode_admits_a_pinned_client_that_restarts_on_the_same_address(self) -> None: + server, _, peer_socket, _, to_server, drain_peer = self._server_mode_pair(0x1020305C) + connection_id = 0x1020305C + try: + first = CultNetRudpSession(CultNetRudpSessionOptions(connection_id=connection_id, initial_sequence=1)) + to_server(first.create_connect(0, b"join")) + server.receive_once() + accept = [p for p in drain_peer() if p.packet_type == CultNetRudpPacketType.ACCEPT][0] + first.receive(accept, 0) + to_server(first.create_ack_for_received(accept.sequence)) + server.receive_once() + drain_peer() + + # The process restarted on the same address with the same pinned sequence. + restarted = CultNetRudpSession(CultNetRudpSessionOptions(connection_id=connection_id, initial_sequence=1)) + to_server(restarted.create_connect(0, b"join")) + server.receive_once() + for packet in drain_peer(): + restarted.receive(packet, 1) + self.assertFalse(restarted.connected) + + to_server(restarted.due_resends(3_000)[0]) + server.receive_once() + for packet in drain_peer(): + restarted.receive(packet, 3_001) + self.assertTrue(restarted.connected, "the restarted pinned client was never admitted") + finally: + peer_socket.close() + server.close() + def test_cultnet_rudp_server_mode_admits_a_connect_from_a_new_endpoint(self) -> None: server, server_socket, peer_socket, peer, to_server, drain_peer = self._server_mode_pair(0x10203059) other_socket = bind_udp_socket() diff --git a/packages/cultnet-py/tests/test_rudp_connect_generation.py b/packages/cultnet-py/tests/test_rudp_connect_generation.py index 6f68ae62..4bbd896a 100644 --- a/packages/cultnet-py/tests/test_rudp_connect_generation.py +++ b/packages/cultnet-py/tests/test_rudp_connect_generation.py @@ -351,5 +351,77 @@ class CultNetRudpConnectGenerationTests(unittest.TestCase): self.assertTrue(server.check_timeout(100_000, 1_000)) + # Ack Cut 1d, batch 2: the connect-attempt timeout. + + def test_an_ack_naming_the_pending_connect_does_not_retire_it(self) -> None: + server, old = session(500), session(1) + old_accept = server.accept_connect(old.create_connect(0), 0) + old.receive(old_accept, 0) + server.receive(old.create_ack_for_received(old_accept.sequence), 0) + + restarted = session(1) + first = restarted.create_connect(0, b"join") + reply = server.accept_connect(first, 1) + self.assertEqual(reply.packet_type, CultNetRudpPacketType.ACK) + self.assertTrue(self.names_sequence(reply, first.sequence), "the Ack must name the pending Connect for this test to bite") + restarted.receive(reply, 1) + self.assertFalse(restarted.connected) + self.assertEqual(restarted.pending_reliable_sequences, (first.sequence,), "an Ack retired the Connect") + + fresh = restarted.due_resends(3_000) + self.assertEqual(len(fresh), 1, "the client waits for ever with nothing to resend") + self.assertEqual(fresh[0].payload, b"join") + accept = server.accept_connect(fresh[0], 3_000) + self.assertEqual(accept.packet_type, CultNetRudpPacketType.ACCEPT) + restarted.receive(accept, 3_000) + self.assertTrue(restarted.connected) + + def test_a_late_copy_of_an_abandoned_connect_is_stale_once_its_replacement_is_accepted(self) -> None: + server, client = session(500), session(10) + abandoned = client.create_connect(0) + fresh = client.due_resends(3_000) + self.assertEqual(len(fresh), 1) + self.assertEqual(fresh[0].sequence, abandoned.sequence + 4_095) + accept = server.accept_connect(fresh[0], 3_001) + client.receive(accept, 3_002) + server.receive(client.create_ack_for_received(accept.sequence), 3_002) + self.assertTrue(client.connected) + a = send(client, "a") + self.assertEqual(names(server.receive(a, 3_003)), ["a"]) + client.receive(server.create_ack_for_received(a.sequence), 3_003) + + self.assertTrue(server.connect_repeats(abandoned), "the late copy would restart the server") + reply = server.accept_connect(abandoned, 3_004) + self.assertEqual(reply.packet_type, CultNetRudpPacketType.ACK) + client.receive(reply, 3_005) + self.assertTrue(client.connected) + self.assertEqual(names(server.receive(send(client, "b"), 3_006)), ["b"]) + + def test_a_server_sitting_at_the_jump_is_left_by_the_next_attempt(self) -> None: + server, old = session(500), session(5_095) + old_accept = server.accept_connect(old.create_connect(0), 0) + old.receive(old_accept, 0) + server.receive(old.create_ack_for_received(old_accept.sequence), 0) + + restarted = session(1_000) + first = restarted.create_connect(0, b"join") + restarted.receive(server.accept_connect(first, 1), 1) + self.assertFalse(restarted.connected) + + second = restarted.due_resends(3_000)[0] + self.assertEqual(second.sequence, 5_095, "the jump lands on the server's generation") + reply = server.accept_connect(second, 3_001) + self.assertEqual(reply.packet_type, CultNetRudpPacketType.ACK) + restarted.receive(reply, 3_001) + self.assertFalse(restarted.connected) + + third = restarted.due_resends(6_000)[0] + self.assertEqual(third.sequence, 5_095 + 4_095) + accept = server.accept_connect(third, 6_001) + self.assertEqual(accept.packet_type, CultNetRudpPacketType.ACCEPT) + restarted.receive(accept, 6_001) + self.assertTrue(restarted.connected) + + if __name__ == "__main__": unittest.main()