mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +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);
|
var l: std.ArrayList(User) = std.ArrayList(User).init(self.alloc);
|
||||||
defer l.deinit();
|
defer l.deinit();
|
||||||
|
|
||||||
|
// the potential race condition is fixed by jsonifying with the mutex locked
|
||||||
var it = JsonUserIteratorWithRaceCondition.init(&self.users);
|
var it = JsonUserIteratorWithRaceCondition.init(&self.users);
|
||||||
while (it.next()) |user| {
|
while (it.next()) |user| {
|
||||||
try l.append(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!
|
// causing it to GROW -> realloc -> all slices get invalidated!
|
||||||
//
|
//
|
||||||
// So, to mitigate that, either:
|
// 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
|
// - or: the iterator must make copies of the strings
|
||||||
self.lock.lock();
|
self.lock.lock();
|
||||||
defer self.lock.unlock();
|
defer self.lock.unlock();
|
||||||
|
|
Loading…
Add table
Reference in a new issue