Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix(client): panic: send on closed channel #179

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,20 @@ type Client struct {
// Ya like logs?
Log Logger

Disconnected chan bool
Disconnected chan struct{}

mutex sync.Mutex
mutex sync.Mutex
closeOnce sync.Once
}

func (c *Client) Close() {
c.closeOnce.Do(func() {
c.mutex.Lock()
defer c.mutex.Unlock()

close(c.Disconnected)
close(c.Opcodes)
})
}

// SendRequest abstracts the logic every subclient uses to send a request and
Expand Down
30 changes: 20 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,9 @@ func (c *Client) Disconnect() error {

func (c *Client) markDisconnected() {
c.once.Do(func() {
select {
case c.client.Disconnected <- true:
default:
}
andreykaipov marked this conversation as resolved.
Show resolved Hide resolved

c.client.Log.Printf("[TRACE] Closing internal channels")
c.client.Close()
close(c.IncomingEvents)
close(c.client.Opcodes)
close(c.client.IncomingResponses)
close(c.client.Disconnected)
})
}

Expand All @@ -158,7 +151,7 @@ func New(host string, opts ...Option) (*Client, error) {
requestHeader: http.Header{"User-Agent": []string{"goobs/" + LibraryVersion}},
eventSubscriptions: subscriptions.All,
client: &api.Client{
Disconnected: make(chan bool),
Disconnected: make(chan struct{}),
IncomingResponses: make(chan *opcodes.RequestResponse),
Opcodes: make(chan opcodes.Opcode),
ResponseTimeout: 10000,
Expand Down Expand Up @@ -210,7 +203,24 @@ func (c *Client) connect() (err error) {
authComplete := make(chan error)

go c.handleRawServerMessages(authComplete)
go c.handleOpcodes(authComplete)
go func() {
c.handleOpcodes(authComplete)

// we write to IncomingResponses only from one place:
// * c.handleOpcodes
// and we also read from it in:
// * c.client.SendRequest
// thus the `close` must happen only after c.handleOpcodes finished,
// and is desired to happen after c.client.SendRequest will stop
// using the channel as well (to avoid handling nil Responses).
//
// This line right here:
// * it is after c.handleOpcodes.
// * this place is reachable only after c.client.Opcodes is closed,
// which is possible only when c.client.Disconnected is closed,
// which means c.client.SendRequest would not write into c.client.IncomingResponses.
andreykaipov marked this conversation as resolved.
Show resolved Hide resolved
close(c.client.IncomingResponses)
}()

timer := time.NewTimer(c.client.ResponseTimeout * time.Millisecond)
defer timer.Stop()
Expand Down
Loading