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

go test -race fails #1

Open
predmond opened this issue Feb 26, 2016 · 2 comments
Open

go test -race fails #1

predmond opened this issue Feb 26, 2016 · 2 comments

Comments

@predmond
Copy link
Contributor

go test -race ./... fails with 4 data races detected.

My test which uses disposable-redis found these errors too.

@predmond
Copy link
Contributor Author

Adding a waitgroup and waiting for the goroutine in run to finish fixes the race.

diff --git a/disposable_redis.go b/disposable_redis.go
index e764be9..cab9157 100644
--- a/disposable_redis.go
+++ b/disposable_redis.go
@@ -12,6 +12,7 @@ import (
        "math/rand"
        "os/exec"
        "strings"
+       "sync"
        "time"

        redigo "github.com/garyburd/redigo/redis"
@@ -33,6 +34,7 @@ type Server struct {
        cmd     *exec.Cmd
        port    uint16
        running bool
+       wg      sync.WaitGroup
 }

 // Start and run the process, return an error if it cannot be run
@@ -43,7 +45,9 @@ func (r *Server) run() error {
        ch := make(chan error)

        // we wait for LaunchWaitTimeout and see if the server quit due to an error
+       r.wg.Add(1)
        go func() {
+               defer r.wg.Done()
                err := r.cmd.Wait()
                select {
                case ch <- err:
@@ -147,6 +151,7 @@ func (r *Server) Stop() error {
                return err
        }

+       r.wg.Wait()
        r.cmd.Wait()

        return nil

@dvirsky
Copy link
Member

dvirsky commented Feb 28, 2016

thanks. care to make it a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants