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

build: test cleanup wrt wg.Done #5746

Open
chai2010 opened this issue Jun 21, 2013 · 10 comments

Comments

@chai2010
Copy link
Contributor

commented Jun 21, 2013

just like this test example:

func TestFailed(t *testing.T) {
    var wg sync.WaitGroup
    for i := 0; i < 2; i++ {
        wg.Add(1)
        go func(id int) {
            // defer wg.Done()
            t.Fatalf("TestFailed: id = %v\n", id)
            wg.Done()
        }(i)
    }
    wg.Wait()
}

go test
fatal error: all goroutines are asleep - deadlock!

---

When the t.Fatalf panic, goroutine can't reach wg.Done().
So wg.Wait() deadlock!

There are lots of similar iusses, eg:
http://golang.org/src/pkg/net/rpc/server_test.go?s=12968:13320#L548

If the issue verified, i can submit patch.
@davecheney

This comment has been minimized.

Copy link
Contributor

commented Jun 21, 2013

Comment 1:

That benchmark code is broken, Fatalf should not be called from a goroutine, only the
testing goroutine.
If you want to propose a fix for this, please follow the contribution guidelines here,
http://golang.org/doc/contribute.html.

Labels changed: added priority-later, removed priority-triage.

Status changed to HelpWanted.

@chai2010

This comment has been minimized.

Copy link
Contributor Author

commented Jun 21, 2013

Comment 2:

if the Fatalf can't be called from a goroutine,
how to report the other goroutine Fatal infomation?
I look over the `testing.T` and `testing.T` source code,
the `{t|b}.Fatalf` is thread safe.
can we use `defer wg.Done()` to fix these issue(net/rpc/server_test.go [1])?
for p := 0; p < procs; p++ {
    go func() {
        defer wg.Done()
        reply := new(Reply)
        for atomic.AddInt32(&N, -1) >= 0 {
            err := client.Call("Arith.Add", args, reply)
            if err != nil {
                b.Fatalf("rpc error: Add: expected no error but got string %q", err.Error())
            }
            if reply.C != args.A+args.B {
                b.Fatalf("rpc error: Add: expected %d got %d", reply.C, args.A+args.B)
            }
        }
    }()
}
---
[1]
http://golang.org/src/pkg/net/rpc/server_test.go?s=12968:13320#L548
@davecheney

This comment has been minimized.

Copy link
Contributor

commented Jun 21, 2013

Comment 3:

You must use t.Errorf, but your suggestion to use defer wg.Done() is better.
@chai2010

This comment has been minimized.

Copy link
Contributor Author

commented Jun 21, 2013

Comment 4:

If we use t.Errorf, the goroutine will continues execution.
If test failed, i think we need stop the goroutine execution.
t.Errorf is equivalent to t.Logf followed by t.Fail.
t.Fatalf is equivalent to t.Logf followed by t.FailNow.
So, i think t.Fatalf is better than t.Errorf.
@cznic

This comment has been minimized.

Copy link
Contributor

commented Jun 21, 2013

Comment 5:

@4: "If we use t.Errorf, the goroutine will continues execution."
I think one can safely do:
    t.Errorf(whatever, foo)
    return
@chai2010

This comment has been minimized.

Copy link
Contributor Author

commented Jun 21, 2013

Comment 6:

the patch has been sent as https://golang.org/cl/10454043/
@davecheney

This comment has been minimized.

Copy link
Contributor

commented Jun 27, 2013

Comment 7:

Status changed to Started.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Nov 27, 2013

Comment 9:

Labels changed: added go1.3maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2013

Comment 10:

Labels changed: added release-none, removed go1.3maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2013

Comment 11:

Labels changed: added repo-main.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@bradfitz bradfitz removed the Started label Jan 6, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.