mafredri / cdp

Package cdp provides type-safe bindings for the Chrome DevTools Protocol (CDP), written in the Go programming language.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

session.Manager.watch may not finish when unexpected error happens on websocket

jmuk opened this issue · comments

We've noticed that sometimes session.Manager.watch function does not stop and consumes a lot of CPU cycles. As I checked the Err(), it turns out that it gets websocket.CloseError -- like

Manager.watch: error receiving message event: cdp.Target: ReceivedMessageFromTarget Recv: rpcc: the connection is closing: websocket: close 1006 (abnormal closure): unexpected EOF           
Manager.watch: error receiving detached event: cdp.Target: DetachedFromTarget Recv: rpcc: the connection is closing: websocket: close 1006 (abnormal closure): unexpected EOF                 
Manager.watch: error receiving detached event: cdp.Target: DetachedFromTarget Recv: rpcc: the connection is closing: websocket: close 1006 (abnormal closure): unexpected EOF                 
Manager.watch: error receiving detached event: cdp.Target: DetachedFromTarget Recv: rpcc: the connection is closing: websocket: close 1006 (abnormal closure): unexpected EOF 

Indeed, rpcc.Stream may return websocket errors directly, while isClosing function does not care about them (https://github.com/mafredri/cdp/blob/master/session/manager.go#L72). When this happens, the goroutine enters into a busy loop of Ready() -> Recv() -> format error.

This can be fixed by handling websocket errors in isClosing, or wrapping websocket errors in rpcc.Stream properly.

See also: https://crbug.com/1098076#c6