Skip to content

Commit

Permalink
fix: check if writer is nil when closing client rpc
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Stewart <christian@aperture.us>
  • Loading branch information
paralin committed Aug 3, 2024
1 parent 0a75ea3 commit 603c6b1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
12 changes: 9 additions & 3 deletions srpc/client-rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func NewClientRPC(ctx context.Context, service, method string) *ClientRPC {
// Start sets the writer and writes the MsgSend message.
// must only be called once!
func (r *ClientRPC) Start(writer PacketWriter, writeFirstMsg bool, firstMsg []byte) error {
if writer == nil {
return ErrNilWriter
}

if err := r.ctx.Err(); err != nil {
r.ctxCancel()
_ = writer.Close()
Expand Down Expand Up @@ -104,9 +108,11 @@ func (r *ClientRPC) HandleCallStart(pkt *CallStart) error {

// Close releases any resources held by the ClientRPC.
func (r *ClientRPC) Close() {
_ = r.WriteCallCancel()

r.bcast.HoldLock(func(broadcast func(), getWaitCh func() <-chan struct{}) {
r.closeLocked(broadcast)
// call did not start yet if writer is nil.
if r.writer != nil {
_ = r.WriteCallCancel()
r.closeLocked(broadcast)
}
})
}
3 changes: 3 additions & 0 deletions srpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (c *client) ExecCall(ctx context.Context, service, method string, in, out M
if err != nil {
return err
}

if err := clientRPC.Start(writer, true, firstMsg); err != nil {
return err
}
Expand All @@ -63,6 +64,7 @@ func (c *client) ExecCall(ctx context.Context, service, method string, in, out M
if err := out.UnmarshalVT(msg); err != nil {
return errors.Wrap(ErrInvalidMessage, err.Error())
}

return nil
}

Expand All @@ -83,6 +85,7 @@ func (c *client) NewStream(ctx context.Context, service, method string, firstMsg
if err != nil {
return nil, err
}

if err := clientRPC.Start(writer, firstMsg != nil, firstMsgData); err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions srpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ var (
ErrEmptyServiceID = errors.New("service id empty")
// ErrNoAvailableClients is returned if no clients were available.
ErrNoAvailableClients = errors.New("no available rpc clients")
// ErrNilWriter is returned if the rpc writer is nil.
ErrNilWriter = errors.New("writer cannot be nil")
)

0 comments on commit 603c6b1

Please sign in to comment.