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

Fix data races #694

Merged
merged 5 commits into from
Nov 4, 2024
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ lint:
./...
@staticcheck ./...
@gosec -quiet -exclude=G110,G204,G304,G404 -exclude-generated ./...
@go vet -stdmethods=false ./...
@go vet -vettool=$(shell go env GOPATH)/bin/checklocks ./...

.PHONY: pre-commit
pre-commit: build wasm test lint
Expand All @@ -120,6 +122,7 @@ install/dev:
go install github.com/icholy/gomajor@v0.13.1
go install github.com/stateful/go-proto-gql/protoc-gen-gql@latest
go install github.com/atombender/go-jsonschema@v0.16.0
go install gvisor.dev/gvisor/tools/checklocks/cmd/checklocks@go

.PHONY: install/goreleaser
install/goreleaser:
Expand Down
10 changes: 7 additions & 3 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Auth struct {
env Env

// loginInProgress is a mutex to prevent concurrent `Login` calls.
// +checkatomic
loginInProgress uint32

// loginSession contains details about the current login session.
Expand Down Expand Up @@ -149,7 +150,6 @@ func (a *Auth) Login(ctx context.Context) error {
if !atomic.CompareAndSwapUint32(&a.loginInProgress, 0, 1) {
return errors.New("login session already in progress")
}
defer atomic.StoreUint32(&a.loginInProgress, 0)

a.log.Debug("start logging in")

Expand All @@ -159,10 +159,14 @@ func (a *Auth) Login(ctx context.Context) error {
a.env = &desktopEnv{Session: a.loginSession}
}

var err error
if a.env.IsAutonomous() {
return a.loginAuto(ctx)
err = a.loginAuto(ctx)
} else {
err = a.loginManual(ctx)
}
return a.loginManual(ctx)
atomic.StoreUint32(&a.loginInProgress, 0)
return err
}

func (a *Auth) loginAuto(ctx context.Context) error {
Expand Down
2 changes: 0 additions & 2 deletions internal/command/command_inline_shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
)

func TestInlineShellCommand_CollectEnv(t *testing.T) {
t.Parallel()

t.Run("Fifo", func(t *testing.T) {
envCollectorUseFifo = true
testInlineShellCommandCollectEnv(t)
Expand Down
28 changes: 9 additions & 19 deletions internal/command/command_terminal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package command

import (
"bufio"
"bytes"
"context"
"io"
"os"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"

"github.com/stateful/runme/v3/internal/sbuffer"
"github.com/stateful/runme/v3/internal/session"
runnerv2 "github.com/stateful/runme/v3/pkg/api/gen/proto/go/runme/runner/v2"
)
Expand All @@ -26,7 +26,7 @@ func TestTerminalCommand_EnvPropagation(t *testing.T) {
session, err := session.New()
require.NoError(t, err)
stdinR, stdinW := io.Pipe()
stdout := bytes.NewBuffer(nil)
stdout := sbuffer.New(nil)

factory := NewFactory(WithLogger(zaptest.NewLogger(t)))

Expand All @@ -52,12 +52,12 @@ func TestTerminalCommand_EnvPropagation(t *testing.T) {

// Terminal command sets up a trap on EXIT.
// Wait for it before starting to send commands.
expectContainLines(ctx, t, stdout, []string{"trap -- \"__cleanup\" EXIT"})
expectContainLines(t, stdout, []string{"trap -- \"__cleanup\" EXIT"})

_, err = stdinW.Write([]byte("export TEST_ENV=1\n"))
require.NoError(t, err)
// Wait for the prompt before sending the next command.
expectContainLines(ctx, t, stdout, []string{"$"})
expectContainLines(t, stdout, []string{"$"})
_, err = stdinW.Write([]byte("exit\n"))
require.NoError(t, err)

Expand All @@ -66,13 +66,11 @@ func TestTerminalCommand_EnvPropagation(t *testing.T) {
}

func TestTerminalCommand_Intro(t *testing.T) {
t.Parallel()

session, err := session.New(session.WithSeedEnv(os.Environ()))
require.NoError(t, err)

stdinR, stdinW := io.Pipe()
stdout := bytes.NewBuffer(nil)
stdout := sbuffer.New(nil)

factory := NewFactory(WithLogger(zaptest.NewLogger(t)))

Expand All @@ -96,19 +94,15 @@ func TestTerminalCommand_Intro(t *testing.T) {

require.NoError(t, cmd.Start(ctx))

expectContainLines(ctx, t, stdout, []string{envSourceCmd, introSecondLine})
expectContainLines(t, stdout, []string{envSourceCmd, introSecondLine})
}

func expectContainLines(ctx context.Context, t *testing.T, r io.Reader, expected []string) {
func expectContainLines(t *testing.T, r io.Reader, expected []string) {
t.Helper()

var output strings.Builder
hits := make(map[string]bool, len(expected))
output := new(strings.Builder)

for {
buf := new(bytes.Buffer)
r := io.TeeReader(r, buf)

scanner := bufio.NewScanner(r)
for scanner.Scan() {
_, _ = output.WriteString(scanner.Text())
Expand All @@ -125,11 +119,7 @@ func expectContainLines(ctx context.Context, t *testing.T, r io.Reader, expected
return
}

select {
case <-time.After(100 * time.Millisecond):
case <-ctx.Done():
t.Fatalf("error waiting for line %q, instead read %q: %s", expected, buf.Bytes(), ctx.Err())
}
time.Sleep(time.Millisecond * 400)
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/command/command_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func TestCommand_SetWinsize(t *testing.T) {

// TODO(adamb): on macOS is is not necessary, but on Linux
// we need to wait for the shell to start before we start sending commands.
time.Sleep(time.Second)
time.Sleep(time.Second * 3)

err = SetWinsize(cmd, &Winsize{Rows: 45, Cols: 56, X: 0, Y: 0})
require.NoError(t, err)
Expand All @@ -393,7 +393,8 @@ func TestCommand_SetWinsize(t *testing.T) {
require.NoError(t, err)
err = cmd.Wait(context.Background())
require.NoError(t, err, "command failed due to: %s", stdout.String())
require.Contains(t, stdout.String(), "56\r\n45\r\n")
require.Contains(t, stdout.String(), "56\r\n")
require.Contains(t, stdout.String(), "45\r\n")
})
}

Expand Down
3 changes: 2 additions & 1 deletion internal/command/command_virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type virtualCommand struct {

wg sync.WaitGroup // watch goroutines copying I/O

mu sync.Mutex // protect err
mu sync.Mutex // protect err
// +checklocks:mu
err error
}

Expand Down
9 changes: 6 additions & 3 deletions internal/runner/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ type command struct {
Stdout io.Writer
Stderr io.Writer

cmd *exec.Cmd
cmd *exec.Cmd
// +checkatomic
cmdDone uint32

// pty and tty as pseud-terminal primary and secondary.
Expand All @@ -53,8 +54,10 @@ type command struct {

context context.Context
wg sync.WaitGroup
mu sync.Mutex
err error

mu sync.Mutex
// +checklocks:mu
err error

logger *zap.Logger
}
Expand Down
3 changes: 2 additions & 1 deletion internal/runnerv2service/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const (
var opininatedEnvVarNamingRegexp = regexp.MustCompile(`^[A-Z_][A-Z0-9_]{1}[A-Z0-9_]*[A-Z][A-Z0-9_]*$`)

type buffer struct {
mu *sync.Mutex
mu *sync.Mutex
// +checklocks:mu
b *bytes.Buffer
closed *atomic.Bool
close chan struct{}
Expand Down
4 changes: 3 additions & 1 deletion internal/runnerv2service/service_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
Expand Down Expand Up @@ -1092,7 +1093,8 @@ func testStartRunnerServiceServer(t *testing.T) (

server := grpc.NewServer()

runnerService, err := NewRunnerService(factory, logger)
// Using nop logger to avoid data race.
runnerService, err := NewRunnerService(factory, zap.NewNop())
require.NoError(t, err)
runnerv2.RegisterRunnerServiceServer(server, runnerService)

Expand Down
43 changes: 43 additions & 0 deletions internal/sbuffer/safe_buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package sbuffer

import (
"bytes"
"sync"
)

type Buffer struct {
// +checklocks:mu
b *bytes.Buffer
mu *sync.RWMutex
}

func New(buf []byte) *Buffer {
return &Buffer{
b: bytes.NewBuffer(buf),
mu: &sync.RWMutex{},
}
}

func (b *Buffer) Bytes() []byte {
b.mu.RLock()
defer b.mu.RUnlock()
return b.b.Bytes()
}

func (b *Buffer) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.b.Write(p)
}

func (b *Buffer) WriteString(s string) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.b.WriteString(s)
}

func (b *Buffer) Read(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.b.Read(p)
}
3 changes: 2 additions & 1 deletion internal/session/env_store_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

type EnvStoreMap struct {
mu sync.RWMutex
mu sync.RWMutex
// +checklocks:mu
items map[string]string
}

Expand Down
Loading