Skip to content

Commit a00c234

Browse files
authored
Merge pull request #802 from lightpanda-io/endless_loop_fix_and_dot_slash_stitch
Don't allow timeouts to be registered when shutting down
2 parents cb35b36 + 2565409 commit a00c234

File tree

6 files changed

+46
-21
lines changed

6 files changed

+46
-21
lines changed

src/browser/page.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ pub const Page = struct {
144144
return self.fetchData("module", src);
145145
}
146146

147-
pub fn wait(self: *Page) !void {
147+
pub fn wait(self: *Page, wait_ns: usize) !void {
148148
var try_catch: Env.TryCatch = undefined;
149149
try_catch.init(self.main_context);
150150
defer try_catch.deinit();
151151

152-
try self.session.browser.app.loop.run();
152+
try self.session.browser.app.loop.run(wait_ns);
153153

154154
if (try_catch.hasCaught() == false) {
155155
log.debug(.browser, "page wait complete", .{});

src/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn run(alloc: Allocator) !void {
126126
},
127127
};
128128

129-
try page.wait();
129+
try page.wait(std.time.ns_per_s * 3);
130130

131131
// dump
132132
if (opts.dump) {

src/main_wpt.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn run(arena: Allocator, test_file: []const u8, loader: *FileLoader, err_out: *?
157157
var try_catch: Env.TryCatch = undefined;
158158
try_catch.init(runner.page.main_context);
159159
defer try_catch.deinit();
160-
try runner.page.loop.run();
160+
try runner.page.loop.run(std.time.ns_per_ms * 200);
161161

162162
if (try_catch.hasCaught()) {
163163
err_out.* = (try try_catch.err(arena)) orelse "unknwon error";

src/runtime/loop.zig

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const MemoryPool = std.heap.MemoryPool;
2323
const log = @import("../log.zig");
2424
pub const IO = @import("tigerbeetle-io").IO;
2525

26+
const RUN_DURATION = 10 * std.time.ns_per_ms;
27+
2628
// SingleThreaded I/O Loop based on Tigerbeetle io_uring loop.
2729
// On Linux it's using io_uring.
2830
// On MacOS and Windows it's using kqueue/IOCP with a ring design.
@@ -82,7 +84,7 @@ pub const Loop = struct {
8284
// run tail events. We do run the tail events to ensure all the
8385
// contexts are correcly free.
8486
while (self.pending_network_count != 0 or self.pending_timeout_count != 0) {
85-
self.io.run_for_ns(std.time.ns_per_ms * 10) catch |err| {
87+
self.io.run_for_ns(RUN_DURATION) catch |err| {
8688
log.err(.loop, "deinit", .{ .err = err });
8789
break;
8890
};
@@ -102,12 +104,16 @@ pub const Loop = struct {
102104
// Stops when there is no more I/O events registered on the loop.
103105
// Note that I/O events callbacks might register more I/O events
104106
// on the go when they are executed (ie. nested I/O events).
105-
pub fn run(self: *Self) !void {
107+
pub fn run(self: *Self, wait_ns: usize) !void {
106108
// stop repeating / interval timeouts from re-registering
107109
self.stopping = true;
108110
defer self.stopping = false;
109111

110-
while (self.pending_network_count != 0 or self.pending_timeout_count != 0) {
112+
const max_iterations = wait_ns / (RUN_DURATION);
113+
for (0..max_iterations) |_| {
114+
if (self.pending_network_count == 0 and self.pending_timeout_count == 0) {
115+
break;
116+
}
111117
self.io.run_for_ns(std.time.ns_per_ms * 10) catch |err| {
112118
log.err(.loop, "deinit", .{ .err = err });
113119
break;
@@ -187,6 +193,11 @@ pub const Loop = struct {
187193
}
188194

189195
pub fn timeout(self: *Self, nanoseconds: u63, callback_node: ?*CallbackNode) !usize {
196+
if (self.stopping and nanoseconds > std.time.ns_per_ms * 500) {
197+
// we're trying to shutdown, we probably don't want to wait for a new
198+
// long timeout
199+
return 0;
200+
}
190201
const completion = try self.alloc.create(Completion);
191202
errdefer self.alloc.destroy(completion);
192203
completion.* = undefined;

src/testing.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ pub const JsRunner = struct {
435435
}
436436
return err;
437437
};
438-
try self.page.loop.run();
438+
try self.page.loop.run(std.time.ns_per_ms * 200);
439439
@import("root").js_runner_duration += std.time.Instant.since(try std.time.Instant.now(), start);
440440

441441
if (case.@"1") |expected| {

src/url.zig

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ pub const URL = struct {
110110
}
111111
return src;
112112
}
113-
if (src.len == 0) {
113+
114+
var normalized_src = if (std.mem.startsWith(u8, src, "/")) src[1..] else src;
115+
while (std.mem.startsWith(u8, normalized_src, "./")) {
116+
normalized_src = normalized_src[2..];
117+
}
118+
119+
if (normalized_src.len == 0) {
114120
if (opts.alloc == .always) {
115121
return allocator.dupe(u8, base);
116122
}
@@ -125,8 +131,6 @@ pub const URL = struct {
125131
}
126132
};
127133

128-
const normalized_src = if (src[0] == '/') src[1..] else src;
129-
130134
if (std.mem.lastIndexOfScalar(u8, base[protocol_end..], '/')) |index| {
131135
const last_slash_pos = index + protocol_end;
132136
if (last_slash_pos == base.len - 1) {
@@ -216,41 +220,41 @@ test "URL: resolve size" {
216220
test "URL: Stitching Base & Src URLs (Basic)" {
217221
const allocator = testing.allocator;
218222

219-
const base = "https://www.google.com/xyz/abc/123";
223+
const base = "https://lightpanda.io/xyz/abc/123";
220224
const src = "something.js";
221225
const result = try URL.stitch(allocator, src, base, .{});
222226
defer allocator.free(result);
223-
try testing.expectString("https://www.google.com/xyz/abc/something.js", result);
227+
try testing.expectString("https://lightpanda.io/xyz/abc/something.js", result);
224228
}
225229

226230
test "URL: Stitching Base & Src URLs (Just Ending Slash)" {
227231
const allocator = testing.allocator;
228232

229-
const base = "https://www.google.com/";
233+
const base = "https://lightpanda.io/";
230234
const src = "something.js";
231235
const result = try URL.stitch(allocator, src, base, .{});
232236
defer allocator.free(result);
233-
try testing.expectString("https://www.google.com/something.js", result);
237+
try testing.expectString("https://lightpanda.io/something.js", result);
234238
}
235239

236240
test "URL: Stitching Base & Src URLs with leading slash" {
237241
const allocator = testing.allocator;
238242

239-
const base = "https://www.google.com/";
243+
const base = "https://lightpanda.io/";
240244
const src = "/something.js";
241245
const result = try URL.stitch(allocator, src, base, .{});
242246
defer allocator.free(result);
243-
try testing.expectString("https://www.google.com/something.js", result);
247+
try testing.expectString("https://lightpanda.io/something.js", result);
244248
}
245249

246250
test "URL: Stitching Base & Src URLs (No Ending Slash)" {
247251
const allocator = testing.allocator;
248252

249-
const base = "https://www.google.com";
253+
const base = "https://lightpanda.io";
250254
const src = "something.js";
251255
const result = try URL.stitch(allocator, src, base, .{});
252256
defer allocator.free(result);
253-
try testing.expectString("https://www.google.com/something.js", result);
257+
try testing.expectString("https://lightpanda.io/something.js", result);
254258
}
255259

256260
test "URL: Stiching Base & Src URLs (Both Local)" {
@@ -275,11 +279,21 @@ test "URL: Stiching src as full path" {
275279
test "URL: Stitching Base & Src URLs (empty src)" {
276280
const allocator = testing.allocator;
277281

278-
const base = "https://www.google.com/xyz/abc/123";
282+
const base = "https://lightpanda.io/xyz/abc/123";
279283
const src = "";
280284
const result = try URL.stitch(allocator, src, base, .{});
281285
defer allocator.free(result);
282-
try testing.expectString("https://www.google.com/xyz/abc/123", result);
286+
try testing.expectString("https://lightpanda.io/xyz/abc/123", result);
287+
}
288+
289+
test "URL: Stitching dotslash" {
290+
const allocator = testing.allocator;
291+
292+
const base = "https://lightpanda.io/hello/";
293+
const src = "./something.js";
294+
const result = try URL.stitch(allocator, src, base, .{});
295+
defer allocator.free(result);
296+
try testing.expectString("https://lightpanda.io/hello/something.js", result);
283297
}
284298

285299
test "URL: concatQueryString" {

0 commit comments

Comments
 (0)