Skip to content

fix: Race when writing to a closed pipe #1916

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

Merged
merged 1 commit into from
Jun 1, 2022
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
10 changes: 9 additions & 1 deletion provisionersdk/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Serve(ctx context.Context, server proto.DRPCProvisionerServer, options *Ser
if options.Listener == nil {
config := yamux.DefaultConfig()
config.LogOutput = io.Discard
stdio, err := yamux.Server(readWriteCloser{
stdio, err := yamux.Server(&readWriteCloser{
ReadCloser: os.Stdin,
Writer: os.Stdout,
}, config)
Expand All @@ -54,6 +54,9 @@ func Serve(ctx context.Context, server proto.DRPCProvisionerServer, options *Ser
// short-lived processes that can be executed concurrently.
err = srv.Serve(ctx, options.Listener)
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
if errors.Is(err, context.Canceled) {
return nil
}
Expand All @@ -67,3 +70,8 @@ func Serve(ctx context.Context, server proto.DRPCProvisionerServer, options *Ser
}
return nil
}

type readWriteCloser struct {
io.ReadCloser
io.Writer
}
20 changes: 4 additions & 16 deletions provisionersdk/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provisionersdk
import (
"context"
"io"
"net"

"github.com/hashicorp/yamux"
"storj.io/drpc"
Expand All @@ -17,22 +18,14 @@ const (

// TransportPipe creates an in-memory pipe for dRPC transport.
func TransportPipe() (*yamux.Session, *yamux.Session) {
clientReader, clientWriter := io.Pipe()
serverReader, serverWriter := io.Pipe()
c1, c2 := net.Pipe()
yamuxConfig := yamux.DefaultConfig()
yamuxConfig.LogOutput = io.Discard
client, err := yamux.Client(&readWriteCloser{
ReadCloser: clientReader,
Writer: serverWriter,
}, yamuxConfig)
client, err := yamux.Client(c1, yamuxConfig)
if err != nil {
panic(err)
}

server, err := yamux.Server(&readWriteCloser{
ReadCloser: serverReader,
Writer: clientWriter,
}, yamuxConfig)
server, err := yamux.Server(c2, yamuxConfig)
if err != nil {
panic(err)
}
Expand All @@ -44,11 +37,6 @@ func Conn(session *yamux.Session) drpc.Conn {
return &multiplexedDRPC{session}
}

type readWriteCloser struct {
io.ReadCloser
io.Writer
}

// Allows concurrent requests on a single dRPC connection.
// Required for calling functions concurrently.
type multiplexedDRPC struct {
Expand Down