Skip to content

tomb kills self after the last goroutine completes #28

@countvonzero

Description

@countvonzero

hi, i would like to understand the rationale of this block of code.

func (t *Tomb) run(f func() error) {
        err := f()
        t.m.Lock()
        defer t.m.Unlock()
        t.alive--
        if t.alive == 0 || err != nil {
                t.kill(err)
                if t.alive == 0 {
                        close(t.dead)
                }
        }
}

i am fitting Tomb into a long running service where goroutines are started and completed. the Tomb will kill itself after the last goroutine finishes while the service is still running and will still launch more goroutines. what is the expected usage of Tomb in this case? what will happen if the code is changed to

func (t *Tomb) run(f func() error) {
        err := f()
        t.m.Lock()
        defer t.m.Unlock()
        t.alive--
        if err != nil {
                t.kill(err)
                if t.alive == 0 {
                        close(t.dead)
                }
                return
        }
        if t.alive == 0 {
                select {
                default:
                        return
                case <-t.dying:
                        close(t.dead)
                }
        }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions