@@ -102,12 +102,16 @@ pub const Loop = struct {
102102 // Stops when there is no more I/O events registered on the loop.
103103 // Note that I/O events callbacks might register more I/O events
104104 // on the go when they are executed (ie. nested I/O events).
105- pub fn run (self : * Self ) ! void {
105+ pub fn run (self : * Self , wait_time : usize ) ! void {
106106 // stop repeating / interval timeouts from re-registering
107107 self .stopping = true ;
108108 defer self .stopping = false ;
109109
110- while (self .pending_network_count != 0 or self .pending_timeout_count != 0 ) {
110+ const max_iterations = wait_time / (std .time .ns_per_ms * 10 );
111+ for (0.. max_iterations ) | _ | {
112+ if (self .pending_network_count == 0 and self .pending_timeout_count == 0 ) {
113+ break ;
114+ }
111115 self .io .run_for_ns (std .time .ns_per_ms * 10 ) catch | err | {
112116 log .err (.loop , "deinit" , .{ .err = err });
113117 break ;
@@ -187,12 +191,6 @@ pub const Loop = struct {
187191 }
188192
189193 pub fn timeout (self : * Self , nanoseconds : u63 , callback_node : ? * CallbackNode ) ! usize {
190- if (self .stopping ) {
191- // Prevents a timeout callback from creating a new timeout, which
192- // would make `loop.run` run forever.
193- return 0 ;
194- }
195-
196194 const completion = try self .alloc .create (Completion );
197195 errdefer self .alloc .destroy (completion );
198196 completion .* = undefined ;
0 commit comments