-
Notifications
You must be signed in to change notification settings - Fork 54
feat: graceful shutdown improvements for browser VMs #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,14 @@ import ( | |
| "encoding/json" | ||
| "fmt" | ||
| "log/slog" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
| "os/exec" | ||
| "os/signal" | ||
| "strings" | ||
| "sync" | ||
| "syscall" | ||
| "time" | ||
|
|
||
|
|
@@ -149,9 +151,14 @@ func main() { | |
| fs.ServeHTTP(w, r) | ||
| }) | ||
|
|
||
| r.Get("/health", healthHandler(upstreamMgr)) | ||
|
|
||
| srv := &http.Server{ | ||
| Addr: fmt.Sprintf(":%d", config.Port), | ||
| Handler: r, | ||
| BaseContext: func(_ net.Listener) context.Context { | ||
| return ctx | ||
| }, | ||
| } | ||
|
|
||
| // wait up to 10 seconds for initial upstream; exit nonzero if not found | ||
|
|
@@ -191,6 +198,9 @@ func main() { | |
| srvDevtools := &http.Server{ | ||
| Addr: fmt.Sprintf("0.0.0.0:%d", config.DevToolsProxyPort), | ||
| Handler: rDevtools, | ||
| BaseContext: func(_ net.Listener) context.Context { | ||
| return ctx | ||
| }, | ||
| } | ||
|
|
||
| // ChromeDriver proxy: intercepts POST /session to inject the DevTools proxy | ||
|
|
@@ -216,6 +226,9 @@ func main() { | |
| srvChromeDriver := &http.Server{ | ||
| Addr: fmt.Sprintf("0.0.0.0:%d", config.ChromeDriverProxyPort), | ||
| Handler: rChromeDriver, | ||
| BaseContext: func(_ net.Listener) context.Context { | ||
| return ctx | ||
| }, | ||
| } | ||
|
|
||
| go func() { | ||
|
|
@@ -269,6 +282,74 @@ func main() { | |
| } | ||
| } | ||
|
|
||
| func healthHandler(mgr *devtoolsproxy.UpstreamManager) http.HandlerFunc { | ||
| return func(w http.ResponseWriter, r *http.Request) { | ||
| cdpStatus := "ok" | ||
| if mgr.Current() == "" { | ||
| cdpStatus = "not_ready" | ||
| } | ||
|
|
||
| chromeStatus := "ok" | ||
| chromeProbeCtx, cancel := context.WithTimeout(r.Context(), 2*time.Second) | ||
| defer cancel() | ||
| req, _ := http.NewRequestWithContext(chromeProbeCtx, http.MethodGet, "http://127.0.0.1:9223/json/version", nil) | ||
| resp, err := http.DefaultClient.Do(req) | ||
| if err != nil || resp.StatusCode != http.StatusOK { | ||
| chromeStatus = "error" | ||
| } | ||
| if resp != nil { | ||
| resp.Body.Close() | ||
| } | ||
|
|
||
| status := http.StatusOK | ||
| if cdpStatus != "ok" || chromeStatus != "ok" { | ||
| status = http.StatusServiceUnavailable | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(status) | ||
| json.NewEncoder(w).Encode(map[string]string{ | ||
| "cdp_status": cdpStatus, | ||
| "chrome_status": chromeStatus, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // wsTracker tracks active WebSocket connections so they can be | ||
| // closed with a proper close frame during shutdown. | ||
| type wsTracker struct { | ||
| mu sync.Mutex | ||
| conns map[*wsTrackerEntry]struct{} | ||
| } | ||
|
|
||
| type wsTrackerEntry struct { | ||
| closeFunc func() | ||
| } | ||
|
|
||
| func newWSTracker() *wsTracker { | ||
| return &wsTracker{conns: make(map[*wsTrackerEntry]struct{})} | ||
| } | ||
|
|
||
| func (t *wsTracker) Add(closeFunc func()) func() { | ||
| entry := &wsTrackerEntry{closeFunc: closeFunc} | ||
| t.mu.Lock() | ||
| t.conns[entry] = struct{}{} | ||
| t.mu.Unlock() | ||
| return func() { | ||
| t.mu.Lock() | ||
| delete(t.conns, entry) | ||
| t.mu.Unlock() | ||
| } | ||
| } | ||
|
|
||
| func (t *wsTracker) CloseAll() { | ||
| t.mu.Lock() | ||
| defer t.mu.Unlock() | ||
| for entry := range t.conns { | ||
| entry.closeFunc() | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CloseAll holds lock while invoking arbitrary callbacksMedium Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit 35ee1e8. Configure here. |
||
|
|
||
| func mustFFmpeg() { | ||
| cmd := exec.Command("ffmpeg", "-version") | ||
| if err := cmd.Run(); err != nil { | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wsTracker entirely unused dead code
Low Severity
The
wsTrackertype,wsTrackerEntry,newWSTracker,Add, andCloseAllare defined but never instantiated or called anywhere in the codebase. The PR description mentions this is "for future explicit close-on-shutdown," but shipping dead code adds maintenance burden without providing value. This scaffolding can be added when the feature is actually wired up.Reviewed by Cursor Bugbot for commit 35ee1e8. Configure here.