Skip to content

Commit 94ce121

Browse files
committed
Retry ping request on network error with keepalive: false
1 parent 1fad6a2 commit 94ce121

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
cannot be negative numbers.
77
* Timestamps were observed to be negative in a few occurrences, for platforms that do not provide the `performance.now` API, namely QML, and in which we fallback to the `Date.now` API.
88
* If event timestamps are negative pings are rejected by the pipeline.
9+
* [#TODO](https://github.com/mozilla/glean.js/pull/TODO): Retry ping request on network error with `keepalive: false`. This is sometimes the issue Chrome browsers below v81.
910

1011
# v0.31.0 (2022-01-25)
1112

glean/src/platform/browser/uploader.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { DEFAULT_UPLOAD_TIMEOUT_MS, UploadResultStatus, UploadResult } from "../
99
const LOG_TAG = "platform.browser.Uploader";
1010

1111
class BrowserUploader extends Uploader {
12-
async post(url: string, body: string | Uint8Array, headers: Record<string, string> = {}): Promise<UploadResult> {
12+
async post(
13+
url: string,
14+
body: string | Uint8Array,
15+
headers: Record<string, string> = {},
16+
keepalive = true
17+
): Promise<UploadResult> {
1318
const controller = new AbortController();
1419
const timeout = setTimeout(() => controller.abort(), DEFAULT_UPLOAD_TIMEOUT_MS);
1520

@@ -19,7 +24,7 @@ class BrowserUploader extends Uploader {
1924
headers,
2025
method: "POST",
2126
body: body,
22-
keepalive: true,
27+
keepalive,
2328
// Strips any cookies or authorization headers from the request.
2429
credentials: "omit",
2530
signal: controller.signal,
@@ -31,6 +36,13 @@ class BrowserUploader extends Uploader {
3136
if (e instanceof DOMException) {
3237
log(LOG_TAG, ["Timeout while attempting to upload ping.\n", e], LoggingLevel.Error);
3338
} else if (e instanceof TypeError) {
39+
if (keepalive) {
40+
// Try again without `keepalive`, because that may be the issue.
41+
// This problem was observed in chromium versions below v81.
42+
// See: https://chromium.googlesource.com/chromium/src/+/26d70b36dd1c18244fb17b91d275332c8b73eab3
43+
return this.post(url, body, headers, false);
44+
}
45+
3446
// From MDN: "A fetch() promise will reject with a TypeError
3547
// when a network error is encountered or CORS is misconfigured on the server-side,
3648
// although this usually means permission issues or similar"

0 commit comments

Comments
 (0)