feat(browser): add timeout, idle auto-close, and max-pages safety mechanisms

Prevent resource leaks and hanging actions in the browser tool:
- Per-action context timeout (default 30s, configurable via timeoutMs param or config)
- Idle page reaper goroutine closes pages unused for 10min (configurable)
- Max pages per tenant (default 5) with LRU eviction
- RefStore cleanup on page close/evict/reap to prevent memory leaks
This commit is contained in:
viettranx committed 2026-03-25 13:55:33 +07:00
1 parent afbecd3e4c
commit ee4d34ae28
8 files changed
+226 -12

No files matched your search

+13
View File
@@ -6,6 +6,7 @@ import (
"log/slog"
"os"
"path/filepath"
"time"
"github.com/google/uuid"
@@ -96,6 +97,18 @@ func setupToolRegistry(
opts = append(opts, browser.WithHeadless(cfg.Tools.Browser.Headless))
slog.Info("browser tool enabled", "headless", cfg.Tools.Browser.Headless)
}
if cfg.Tools.Browser.ActionTimeoutMs > 0 {
opts = append(opts, browser.WithActionTimeout(time.Duration(cfg.Tools.Browser.ActionTimeoutMs)*time.Millisecond))
}
if cfg.Tools.Browser.IdleTimeoutMs > 0 {
opts = append(opts, browser.WithIdleTimeout(time.Duration(cfg.Tools.Browser.IdleTimeoutMs)*time.Millisecond))
} else if cfg.Tools.Browser.IdleTimeoutMs < 0 {
// Explicitly disable idle reaper with negative value
opts = append(opts, browser.WithIdleTimeout(0))
}
if cfg.Tools.Browser.MaxPages > 0 {
opts = append(opts, browser.WithMaxPages(cfg.Tools.Browser.MaxPages))
}
browserMgr = browser.New(opts...)
toolsReg.Register(browser.NewBrowserTool(browserMgr))
}