Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions src/runtime/loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ pub const Loop = struct {

// run tail events. We do run the tail events to ensure all the
// contexts are correcly free.
while (self.hasPendinEvents()) {
self.io.run_for_ns(10 * std.time.ns_per_ms) catch |err| {
while (self.pending_network_count != 0 or self.pending_timeout_count != 0) {
self.io.run_for_ns(std.time.ns_per_ms * 10) catch |err| {
log.err(.loop, "deinit", .{ .err = err });
break;
};
}

if (comptime CANCEL_SUPPORTED) {
self.io.cancel_all();
}
Expand All @@ -96,21 +97,6 @@ pub const Loop = struct {
self.cancelled.deinit(self.alloc);
}

// We can shutdown once all the pending network IO is complete.
// In debug mode we also wait until al the pending timeouts are complete
// but we only do this so that the `timeoutCallback` can free all allocated
// memory and we won't report a leak.
fn hasPendinEvents(self: *const Self) bool {
if (self.pending_network_count > 0) {
return true;
}

if (builtin.mode != .Debug) {
return false;
}
return self.pending_timeout_count > 0;
}

// Retrieve all registred I/O events completed by OS kernel,
// and execute sequentially their callbacks.
// Stops when there is no more I/O events registered on the loop.
Expand All @@ -121,9 +107,11 @@ pub const Loop = struct {
self.stopping = true;
defer self.stopping = false;

while (self.pending_network_count > 0) {
try self.io.run_for_ns(10 * std.time.ns_per_ms);
// at each iteration we might have new events registred by previous callbacks
while (self.pending_network_count != 0 or self.pending_timeout_count != 0) {
self.io.run_for_ns(std.time.ns_per_ms * 10) catch |err| {
log.err(.loop, "deinit", .{ .err = err });
break;
};
}
}

Expand Down