Skip to content

Commit

Permalink
cmd/thanos/receive: fix close chan panic (#1595)
Browse files Browse the repository at this point in the history
* cmd/thanos/receive: fix close chan panic

This commit fixes a panic caused by sending on the chan which is closed
too early. This chan was closed when the setup func returned, rather
than when the group's goroutine returned.

Fixes: #1594
Signed-off-by: Lucas Servén Marín <[email protected]>

* cmd/thanos/receive: correctly clean up uploader

This commit fixes how the uploader is cleaned up. Currently, it was
cleaned up in a defer block that was run before the run groups. This
ensures the cleanup occurs only when the run groups are returning.

Signed-off-by: Lucas Servén Marín <[email protected]>
  • Loading branch information
squat authored and brancz committed Oct 2, 2019
1 parent f6b159b commit 4c7ecd3
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions cmd/thanos/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ func runReceive(
cancel()
})
} else {
defer close(updates)
cancel := make(chan struct{})
g.Add(func() error {
defer close(updates)
updates <- receive.SingleNodeHashring(endpoint)
<-cancel
return nil
Expand Down Expand Up @@ -370,25 +370,12 @@ func runReceive(
return err
}

// Ensure we close up everything properly.
defer func() {
if err != nil {
runutil.CloseWithLogOnErr(logger, bkt, "bucket client")
}
}()

s := shipper.New(logger, reg, dataDir, bkt, func() labels.Labels { return lset }, metadata.ReceiveSource)

// Before starting, ensure any old blocks are uploaded.
if uploaded, err := s.Sync(context.Background()); err != nil {
level.Warn(logger).Log("err", err, "failed to upload", uploaded)
}
// Before quitting, ensure all blocks are uploaded.
defer func() {
if uploaded, err := s.Sync(context.Background()); err != nil {
level.Warn(logger).Log("err", err, "failed to upload", uploaded)
}
}()

{
// Run the uploader in a loop.
Expand All @@ -410,7 +397,18 @@ func runReceive(
// Upload on demand.
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
defer close(uploadC)
// Ensure we clean up everything properly.
defer func() {
runutil.CloseWithLogOnErr(logger, bkt, "bucket client")
}()
// Before quitting, ensure all blocks are uploaded.
defer func() {
<-uploadC
if uploaded, err := s.Sync(context.Background()); err != nil {
level.Warn(logger).Log("err", err, "failed to upload", uploaded)
}
}()
defer close(uploadDone)
for {
select {
case <-ctx.Done():
Expand All @@ -419,7 +417,6 @@ func runReceive(
if uploaded, err := s.Sync(ctx); err != nil {
level.Warn(logger).Log("err", err, "failed to upload", uploaded)
}
cancel()
uploadDone <- struct{}{}
}
}
Expand Down

0 comments on commit 4c7ecd3

Please sign in to comment.