From cdb7308a2344be35cf387c342365857f78f05548 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Fri, 20 Jan 2023 20:35:10 +0100 Subject: [PATCH] endpoint example: added comment: race cond fixed --- examples/endpoint/users.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/endpoint/users.zig b/examples/endpoint/users.zig index 4d7c5eb..7e0a825 100644 --- a/examples/endpoint/users.zig +++ b/examples/endpoint/users.zig @@ -122,6 +122,7 @@ pub fn toJSON(self: *Self) ![]const u8 { var l: std.ArrayList(User) = std.ArrayList(User).init(self.alloc); defer l.deinit(); + // the potential race condition is fixed by jsonifying with the mutex locked var it = JsonUserIteratorWithRaceCondition.init(&self.users); while (it.next()) |user| { try l.append(user); @@ -152,7 +153,7 @@ pub fn listWithRaceCondition(self: *Self, out: *std.ArrayList(User)) !void { // causing it to GROW -> realloc -> all slices get invalidated! // // So, to mitigate that, either: - // - listing and converting to JSON must become one locked operation + // - [x] listing and converting to JSON must become one locked operation // - or: the iterator must make copies of the strings self.lock.lock(); defer self.lock.unlock();