Skip to content

Commit e301ba0

Browse files
committed
Don't share or reuse timeout_completion
Results in undefined behavior when a client disconnects and another reconnects while the timeout is being monitored: #436 (comment)
1 parent b12eef2 commit e301ba0

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/server.zig

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,20 @@ const Server = struct {
6464
// a memory poor for our Clietns
6565
client_pool: std.heap.MemoryPool(Client),
6666

67+
timeout_completion_pool: std.heap.MemoryPool(Completion),
68+
6769
// I/O fields
6870
conn_completion: Completion,
6971
close_completion: Completion,
7072
accept_completion: Completion,
71-
timeout_completion: Completion,
7273

7374
// The response to send on a GET /json/version request
7475
json_version_response: []const u8,
7576

7677
fn deinit(self: *Server) void {
7778
self.send_pool.deinit();
7879
self.client_pool.deinit();
80+
self.timeout_completion_pool.deinit();
7981
self.allocator.free(self.json_version_response);
8082
}
8183

@@ -121,11 +123,16 @@ const Server = struct {
121123
}
122124

123125
fn queueTimeout(self: *Server) void {
126+
const completion = self.timeout_completion_pool.create() catch |err| {
127+
log.err("failed to create timeout completion: {any}", .{err});
128+
return;
129+
};
130+
124131
self.loop.io.timeout(
125132
*Server,
126133
self,
127134
callbackTimeout,
128-
&self.timeout_completion,
135+
completion,
129136
TimeoutCheck,
130137
);
131138
}
@@ -135,8 +142,7 @@ const Server = struct {
135142
completion: *Completion,
136143
result: TimeoutError!void,
137144
) void {
138-
std.debug.assert(completion == &self.timeout_completion);
139-
145+
self.timeout_completion_pool.destroy(completion);
140146
const client = self.client orelse return;
141147

142148
if (result) |_| {
@@ -1008,10 +1014,10 @@ pub fn run(
10081014
.conn_completion = undefined,
10091015
.close_completion = undefined,
10101016
.accept_completion = undefined,
1011-
.timeout_completion = undefined,
10121017
.json_version_response = json_version_response,
10131018
.send_pool = std.heap.MemoryPool(Send).init(allocator),
10141019
.client_pool = std.heap.MemoryPool(Client).init(allocator),
1020+
.timeout_completion_pool = std.heap.MemoryPool(Completion).init(allocator),
10151021
};
10161022
defer server.deinit();
10171023

0 commit comments

Comments
 (0)