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: teardownFunc check in SetTeardown incorrect #12

Merged
merged 2 commits into from
Jul 29, 2015
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
2 changes: 0 additions & 2 deletions goprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ type Process interface {
// lifecycle of a Process.
type TeardownFunc func() error

var nilTeardownFunc = func() error { return nil }

// ProcessFunc is a function that takes a process. Its main use case is goprocess.Go,
// which spawns a ProcessFunc in its own goroutine, and returns a corresponding
// Process object.
Expand Down
2 changes: 0 additions & 2 deletions goprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,11 @@ func TestAddChild(t *testing.T) {
testNone(t, Q)

go b.Close()
testNone(t, Q)
d.Close()
testStrs(t, Q, "b", "d")
testStrs(t, Q, "b", "d")

go a.Close()
testNone(t, Q)
c.Close()
testStrs(t, Q, "a", "c")
testStrs(t, Q, "a", "c")
Expand Down
28 changes: 14 additions & 14 deletions impl-mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ type process struct {
// **after** entering <-Closing(), and
// **before** <-Closed().
func newProcess(tf TeardownFunc) *process {
if tf == nil {
tf = nilTeardownFunc
}

return &process{
teardown: tf,
closed: make(chan struct{}),
Expand Down Expand Up @@ -123,17 +119,19 @@ func (p *process) Go(f ProcessFunc) Process {
// SetTeardown to assign a teardown function
func (p *process) SetTeardown(tf TeardownFunc) {
if tf == nil {
tf = nilTeardownFunc
panic("cannot set nil TeardownFunc")
}

p.Lock()
if p.teardown == nil {
select {
case <-p.Closed():
p.teardown = tf
p.closeErr = tf()
default:
}
if p.teardown != nil {
panic("cannot SetTeardown twice")
}

select {
case <-p.Closed():
p.teardown = tf
p.closeErr = tf()
default:
}
p.Unlock()
}
Expand Down Expand Up @@ -196,8 +194,10 @@ func (p *process) doClose() {
}
}

p.closeErr = p.teardown() // actually run the close logic (ok safe to teardown)
close(p.closed) // signal that we're shut down (Closed)
if p.teardown != nil {
p.closeErr = p.teardown() // actually run the close logic (ok safe to teardown)
}
close(p.closed) // signal that we're shut down (Closed)

// go remove all the parents from the process links. optimization.
go func(waiters []*processLink) {
Expand Down