Skip to content

Commit

Permalink
fix: add timeout for rare case
Browse files Browse the repository at this point in the history
  • Loading branch information
canstand committed Aug 3, 2023
1 parent bef52aa commit 2436665
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
2 changes: 0 additions & 2 deletions browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,7 @@ func (b *browserContextImpl) onClose() {
}

func (b *browserContextImpl) onPage(page *pageImpl) {
b.Lock()
b.pages = append(b.pages, page)
b.Unlock()
b.Emit("page", page)
opener, _ := page.Opener()
if opener != nil && !opener.IsClosed() {
Expand Down
42 changes: 37 additions & 5 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ func (c *connection) cleanup() {
func (c *connection) Dispatch(msg *message) {
method := msg.Method
if msg.ID != 0 {
cb, _ := c.callbacks.Load(msg.ID)
if msg.Error != nil {
cb, ok := c.callbacks.LoadAndDelete(msg.ID)
if ok && msg.Error != nil {
cb.(chan callback) <- callback{
Error: parseError(msg.Error.Error),
}
} else {
} else if ok {
cb.(chan callback) <- callback{
Data: c.replaceGuidsWithChannels(msg.Result),
}
Expand Down Expand Up @@ -244,8 +244,28 @@ func (c *connection) sendMessageToServer(guid string, method string, params inte
if c.tracingCount.Load() > 0 && len(stack) > 0 && guid != "localUtils" {
c.LocalUtils().AddStackToTracingNoReply(id, stack)
}
result := <-cb.(chan callback)
c.callbacks.Delete(id)

var result callback
timeout := getTimeoutFromParams(params)
if timeout != nil { // if event has a timeout, wait result for timeout (and extra 500ms)
select {
case result = <-cb.(chan callback):
case <-time.After(time.Duration(*timeout+500) * time.Millisecond):
result = callback{
Error: &Error{
Name: "Timeout",
Message: fmt.Sprintf("Timeout of %vms exceeded, and method %s no result received", *timeout, method),
Stack: fmt.Sprintf("%v", stack),
},
}
}
} else {
result = <-cb.(chan callback)
}
cb, ok = c.callbacks.LoadAndDelete(id)
if ok {
close(cb.(chan callback))
}
if result.Error != nil {
return nil, result.Error
}
Expand Down Expand Up @@ -339,3 +359,15 @@ func fromNullableChannel(v interface{}) interface{} {
}
return fromChannel(v)
}

func getTimeoutFromParams(params interface{}) *float64 {
v, ok := params.(map[string]interface{})
if !ok {
return nil
}
timeout, ok := v["timeout"].(*float64)
if !ok {
return nil
}
return timeout
}

0 comments on commit 2436665

Please sign in to comment.