Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Stop TLS-outage tests from erasing each other

justrach635e520b9ba1
diff --git a/src/agent_tls_retry_tests.zig b/src/agent_tls_retry_tests.zig
index 18980fba..302b67e9 100644
--- a/src/agent_tls_retry_tests.zig
+++ b/src/agent_tls_retry_tests.zig
@@ -44,6 +44,9 @@ const Fixture = struct {
previous_cancel: bool,
fn init(self: *Fixture) !void {
+ // Serialize: init installs (and clears) the process-global TLS test hooks.
+ clients.lockTestHooksForTest();
+ errdefer clients.unlockTestHooksForTest();
const gpa = std.testing.allocator;
const io = std.testing.io;
self.runtime.init(gpa, io);
@@ -79,7 +82,10 @@ const Fixture = struct {
}
fn serve(self: *Fixture, replies: []const transport.Reply) void {
- self.server_future = std.testing.io.async(transport.serveReplies, .{ std.testing.io, &self.server, replies, &self.accepted });
+ // Eager start: io.async may run inline, so a peer awaited only in deinit
+ // is not guaranteed to be serving while the request under test runs.
+ self.server_future = std.testing.io.concurrent(transport.serveReplies, .{ std.testing.io, &self.server, replies, &self.accepted }) catch
+ @panic("test server task could not start");
}
fn deinit(self: *Fixture) void {
@@ -95,6 +101,7 @@ const Fixture = struct {
self.arena.deinit();
Agent.esc_cancel.store(self.previous_cancel, .release);
current_probe = null;
+ clients.unlockTestHooksForTest();
}
fn expectReleased(self: *Fixture) !void {
diff --git a/src/http_client.zig b/src/http_client.zig
index 654ac350..830fddae 100644
--- a/src/http_client.zig
+++ b/src/http_client.zig
@@ -220,6 +220,10 @@ var g_ca_warm_failed: std.atomic.Value(bool) = .init(false);
const no_test_failure = std.math.maxInt(u64);
var g_test_fail_generation: std.atomic.Value(u64) = .init(no_test_failure);
var g_test_fail_through_generation: std.atomic.Value(u64) = .init(no_test_failure);
+/// These hooks are process-global and `installForTest` clears them, so tests
+/// that inject an outage must not overlap: one test's init would otherwise
+/// erase another's injection mid-request and change its attempt ladder.
+var g_test_hooks_mutex: Io.Mutex = .init;
var g_test_fail_ca_warm: std.atomic.Value(bool) = .init(false);
var g_test_fail_generation_alloc: std.atomic.Value(bool) = .init(false);
var g_test_tls_arrivals: ?*std.atomic.Value(usize) = null;
@@ -282,6 +286,18 @@ pub fn installConstructionTlsBarrierForTest(arrivals: *std.atomic.Value(usize),
g_test_tls_release = release;
}
+/// Serialize a test that both injects these hooks and runs requests under them.
+/// Pair every lock with an unlock on the same thread, including error paths.
+pub fn lockTestHooksForTest() void {
+ if (comptime !builtin.is_test) return;
+ g_test_hooks_mutex.lockUncancelable(std.testing.io);
+}
+
+pub fn unlockTestHooksForTest() void {
+ if (comptime !builtin.is_test) return;
+ g_test_hooks_mutex.unlock(std.testing.io);
+}
+
inline fn resetTestHooks() void {
if (comptime !builtin.is_test) return;
g_test_wait_entered = null;
diff --git a/src/http_client_integration_tests.zig b/src/http_client_integration_tests.zig
index 8ce00b2b..b10a3b61 100644
--- a/src/http_client_integration_tests.zig
+++ b/src/http_client_integration_tests.zig
@@ -311,6 +311,9 @@ test "child retry ladder recovers within the same request" {
}
test "later root and child recover after the retry ladder exhausts TLS generations" {
+ // Shares the process-global TLS hooks with agent_tls_retry_tests.zig's tests.
+ http_client.lockTestHooksForTest();
+ defer http_client.unlockTestHooksForTest();
const gpa = std.testing.allocator;
const io = std.testing.io;
var runtime: http_client.Runtime = undefined;
diff --git a/src/http_send_test.zig b/src/http_send_test.zig
index d944958d..43c18d93 100644
--- a/src/http_send_test.zig
+++ b/src/http_send_test.zig
@@ -7,7 +7,7 @@ const Agent = @import("agent.zig").Agent;
const http = @import("http.zig");
const agent_stream = @import("agent_stream.zig");
-const head_ms: u64 = 150; // not 30_000: the loopback head exception stays off
+const head_ms: u64 = 400; // not 30_000: the loopback head exception stays off
const paced_len: usize = 2 * 1024 * 1024;
const Peer = struct {
@@ -30,7 +30,8 @@ const Peer = struct {
if (line.len > name.len and std.ascii.eqlIgnoreCase(line[0..name.len], name))
len = std.fmt.parseInt(usize, std.mem.trim(u8, line[name.len..], " \r"), 10) catch return;
}
- // 32 KiB per 10 ms: the whole body takes several head budgets.
+ // 32 KiB per 25 ms: the whole body spans several head budgets, and each
+ // pause stays far under one so a descheduled peer cannot trip it.
var since_pause: usize = 0;
while (got.load(.monotonic) < len) {
const n = @min(len - got.load(.monotonic), 8192);
@@ -39,7 +40,7 @@ const Peer = struct {
since_pause += n;
if (since_pause >= 32 * 1024) {
since_pause = 0;
- io.sleep(.fromMilliseconds(10), .awake) catch return;
+ io.sleep(.fromMilliseconds(25), .awake) catch return;
}
}
var output_buf: [1024]u8 = undefined;