fix(chrome): propagate caller ctx cancellation to CDP calls

InstallBundle, EvaluateExtractors, NextActionFromV8 ignored the caller
ctx and ran chromedp.Run on d.tabCtx alone, so step deadlines and
Ctrl-C couldn't interrupt an in-flight CDP round-trip on a hung tab.

Add a runCtx helper that derives a chromedp-bound context which also
cancels when the caller's ctx cancels, and route the three V8 entry
points through it.
This commit is contained in:
pj committed 2026-05-03 11:03:15 +07:00
1 parent d0947d852c
commit 3b204b75dc
2 files changed
+75 -6

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
package chrome
import (
"context"
"testing"
"time"
)
// TestRunCtx_CallerCancelPropagates confirms that cancelling the caller's
// context cancels the chromedp-bound context returned by runCtx. This is the
// channel by which step deadlines and Ctrl-C reach in-flight CDP calls.
func TestRunCtx_CallerCancelPropagates(t *testing.T) {
tabCtx, tabCancel := context.WithCancel(context.Background())
defer tabCancel()
d := &Driver{tabCtx: tabCtx}
callerCtx, callerCancel := context.WithCancel(context.Background())
derived, cancel := d.runCtx(callerCtx)
defer cancel()
callerCancel()
select {
case <-derived.Done():
case <-time.After(time.Second):
t.Fatal("derived ctx did not cancel after caller cancellation")
}
}
// TestRunCtx_TabCancelPropagates confirms the inverse: tearing down the tab
// also cancels any in-flight derived context.
func TestRunCtx_TabCancelPropagates(t *testing.T) {
tabCtx, tabCancel := context.WithCancel(context.Background())
d := &Driver{tabCtx: tabCtx}
derived, cancel := d.runCtx(context.Background())
defer cancel()
tabCancel()
select {
case <-derived.Done():
case <-time.After(time.Second):
t.Fatal("derived ctx did not cancel after tab cancellation")
}
}