~axwalk/juju-core/api-uniter-machine

« back to all changes in this revision

Viewing changes to worker/simpleworker.go

[r=axwalk],[bug=1308852] worker: fix simpleworker Kill

gccgo does not like it when you
close a channel twice. It's not
a very nice thing anyway.

Fixes lp:1308852

https://codereview.appspot.com/98480046/

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
// by the doWork function will be returned by the worker's Wait function.
19
19
func NewSimpleWorker(doWork func(stopCh <-chan struct{}) error) Worker {
20
20
        w := &simpleWorker{
21
 
                stopc: make(chan struct{}),
 
21
                stopc: make(chan struct{}, 1),
22
22
                done:  make(chan struct{}),
23
23
        }
24
24
        go func() {
 
25
                defer close(w.done)
25
26
                w.err = doWork(w.stopc)
26
 
                close(w.done)
27
27
        }()
28
28
        return w
29
29
}
31
31
// Kill implements Worker.Kill() and will close the channel given to the doWork
32
32
// function.
33
33
func (w *simpleWorker) Kill() {
34
 
        defer func() {
35
 
                // Allow any number of calls to Kill - the second and subsequent calls
36
 
                // will panic, but we don't care.
37
 
                recover()
38
 
        }()
39
 
        close(w.stopc)
 
34
        select {
 
35
        case w.stopc <- struct{}{}:
 
36
        default: // stopc is already closed
 
37
        }
40
38
}
41
39
 
42
40
// Wait implements Worker.Wait(), and will return the error returned by