From 6881602792ccd5312ea840380ec8712f429febbe Mon Sep 17 00:00:00 2001 From: ankur22 Date: Fri, 24 May 2024 10:19:21 +0100 Subject: [PATCH] Add fix to prevent iframe panic on navigation The change prevents the iframe child frames from being removed when a detach event comes in from Chrome. An iframe seems to be associated to multiple sessions. The initial sessions that is linked to the mainframe that the iframe belongs to and its own session. When the page navigates, a detach event arrives with the initial session and a swap type for the iframe. The iframe though is associated to its own session. In this scenario we want to avoid removing frames, which prevents panics occurring when the iframe navigates. --- common/frame_manager.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/common/frame_manager.go b/common/frame_manager.go index 67bc43a45..6638aa106 100644 --- a/common/frame_manager.go +++ b/common/frame_manager.go @@ -173,6 +173,24 @@ func (m *FrameManager) frameDetached(frameID cdp.FrameID, reason cdppage.FrameDe return } + // This helps prevent an iframe and its child frames from being removed + // when the type of detach is a swap. After this detach event usually + // the iframe navigates, which requires the frames to be present for the + // navigate to work. + fs := m.page.getFrameSession(frameID) + if fs != nil { + m.logger.Debugf("FrameManager:frameDetached:sessionFound", + "fmid:%d fid:%v fsID1:%v fsID2:%v found session for frame", + m.ID(), frameID, fs.session.ID(), m.session.ID()) + + if fs.session.ID() != m.session.ID() { + m.logger.Debugf("FrameManager:frameDetached:notSameSession:return", + "fmid:%d fid:%v event session and frame session do not match", + m.ID(), frameID) + return + } + } + if reason == cdppage.FrameDetachedReasonSwap { // When a local frame is swapped out for a remote // frame, we want to keep the current frame which is