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
This commit is contained in:
@@ -146,11 +146,35 @@ pub const TestP2PPair = struct {
|
|||||||
connect_addr.port = self.server_port;
|
connect_addr.port = self.server_port;
|
||||||
connect_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } };
|
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| {
|
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});
|
std.debug.print("TestP2PPair: Connection failed: {}\n", .{err});
|
||||||
return 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
|
// Wait for connection to be established
|
||||||
self.waitForConnection(timeout_ms) catch |err| {
|
self.waitForConnection(timeout_ms) catch |err| {
|
||||||
std.debug.print("TestP2PPair: Connection timeout\n", .{});
|
std.debug.print("TestP2PPair: Connection timeout\n", .{});
|
||||||
@@ -200,6 +224,18 @@ pub const TestP2PPair = struct {
|
|||||||
return self.connected_peer_id;
|
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
|
/// Check if both instances are connected
|
||||||
pub fn isConnected(self: *Self) bool {
|
pub fn isConnected(self: *Self) bool {
|
||||||
return self.server.getStats().active_connections.load(.acquire) > 0 and
|
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
|
// Self-tests for the helper
|
||||||
test "TestP2PPair basic init and deinit" {
|
test "TestP2PPair basic init and deinit" {
|
||||||
var pair = try TestP2PPair.init(testing.allocator);
|
var pair = try TestP2PPair.init(testing.allocator);
|
||||||
|
|||||||
Reference in New Issue
Block a user