From 37a74e1ca90d26dc48106e5192311d1e8b7e6b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammed=20Tanr=C4=B1kulu?= Date: Tue, 10 Feb 2026 20:15:01 -0300 Subject: [PATCH] fix: "test-connected-coverage" Noise handshake deadlock. spawn background server update thread during connectToPeer when Noise is enabled so the 3 message handshake can complete in single threaded tests --- tests/helpers/test_p2p_pair.zig | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/helpers/test_p2p_pair.zig b/tests/helpers/test_p2p_pair.zig index 3a58308..d440442 100644 --- a/tests/helpers/test_p2p_pair.zig +++ b/tests/helpers/test_p2p_pair.zig @@ -146,11 +146,35 @@ pub const TestP2PPair = struct { connect_addr.port = self.server_port; connect_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } }; + // When Noise encryption is enabled, connectToPeer() blocks during the + // Noise_XX handshake (3 message round-trips). In a single-threaded test, + // this deadlocks because the server never gets to call update()/accept(). + // Solve by running the server's update loop on a background thread. + var server_done = atomic.Value(bool).init(false); + var server_thread: ?std.Thread = null; + + if (self.server.noise_enabled) { + server_thread = std.Thread.spawn(.{}, serverUpdateLoop, .{ self.server, &server_done }) catch { + return Error.InvalidState; + }; + } + self.connected_peer_id = self.client.connectToPeer(connect_addr) catch |err| { + // Signal thread to stop before returning error + if (server_thread) |t| { + server_done.store(true, .release); + t.join(); + } std.debug.print("TestP2PPair: Connection failed: {}\n", .{err}); return err; }; + // Stop the server background thread now that handshake is complete + if (server_thread) |t| { + server_done.store(true, .release); + t.join(); + } + // Wait for connection to be established self.waitForConnection(timeout_ms) catch |err| { std.debug.print("TestP2PPair: Connection timeout\n", .{}); @@ -200,6 +224,18 @@ pub const TestP2PPair = struct { return self.connected_peer_id; } + /// Get the peer ID that the server assigned to the client connection. + /// This is different from getPeerId() which returns the client's view of the server. + pub fn getServerSidePeerId(self: *Self) ?PeerId { + self.server.peer_connections_mutex.lock(); + defer self.server.peer_connections_mutex.unlock(); + var iter = self.server.peer_connections.iterator(); + if (iter.next()) |entry| { + return entry.key_ptr.*; + } + return null; + } + /// Check if both instances are connected pub fn isConnected(self: *Self) bool { return self.server.getStats().active_connections.load(.acquire) > 0 and @@ -254,6 +290,15 @@ pub const TestP2PPair = struct { } }; +/// Background thread function that runs server.update() in a loop. +/// Used during Noise handshake to prevent single-threaded deadlock. +fn serverUpdateLoop(server: *P2P, done: *atomic.Value(bool)) void { + while (!done.load(.acquire)) { + server.update() catch {}; + std.Thread.sleep(5 * std.time.ns_per_ms); + } +} + // Self-tests for the helper test "TestP2PPair basic init and deinit" { var pair = try TestP2PPair.init(testing.allocator);