Skip to content

Commit 46b24f7

Browse files
committed
fix: Race when writing to a closed pipe
This is such an intermittent race it's difficult to track, but regardless this is an improvement to the code.
1 parent 9b19dc9 commit 46b24f7

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

provisionersdk/serve.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func Serve(ctx context.Context, server proto.DRPCProvisionerServer, options *Ser
3131
if options.Listener == nil {
3232
config := yamux.DefaultConfig()
3333
config.LogOutput = io.Discard
34-
stdio, err := yamux.Server(readWriteCloser{
34+
stdio, err := yamux.Server(&readWriteCloser{
3535
ReadCloser: os.Stdin,
3636
Writer: os.Stdout,
3737
}, config)
@@ -54,6 +54,9 @@ func Serve(ctx context.Context, server proto.DRPCProvisionerServer, options *Ser
5454
// short-lived processes that can be executed concurrently.
5555
err = srv.Serve(ctx, options.Listener)
5656
if err != nil {
57+
if errors.Is(err, io.EOF) {
58+
return nil
59+
}
5760
if errors.Is(err, context.Canceled) {
5861
return nil
5962
}
@@ -67,3 +70,8 @@ func Serve(ctx context.Context, server proto.DRPCProvisionerServer, options *Ser
6770
}
6871
return nil
6972
}
73+
74+
type readWriteCloser struct {
75+
io.ReadCloser
76+
io.Writer
77+
}

provisionersdk/transport.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provisionersdk
33
import (
44
"context"
55
"io"
6+
"net"
67

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

1819
// TransportPipe creates an in-memory pipe for dRPC transport.
1920
func TransportPipe() (*yamux.Session, *yamux.Session) {
20-
clientReader, clientWriter := io.Pipe()
21-
serverReader, serverWriter := io.Pipe()
21+
c1, c2 := net.Pipe()
2222
yamuxConfig := yamux.DefaultConfig()
2323
yamuxConfig.LogOutput = io.Discard
24-
client, err := yamux.Client(&readWriteCloser{
25-
ReadCloser: clientReader,
26-
Writer: serverWriter,
27-
}, yamuxConfig)
24+
client, err := yamux.Client(c1, yamuxConfig)
2825
if err != nil {
2926
panic(err)
3027
}
31-
32-
server, err := yamux.Server(&readWriteCloser{
33-
ReadCloser: serverReader,
34-
Writer: clientWriter,
35-
}, yamuxConfig)
28+
server, err := yamux.Server(c2, yamuxConfig)
3629
if err != nil {
3730
panic(err)
3831
}
@@ -44,11 +37,6 @@ func Conn(session *yamux.Session) drpc.Conn {
4437
return &multiplexedDRPC{session}
4538
}
4639

47-
type readWriteCloser struct {
48-
io.ReadCloser
49-
io.Writer
50-
}
51-
5240
// Allows concurrent requests on a single dRPC connection.
5341
// Required for calling functions concurrently.
5442
type multiplexedDRPC struct {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy