mirror of https://github.com/docker/cli.git
Merge pull request #29456 from cpuguy83/fix_client_events_race
Fixes a race condition in client events monitoring
This commit is contained in:
commit
368e309073
|
@ -22,17 +22,20 @@ func (cli *Client) Events(ctx context.Context, options types.EventsOptions) (<-c
|
||||||
messages := make(chan events.Message)
|
messages := make(chan events.Message)
|
||||||
errs := make(chan error, 1)
|
errs := make(chan error, 1)
|
||||||
|
|
||||||
|
started := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
defer close(errs)
|
defer close(errs)
|
||||||
|
|
||||||
query, err := buildEventsQueryParams(cli.version, options)
|
query, err := buildEventsQueryParams(cli.version, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
close(started)
|
||||||
errs <- err
|
errs <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := cli.get(ctx, "/events", query, nil)
|
resp, err := cli.get(ctx, "/events", query, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
close(started)
|
||||||
errs <- err
|
errs <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -40,6 +43,7 @@ func (cli *Client) Events(ctx context.Context, options types.EventsOptions) (<-c
|
||||||
|
|
||||||
decoder := json.NewDecoder(resp.body)
|
decoder := json.NewDecoder(resp.body)
|
||||||
|
|
||||||
|
close(started)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
@ -61,6 +65,7 @@ func (cli *Client) Events(ctx context.Context, options types.EventsOptions) (<-c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
<-started
|
||||||
|
|
||||||
return messages, errs
|
return messages, errs
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue