Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit dc0152b

Browse files
committed
Avoid onRequestPaused error log when ctx canceled
Continuing/Failing a request from onRequestPaused happens when CDP's network.fetch functionality is enabled and therefore k6 browser acts as a proxy for every request that matches the specified pattern. A context.Canceled error here happens most probably because the iteration has ended, closing the browser's context inherited by NetworkManager, meanwhile the CDP command is being executed. Given this situation, and considering that this happens often in sites that are continuously making background requests, showing these error messages is misleading for the end user. Therefore is probably a better solution to change the log level to debug when this situation occurs.
1 parent 0fcdd36 commit dc0152b

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

common/network_manager.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package common
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net"
78
"net/url"
@@ -429,7 +430,7 @@ func (m *NetworkManager) onRequest(event *network.EventRequestWillBeSent, interc
429430
m.frameManager.requestStarted(req)
430431
}
431432

432-
func (m *NetworkManager) onRequestPaused(event *fetch.EventRequestPaused) {
433+
func (m *NetworkManager) onRequestPaused(event *fetch.EventRequestPaused) { //nolint:funlen
433434
m.logger.Debugf("NetworkManager:onRequestPaused",
434435
"sid:%s url:%v", m.session.ID(), event.Request.URL)
435436
defer m.logger.Debugf("NetworkManager:onRequestPaused:return",
@@ -441,18 +442,31 @@ func (m *NetworkManager) onRequestPaused(event *fetch.EventRequestPaused) {
441442
if failErr != nil {
442443
action := fetch.FailRequest(event.RequestID, network.ErrorReasonBlockedByClient)
443444
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
444-
m.logger.Errorf("NetworkManager:onRequestPaused",
445-
"interrupting request: %s", err)
446-
} else {
447-
m.logger.Warnf("NetworkManager:onRequestPaused",
448-
"request %s %s was interrupted: %s", event.Request.Method, event.Request.URL, failErr)
445+
// Avoid logging as error when context is canceled.
446+
// Most probably this happens when trying to fail a site's background request
447+
// while the iteration is ending and therefore the browser context is being closed.
448+
if errors.Is(err, context.Canceled) {
449+
m.logger.Debug("NetworkManager:onRequestPaused", "context canceled interrupting request")
450+
} else {
451+
m.logger.Errorf("NetworkManager:onRequestPaused", "interrupting request: %s", err)
452+
}
449453
return
450454
}
455+
m.logger.Warnf("NetworkManager:onRequestPaused",
456+
"request %s %s was interrupted: %s", event.Request.Method, event.Request.URL, failErr)
457+
458+
return
451459
}
452460
action := fetch.ContinueRequest(event.RequestID)
453461
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
454-
m.logger.Errorf("NetworkManager:onRequestPaused",
455-
"continuing request: %s", err)
462+
// Avoid logging as error when context is canceled.
463+
// Most probably this happens when trying to continue a site's background request
464+
// while the iteration is ending and therefore the browser context is being closed.
465+
if errors.Is(err, context.Canceled) {
466+
m.logger.Debug("NetworkManager:onRequestPaused", "context canceled continuing request")
467+
return
468+
}
469+
m.logger.Errorf("NetworkManager:onRequestPaused", "continuing request: %s", err)
456470
}
457471
}()
458472

0 commit comments

Comments
 (0)