Skip to content

Commit 11fe793

Browse files
committed
Don't allow timeouts to be registered when shutting down
Currently, a timeout that sets a timeout can cause loop.run to block forever. Also, cleanup URL stitch with leading './'. The resulting URL was valid, but since we use the URL as the module cache key, it's important to resolve various representations of the same URL in the same way.
1 parent bdb2338 commit 11fe793

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/runtime/loop.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ pub const Loop = struct {
187187
}
188188

189189
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+
190196
const completion = try self.alloc.create(Completion);
191197
errdefer self.alloc.destroy(completion);
192198
completion.* = undefined;

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)