File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -151,6 +151,7 @@ func (m *Monitor) Stop() {
151151}
152152
153153// clearState resets sessions, pending requests, and computed state.
154+ // It also fails all in-flight send() calls so their goroutines are unblocked.
154155func (m * Monitor ) clearState () {
155156 m .currentURL .Store ("" )
156157
@@ -162,9 +163,29 @@ func (m *Monitor) clearState() {
162163 m .pendingRequests = make (map [string ]networkReqState )
163164 m .pendReqMu .Unlock ()
164165
166+ m .failPendingCommands ()
167+
165168 m .computed .resetOnNavigation ()
166169}
167170
171+ // failPendingCommands unblocks all in-flight send() calls by delivering an
172+ // error response. This prevents goroutine leaks when the connection is torn
173+ // down during reconnect.
174+ func (m * Monitor ) failPendingCommands () {
175+ m .pendMu .Lock ()
176+ old := m .pending
177+ m .pending = make (map [int64 ]chan cdpMessage )
178+ m .pendMu .Unlock ()
179+
180+ disconnectErr := & cdpError {Code : - 1 , Message : "connection closed" }
181+ for _ , ch := range old {
182+ select {
183+ case ch <- cdpMessage {Error : disconnectErr }:
184+ default :
185+ }
186+ }
187+ }
188+
168189// readLoop reads CDP messages, routing responses to pending callers and dispatching events.
169190func (m * Monitor ) readLoop (ctx context.Context ) {
170191 m .lifeMu .Lock ()
You can’t perform that action at this time.
0 commit comments