fix crash

This commit is contained in:
Josh at WLTechBlog
2025-12-12 07:57:09 -07:00
parent 87e5e0555d
commit 051b912122
3 changed files with 112 additions and 25 deletions

View File

@@ -2371,6 +2371,27 @@ func (d *Daemon) findPageByID(tabID string) (*rod.Page, error) {
// Find the page with the matching ID
for _, p := range pages {
if string(p.TargetID) == tabID {
// IMPORTANT: Pages retrieved from browser.Pages() may not be fully initialized.
// The rod library has a known issue where internal maps (like the helper map) are nil
// when pages are retrieved this way, causing "assignment to entry in nil map" panics.
// See: https://github.com/go-rod/rod/issues/331
//
// To work around this, we need to ensure the page is properly initialized.
// We use a recover block to catch any panics during initialization attempts.
defer func() {
if r := recover(); r != nil {
d.debugLog("Recovered from panic while initializing page %s: %v", tabID, r)
}
}()
// Try to initialize the page by calling Info() which sets up internal state
_, err := p.Info()
if err != nil {
d.debugLog("Warning: found page %s but failed to initialize it: %v", tabID, err)
// Return error instead of continuing, as the page is likely not usable
return nil, fmt.Errorf("found page %s but failed to initialize: %w", tabID, err)
}
return p, nil
}
}
@@ -2405,16 +2426,22 @@ func (d *Daemon) getTab(tabID string) (*rod.Page, error) {
}
// If not in memory, try to find it
// Note: This can happen if the daemon was restarted but the browser is still running,
// or if a tab was created outside of the daemon. Pages retrieved this way may have
// initialization issues (see findPageByID for details).
page, err = d.findPageByID(actualTabID)
if err != nil {
return nil, err
}
// If found, cache it for future use
// If found, cache it for future use and set up console logging
if page != nil {
d.tabs[actualTabID] = page
// Set up console logging for this tab since it wasn't created through openTab
d.setupConsoleLogging(actualTabID, page)
// Update tab history and current tab
d.updateTabHistory(actualTabID)
d.debugLog("Found and cached existing tab %s from browser", actualTabID)
return page, nil
}