Skip to content

Commit 121d089

Browse files
author
cyk
committed
fix(stream): 修复链接过期检测逻辑,避免将上下文取消视为链接过期
1 parent e7af7e7 commit 121d089

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

internal/stream/util.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ func IsLinkExpiredError(err error) bool {
4343
if err == nil {
4444
return false
4545
}
46+
47+
// Don't treat context cancellation as link expiration
48+
// This happens when user pauses/seeks video or cancels download
49+
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
50+
return false
51+
}
52+
4653
errStr := strings.ToLower(err.Error())
4754

4855
// Common expired link error keywords
@@ -62,8 +69,8 @@ func IsLinkExpiredError(err error) bool {
6269
if statusErr, ok := errs.UnwrapOrSelf(err).(net.HttpStatusCodeError); ok {
6370
code := int(statusErr)
6471
// 401 Unauthorized, 403 Forbidden, 410 Gone are common for expired links
65-
// 500 Internal Server Error - some providers (e.g., Baidu) return 500 when link expires
66-
if code == 401 || code == 403 || code == 410 || code == 500 {
72+
// Note: Removed 500 to avoid false positives from temporary network errors
73+
if code == 401 || code == 403 || code == 410 {
6774
return true
6875
}
6976
}
@@ -158,7 +165,9 @@ func (r *RefreshableRangeReader) doRefreshLocked(ctx context.Context) error {
158165
}
159166

160167
log.Infof("Link expired, attempting to refresh...")
161-
newLink, _, refreshErr := r.link.Refresher(ctx)
168+
// Use independent context for refresh to prevent cancellation from affecting link refresh
169+
refreshCtx := context.WithoutCancel(ctx)
170+
newLink, _, refreshErr := r.link.Refresher(refreshCtx)
162171
if refreshErr != nil {
163172
return fmt.Errorf("failed to refresh link: %w", refreshErr)
164173
}

0 commit comments

Comments
 (0)