Skip to content
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: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func client(addr string, args []string) int {
}
defer conn.Close()

enc, dec := gob.NewEncoder(conn), gob.NewDecoder(conn)
enc, dec := newMsgEncoder(conn), gob.NewDecoder(conn)

cmd := exec.Command(args[0], args[1:]...)

Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func server(args []string) int {
}
defer conn.Close()

enc, dec := gob.NewEncoder(conn), gob.NewDecoder(conn)
enc, dec := newMsgEncoder(conn), gob.NewDecoder(conn)

err = enc.Encode(os.Environ())
if err != nil {
Expand Down
21 changes: 20 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"encoding/gob"
"io"
"sync"
"syscall"
)

Expand All @@ -16,7 +17,25 @@ type msg struct {
Data []byte
}

func msgWrite(enc *gob.Encoder, typ string) io.WriteCloser {
// msgEncoder is a gob encoder that is safe for concurrent use, since the
// stdout/stderr writers and the main goroutine all encode to a single
// connection.
type msgEncoder struct {
mu sync.Mutex
enc *gob.Encoder
}

func newMsgEncoder(w io.Writer) *msgEncoder {
return &msgEncoder{enc: gob.NewEncoder(w)}
}

func (e *msgEncoder) Encode(v interface{}) error {
e.mu.Lock()
defer e.mu.Unlock()
return e.enc.Encode(v)
}

func msgWrite(enc *msgEncoder, typ string) io.WriteCloser {
r, w := io.Pipe()
go func() {
defer r.Close()
Expand Down
2 changes: 1 addition & 1 deletion util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestMsgWrite(t *testing.T) {
tests := []string{"foo", "bar", "baz"}

pr, pw := io.Pipe()
wc := msgWrite(gob.NewEncoder(pw), "test")
wc := msgWrite(newMsgEncoder(pw), "test")

go func() {
for _, test := range tests {
Expand Down
Loading