Skip to content

Commit

Permalink
Fix issue with the receive loop trying to process once the connection…
Browse files Browse the repository at this point in the history
… is stopped.
  • Loading branch information
winsock committed Feb 16, 2021
1 parent 18178c4 commit f4c7402
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ func (c *Conn) SendCommand(ctx context.Context, command command.Command) (*RawRe
}

func (c *Conn) ExitAndClose() {
// Attempt a graceful closing of the connection with FreeSWITCH
ctx, cancel := context.WithTimeout(c.runningContext, time.Second)
_, _ = c.SendCommand(ctx, command.Exit{})
cancel()
c.Close()
c.closeOnce.Do(func() {
// Attempt a graceful closing of the connection with FreeSWITCH
ctx, cancel := context.WithTimeout(c.runningContext, time.Second)
_, _ = c.SendCommand(ctx, command.Exit{})
cancel()
c.close()
})
}

func (c *Conn) Close() {
Expand Down Expand Up @@ -232,7 +234,7 @@ func (c *Conn) eventLoop() {
}

func (c *Conn) receiveLoop() {
for {
for c.runningContext.Err() == nil {
response, err := c.readResponse()
if err != nil {
break
Expand All @@ -245,8 +247,8 @@ func (c *Conn) receiveLoop() {
break
}
c.responseChanMutex.RUnlock()
if ok {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if ok && c.runningContext.Err() == nil {
ctx, cancel := context.WithTimeout(c.runningContext, 5*time.Second)
select {
case responseChan <- response:
case <-c.runningContext.Done():
Expand Down

0 comments on commit f4c7402

Please sign in to comment.