Merge pull request #1678 from ludovic111/fix/no-play-packets-in-reconfig

This commit is contained in:
Ludovic Marie
2026-10-25 02:38:40 +02:00
committed by GitHub
+65 -2
View File
@@ -147,7 +147,12 @@ impl Session {
pub fn poll(&self) -> Vec<Event> {
self.events.try_iter().collect()
}
/// Send a play packet; silently dropped once the connection is gone.
/// Send a play packet; silently dropped once the connection is gone,
/// and while the server has the connection back in configuration (a
/// proxy switching servers). Vanilla's `handleConfigurationStart` clears
/// the level before it acknowledges, so no play packet follows the
/// acknowledgement; one that did would reach the server as a
/// configuration packet, and Velocity drops such a client.
pub fn send(&self, packet: play::Serverbound) {
if self.debug
&& !matches!(
@@ -159,7 +164,11 @@ impl Session {
{
eprintln!("[net] -> {packet:?}");
}
if let Some(w) = self.writer.lock().unwrap().as_mut()
let mut writer = self.writer.lock().unwrap();
if self.configuring.load(Ordering::Relaxed) {
return;
}
if let Some(w) = writer.as_mut()
&& w.send(&packet.encode()).is_err()
{
w.shutdown();
@@ -686,6 +695,60 @@ mod tests {
assert!(thread.join().unwrap().is_err());
}
#[test]
fn play_packets_wait_out_a_reconfiguration() {
let (client, server) = horizon_protocol::connection::Pipe::pair();
let (mut reader, writer) = Connection::local(client).split();
let (send, events) = channel();
let session = Session {
events,
writer: Arc::new(Mutex::new(Some(writer))),
configuring: Arc::new(AtomicBool::new(false)),
thread: None,
address: "local".into(),
name: "Player".into(),
counters: Arc::new(Mutex::new(None)),
debug: false,
};
let (thread_writer, thread_configuring) =
(session.writer.clone(), session.configuring.clone());
let thread = std::thread::spawn(move || {
receive_play(
&mut reader,
&thread_writer,
&thread_configuring,
&Language::new(),
&send,
)
});
let mut server = Connection::local(server);
let tick_end = play::Serverbound::ClientTickEnd(Empty);
server
.send(&play::Clientbound::StartConfiguration(Empty).encode())
.unwrap();
let (id, body) = server.recv().unwrap();
assert_eq!(
play::Serverbound::decode(id, &body).unwrap(),
play::Serverbound::ConfigurationAcknowledged(Empty)
);
// The game thread still ticks the old level: nothing of it goes out.
session.send(tick_end.clone());
server
.send(&cfg::Clientbound::FinishConfiguration(Empty).encode())
.unwrap();
let (id, body) = server.recv().unwrap();
assert_eq!(
cfg::Serverbound::decode(id, &body).unwrap(),
cfg::Serverbound::FinishConfiguration(Empty)
);
// Back in play, the game's packets flow again.
session.send(tick_end.clone());
let (id, body) = server.recv().unwrap();
assert_eq!(play::Serverbound::decode(id, &body).unwrap(), tick_end);
drop(session);
assert!(thread.join().unwrap().is_err());
}
#[test]
fn server_brand_payload_is_decoded_and_invalid_or_unrelated_payloads_are_ignored() {
let (send, events) = channel();