Skip to content

Commit 8562315

Browse files
committed
fixup! Add value-based stall detection to catch stuck progress
1 parent 2752c13 commit 8562315

File tree

2 files changed

+6
-33
lines changed

2 files changed

+6
-33
lines changed

lib/StallDetector.ts

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export class StallDetector {
1010
private intervalId: ReturnType<typeof setInterval> | null = null
1111
private lastProgressTime = 0
1212
private lastProgressValue = 0
13-
private progressValueCount = 0
1413
private isActive = false
1514

1615
constructor(
@@ -33,7 +32,6 @@ export class StallDetector {
3332

3433
this.lastProgressTime = Date.now()
3534
this.lastProgressValue = 0
36-
this.progressValueCount = 0
3735
this.isActive = true
3836

3937
log(
@@ -47,10 +45,8 @@ export class StallDetector {
4745
}
4846

4947
const now = Date.now()
50-
if (this._isProgressValueStalled()) {
51-
this._handleStall('progress value not changing')
52-
} else if (this._isProgressStalled(now)) {
53-
this._handleStall('no progress events received')
48+
if (this._isProgressStalled(now)) {
49+
this._handleStall('no progress')
5450
}
5551
}, this.options.checkInterval)
5652
}
@@ -71,14 +67,10 @@ export class StallDetector {
7167
* @param progressValue The current progress value (bytes uploaded)
7268
*/
7369
updateProgress(progressValue: number): void {
74-
this.lastProgressTime = Date.now()
75-
76-
// Track if the progress value has changed
77-
if (progressValue === this.lastProgressValue) {
78-
this.progressValueCount++
79-
} else {
70+
// Only update progress time if the value has actually changed
71+
if (progressValue !== this.lastProgressValue) {
72+
this.lastProgressTime = Date.now()
8073
this.lastProgressValue = progressValue
81-
this.progressValueCount = 0
8274
}
8375
}
8476

@@ -97,25 +89,6 @@ export class StallDetector {
9789
return isStalled
9890
}
9991

100-
/**
101-
* Check if upload has stalled based on progress value not changing
102-
*/
103-
private _isProgressValueStalled(): boolean {
104-
// Calculate how many times we expect progress to have changed based on check intervals
105-
const expectedProgressChanges = Math.floor(
106-
this.options.stallTimeout / this.options.checkInterval,
107-
)
108-
const isStalled = this.progressValueCount >= expectedProgressChanges
109-
110-
if (isStalled) {
111-
log(
112-
`tus: progress value stuck at ${this.lastProgressValue} bytes for ${this.progressValueCount} checks`,
113-
)
114-
}
115-
116-
return isStalled
117-
}
118-
11992
/**
12093
* Handle a detected stall
12194
*/

test/spec/test-stall-detection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ describe('tus-stall-detection', () => {
426426
expect(patchReq.method).toBe('PATCH')
427427

428428
const error = await options.onError.toBeCalled()
429-
expect(error.message).toContain('Upload stalled: progress value not changing')
429+
expect(error.message).toContain('Upload stalled: no progress')
430430
expect(options.onProgress.calls.count()).toBeGreaterThan(0)
431431
})
432432
})

0 commit comments

Comments
 (0)