Skip to content

An open-source, faster Devin alternative

TypeScript

drive redial from cell-CP resolve errors — long migration backoff on 503/migrating, clean 1000 close on 404/410 terminal

Brian Reardonfef558f1dffa
diff --git a/internal/api/ws_broker.go b/internal/api/ws_broker.go
index 390655dc..fc64944c 100644
--- a/internal/api/ws_broker.go
+++ b/internal/api/ws_broker.go
@@ -77,6 +77,9 @@ func (s *Server) brokerWebSocket(c echo.Context) error {
if isMigratingErr(err) {
return "", "", fmt.Errorf("%w: %v", wsgateway.ErrUpstreamMigrating, err)
}
+ if isTerminalErr(err) {
+ return "", "", fmt.Errorf("%w: %v", wsgateway.ErrUpstreamTerminal, err)
+ }
return "", "", err
}
return buildWorkerWSURL(r.WorkerURL, cellPathStripped, rawQuery), r.Token, nil
@@ -90,6 +93,22 @@ func (s *Server) brokerWebSocket(c echo.Context) error {
})
}
+// isTerminalErr matches the echo.HTTPError shapes ResolveWorker returns
+// for sandboxes that are permanently gone — 404 (no DB row, e.g. just
+// DELETEd or never existed) and 410 (status=stopped or error). The
+// broker uses this to close the client with a clean 1000 reason
+// instead of cycling through the redial budget.
+func isTerminalErr(err error) bool {
+ if err == nil {
+ return false
+ }
+ var he *echo.HTTPError
+ if !errors.As(err, &he) {
+ return false
+ }
+ return he.Code == http.StatusNotFound || he.Code == http.StatusGone
+}
+
// isMigratingErr peeks at the echo.HTTPError returned by ResolveWorker
// and reports whether it's the "sandbox is migrating, retry shortly"
// flavor — the only ResolveWorker error path that should drive the
diff --git a/internal/wsgateway/dial.go b/internal/wsgateway/dial.go
index d7db909a..73a5a87e 100644
--- a/internal/wsgateway/dial.go
+++ b/internal/wsgateway/dial.go
@@ -12,12 +12,21 @@ import (
)
// ErrUpstreamMigrating is the sentinel the ResolveWorker closure (in
-// internal/api/ws_broker.go) returns wrapped via errors.Join when the
+// internal/api/ws_broker.go) returns wrapped via fmt.Errorf when the
// cell-CP reports the sandbox is mid-migration. The broker's runRedial
// recognizes it and switches to the longer migration backoff cadence
// instead of burning the fast-ladder budget on a guaranteed-to-fail dial.
var ErrUpstreamMigrating = errors.New("upstream migrating")
+// ErrUpstreamTerminal is the sentinel the ResolveWorker closure returns
+// when the cell-CP reports the sandbox is gone for good — 404 not found
+// or 410 stopped/error. The broker's runRedial closes the client with a
+// clean 1000 + the wrapped reason instead of retrying.
+//
+// The reason string sent to the client is extracted from the wrapped
+// error (see terminalReasonFromErr in session.go).
+var ErrUpstreamTerminal = errors.New("upstream terminal")
+
// DialResult classifies an upstream dial outcome so the redial loop can
// pick the right backoff policy. Either Conn is non-nil (success), or
// one of the Terminal/Migrating flags drives the loop's next action.
diff --git a/internal/wsgateway/session.go b/internal/wsgateway/session.go
index 5ec0b83f..a3bcbacb 100644
--- a/internal/wsgateway/session.go
+++ b/internal/wsgateway/session.go
@@ -230,6 +230,14 @@ func (s *Session) runRedial(clientDone <-chan struct{}) (*websocket.Conn, bool,
url, token, err := s.workerWSURLFn()
if err != nil {
log.Printf("wsgateway: redial: url resolve failed sandbox=%s session=%s: %v", s.sandboxID, s.sessionID, err)
+ // Cell-CP says the sandbox is gone for good (DELETEd, stopped,
+ // errored, never existed). Close the client cleanly with the
+ // reason from the wrapped error instead of burning attempts.
+ if errors.Is(err, ErrUpstreamTerminal) {
+ reason := terminalReasonFromErr(err)
+ log.Printf("wsgateway: redial: cell reports terminal (resolve, reason=%q) — closing client sandbox=%s", reason, s.sandboxID)
+ return nil, true, reason, false
+ }
// Cell-CP signaling "currently migrating" → switch to the
// longer cadence (no point burning fast-ladder attempts at a
// resolve that's deterministically going to fail until the
@@ -377,6 +385,25 @@ func writeOne(c *websocket.Conn, mu sync.Locker, mt int, data []byte) error {
return c.WriteMessage(mt, data)
}
+// terminalReasonFromErr extracts a clean close-reason from an error
+// wrapped with ErrUpstreamTerminal. The wrapped echo.HTTPError carries
+// a message like "sandbox sb-xxx has been stopped" or
+// "sandbox sb-xxx not found" — we want the verb only, not the noisy
+// sandbox ID echoed back to clients. Falls back to a generic reason
+// when the wrapped error doesn't match a known shape.
+func terminalReasonFromErr(err error) string {
+ msg := err.Error()
+ lower := strings.ToLower(msg)
+ switch {
+ case strings.Contains(lower, "stopped"):
+ return "sandbox stopped"
+ case strings.Contains(lower, "not found"):
+ return "sandbox not found"
+ default:
+ return "sandbox unavailable"
+ }
+}
+
// terminalReason maps a terminal-status DialResult into the close
// reason string sent to the client. Mirrors the DO's wording so
// clients that pattern-match on the reason continue to work.