Skip to content

Commit 3195ca5

Browse files
committed
review: clearState() now calls failPendingCommands()
1 parent 63e1ee8 commit 3195ca5

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

server/lib/cdpmonitor/monitor.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff 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.
154155
func (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.
169190
func (m *Monitor) readLoop(ctx context.Context) {
170191
m.lifeMu.Lock()

0 commit comments

Comments
 (0)