High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(handlers): screencast and record start render a failed tab lookup through WriteTabContextError, so a failed unfreeze there is a retryable 503 like everywhere else (PIN-369)
Luigi Agostidfae9b63721d
diff --git a/internal/handlers/record_handlers.go b/internal/handlers/record_handlers.goindex ed828714..3df2a50e 100644--- a/internal/handlers/record_handlers.go+++ b/internal/handlers/record_handlers.go@@ -78,7 +78,7 @@ func (h *Handlers) HandleRecordStart(w http.ResponseWriter, r *http.Request) {ctx, resolvedTabID, err := h.tabContext(r, req.TabID)if err != nil {- httpx.Problem(w, http.StatusNotFound, "tab_not_found", "tab not found", false, nil)+ WriteTabContextError(w, err, http.StatusNotFound)return}diff --git a/internal/handlers/screencast.go b/internal/handlers/screencast.goindex 2e4ad126..dd83ba62 100644--- a/internal/handlers/screencast.go+++ b/internal/handlers/screencast.go@@ -31,7 +31,7 @@ func (h *Handlers) HandleScreencast(w http.ResponseWriter, r *http.Request) {ctx, resolvedTabID, err := h.tabContext(r, tabID)if err != nil {- httpx.Problem(w, http.StatusNotFound, "tab_not_found", "tab not found", false, nil)+ WriteTabContextError(w, err, http.StatusNotFound)return}if _, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDomainPolicy); !ok {diff --git a/internal/handlers/screencast_test.go b/internal/handlers/screencast_test.goindex 7d6aa67b..18c5ce6b 100644--- a/internal/handlers/screencast_test.go+++ b/internal/handlers/screencast_test.go@@ -1,7 +1,7 @@package handlersimport (- "encoding/json"+ "errors""net/http""net/http/httptest""testing"@@ -80,7 +80,7 @@ func TestHandleScreencast_Disabled(t *testing.T) {}}-func TestHandleScreencast_TabNotFoundReturnsProblem(t *testing.T) {+func TestHandleScreencast_TabNotFoundUsesTheSharedTabContextError(t *testing.T) {cfg := &config.RuntimeConfig{AllowScreencast: true}h := New(&mockBridge{failTab: true}, cfg, nil, nil, nil)req := httptest.NewRequest("GET", "/screencast?tabId=missing", nil)@@ -88,18 +88,9 @@ func TestHandleScreencast_TabNotFoundReturnsProblem(t *testing.T) {h.HandleScreencast(w, req)- if w.Code != http.StatusNotFound {- t.Fatalf("expected 404, got %d", w.Code)- }- if ct := w.Header().Get("Content-Type"); ct != "application/problem+json" {- t.Fatalf("expected application/problem+json, got %q", ct)- }-- var payload map[string]any- if err := json.Unmarshal(w.Body.Bytes(), &payload); err != nil {- t.Fatalf("decode problem payload: %v", err)- }- if payload["code"] != "tab_not_found" {- t.Fatalf("code = %v, want tab_not_found", payload["code"])+ want := httptest.NewRecorder()+ WriteTabContextError(want, errors.New("tab not found"), http.StatusNotFound)+ if w.Code != http.StatusNotFound || w.Body.String() != want.Body.String() {+ t.Fatalf("got %d %s, want the shared tab-context 404 %s", w.Code, w.Body.String(), want.Body.String())}}diff --git a/internal/handlers/tab_awake_hold_test.go b/internal/handlers/tab_awake_hold_test.goindex 841f7bc1..b894b6a6 100644--- a/internal/handlers/tab_awake_hold_test.go+++ b/internal/handlers/tab_awake_hold_test.go@@ -6,6 +6,7 @@ import ("errors""net/http""net/http/httptest"+ "strings""sync/atomic""testing""time"@@ -32,7 +33,7 @@ func newFreezeIdleFixture(t *testing.T, delay time.Duration) (*bridge.Bridge, *Hif err := chromedp.Run(tabCtx); err != nil {t.Fatalf("start browser: %v", err)}- cfg := &config.RuntimeConfig{TabLifecyclePolicy: "freeze_idle", TabCloseDelay: delay, ActionTimeout: 10 * time.Second, StateDir: t.TempDir()}+ cfg := &config.RuntimeConfig{TabLifecyclePolicy: "freeze_idle", TabCloseDelay: delay, ActionTimeout: 10 * time.Second, AllowScreencast: true, StateDir: t.TempDir()}b := bridge.New(context.Background(), tabCtx, cfg)b.RegisterTab("tabA", tabCtx)return b, New(b, cfg, nil, nil, nil)@@ -63,47 +64,63 @@ func TestATabResolvedForARequestStaysAwakeUntilTheRequestEnds(t *testing.T) {}func TestAFailedUnfreezeIsARetryable503AndKeepsTheSessionsCurrentTab(t *testing.T) {- b, h := newFreezeIdleFixture(t, 30*time.Millisecond)- var failThaw atomic.Bool- failThaw.Store(true)- b.SetLifecycleWriterForTests(func(_ context.Context, frozen bool) error {- if !frozen && failThaw.Swap(false) {- return errors.New("renderer busy")- }- return nil- })- scope := scopedCurrentTab(currentTabScopeSession, "ses_frozen")- h.CurrentTabs.Set(scope, "tabA")- b.ScheduleIdleLifecycle("tabA")- deadline := time.Now().Add(time.Second)- for !b.TabFrozen("tabA") && time.Now().Before(deadline) {- time.Sleep(5 * time.Millisecond)- }- if !b.TabFrozen("tabA") {- t.Fatal("tab never froze")- }- mux := http.NewServeMux()- h.RegisterRoutes(mux, nil)- text := func() *httptest.ResponseRecorder {- w := httptest.NewRecorder()- mux.ServeHTTP(w, session.WithSession(httptest.NewRequest(http.MethodGet, "/text", nil), &session.Session{ID: "ses_frozen"}))- return w- }-- first := text()- var body struct {- Code string `json:"code"`- Retryable bool `json:"retryable"`- }- _ = json.Unmarshal(first.Body.Bytes(), &body)- if first.Code != http.StatusServiceUnavailable || body.Code != "tab_unfreeze_failed" || !body.Retryable {- t.Fatalf("failed unfreeze answered %d %s, want 503 tab_unfreeze_failed retryable", first.Code, first.Body.String())- }- if got, ok := h.CurrentTabs.Get(scope); !ok || got != "tabA" {- t.Fatalf("session current tab = %q %v after a failed unfreeze, want tabA kept", got, ok)- }+ for _, route := range []struct {+ name string+ request func() *http.Request+ retries bool+ }{+ {"text", func() *http.Request { return httptest.NewRequest(http.MethodGet, "/text", nil) }, true},+ {"screencast", func() *http.Request { return httptest.NewRequest(http.MethodGet, "/screencast?tabId=tabA", nil) }, false},+ {"record start", func() *http.Request {+ return httptest.NewRequest(http.MethodPost, "/record/start", strings.NewReader(`{"tabId":"tabA"}`))+ }, false},+ } {+ t.Run(route.name, func(t *testing.T) {+ b, h := newFreezeIdleFixture(t, 30*time.Millisecond)+ var failThaw atomic.Bool+ failThaw.Store(true)+ b.SetLifecycleWriterForTests(func(_ context.Context, frozen bool) error {+ if !frozen && failThaw.Swap(false) {+ return errors.New("renderer busy")+ }+ return nil+ })+ scope := scopedCurrentTab(currentTabScopeSession, "ses_frozen")+ h.CurrentTabs.Set(scope, "tabA")+ b.ScheduleIdleLifecycle("tabA")+ deadline := time.Now().Add(time.Second)+ for !b.TabFrozen("tabA") && time.Now().Before(deadline) {+ time.Sleep(5 * time.Millisecond)+ }+ if !b.TabFrozen("tabA") {+ t.Fatal("tab never froze")+ }+ mux := http.NewServeMux()+ h.RegisterRoutes(mux, nil)+ serve := func() *httptest.ResponseRecorder {+ w := httptest.NewRecorder()+ mux.ServeHTTP(w, session.WithSession(route.request(), &session.Session{ID: "ses_frozen"}))+ return w+ }- if second := text(); second.Code != http.StatusOK {- t.Fatalf("retry after the renderer recovered answered %d %s, want 200", second.Code, second.Body.String())+ first := serve()+ var body struct {+ Code string `json:"code"`+ Retryable bool `json:"retryable"`+ }+ _ = json.Unmarshal(first.Body.Bytes(), &body)+ if first.Code != http.StatusServiceUnavailable || body.Code != "tab_unfreeze_failed" || !body.Retryable {+ t.Fatalf("failed unfreeze answered %d %s, want 503 tab_unfreeze_failed retryable", first.Code, first.Body.String())+ }+ if got, ok := h.CurrentTabs.Get(scope); !ok || got != "tabA" {+ t.Fatalf("session current tab = %q %v after a failed unfreeze, want tabA kept", got, ok)+ }+ if !route.retries {+ return+ }+ if second := serve(); second.Code != http.StatusOK {+ t.Fatalf("retry after the renderer recovered answered %d %s, want 200", second.Code, second.Body.String())+ }+ })}}