Skip to content

Commit c9ef1f4

Browse files
authored
Merge pull request #81 from cloudflare/tdias/issue-77-abort-errors
Don't cancel abort non-existing abort controller. Abort previous controller before replacing (Fixes #77)
2 parents 5bb948b + 36aa9aa commit c9ef1f4

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

src/engines/BandwidthEngine/BandwidthEngine.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class BandwidthMeasurementEngine {
119119

120120
// Public methods
121121
pause() {
122-
this.#cancelCurrentMeasurement();
122+
this.#cancelCurrentMeasurement(`pause()`);
123123
this.#setRunning(false);
124124
}
125125

@@ -157,6 +157,10 @@ class BandwidthMeasurementEngine {
157157
this.#running = running;
158158
setTimeout(() => this.#onRunningChange(this.#running));
159159
}
160+
161+
if (!running) {
162+
this.#currentAbortController?.abort('setRunning(false)');
163+
}
160164
}
161165

162166
#saveMeasurementResults(measIdx, measTiming) {
@@ -271,17 +275,19 @@ class BandwidthMeasurementEngine {
271275
this.#fetchOptions
272276
);
273277

274-
// AbortController and timeout is shared between all retries
275278
if (this.#retries === 0) {
279+
// abort existing abort controller
280+
this.#currentAbortController?.abort('restarting engine');
281+
282+
// create new abort controller
276283
this.#currentAbortController = new AbortController();
277284
if (this.abortRequestDuration) {
278285
const abortTimeout = setTimeout(() => {
279-
this.#cancelCurrentMeasurement();
286+
const errorMessage = `${isDown ? 'Download' : 'Upload'} measurement of ${numBytes} bytes aborted. Measurement exceeded bandwidthAbortRequestDuration (${this.abortRequestDuration}ms)`;
287+
this.#cancelCurrentMeasurement(errorMessage);
280288
this.#retries = 0;
281289
this.#setRunning(false);
282-
this.#onConnectionError(
283-
`${isDown ? 'Download' : 'Upload'} measurement of ${numBytes} bytes aborted. Measurement exceeded bandwidthAbortRequestDuration (${this.abortRequestDuration}ms)`
284-
);
290+
this.#onConnectionError(errorMessage);
285291
}, this.abortRequestDuration);
286292
this.#currentAbortController.signal.addEventListener('abort', () =>
287293
clearTimeout(abortTimeout)
@@ -392,8 +398,10 @@ class BandwidthMeasurementEngine {
392398
});
393399
}
394400

395-
#cancelCurrentMeasurement() {
396-
this.#currentAbortController.abort();
401+
#cancelCurrentMeasurement(reason) {
402+
this.#currentAbortController?.abort(
403+
reason || `aborted with no reason provided`
404+
);
397405
}
398406
}
399407

0 commit comments

Comments
 (0)