mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
endpoint example: added comment: race cond fixed
This commit is contained in:
parent
e2e11c8976
commit
cdb7308a23
1 changed files with 2 additions and 1 deletions
|
@ -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();
|
||||
|
|
Loading…
Add table
Reference in a new issue