Skip to content

Commit

Permalink
Clear gRPC Monitor request only after monitor successfully closes (#1771
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cmaglie authored Jun 17, 2022
1 parent 813cfe7 commit 2ee2138
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions commands/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,9 @@ func (s *ArduinoCoreServerImpl) Monitor(stream rpc.ArduinoCoreService_MonitorSer
// Send a message with Success set to true to notify the caller of the port being now active
_ = stream.Send(&rpc.MonitorResponse{Success: true})

cancelCtx, cancel := context.WithCancel(stream.Context())
go func() {
// close port on gRPC call EOF or errors
defer portProxy.Close()

defer cancel()
for {
msg, err := stream.Recv()
if errors.Is(err, io.EOF) {
Expand Down Expand Up @@ -547,19 +546,25 @@ func (s *ArduinoCoreServerImpl) Monitor(stream rpc.ArduinoCoreService_MonitorSer
}
}()

buff := make([]byte, 4096)
for {
n, err := portProxy.Read(buff)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
stream.Send(&rpc.MonitorResponse{Error: err.Error()})
break
}
if err := stream.Send(&rpc.MonitorResponse{RxData: buff[:n]}); err != nil {
break
go func() {
defer cancel()
buff := make([]byte, 4096)
for {
n, err := portProxy.Read(buff)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
stream.Send(&rpc.MonitorResponse{Error: err.Error()})
break
}
if err := stream.Send(&rpc.MonitorResponse{RxData: buff[:n]}); err != nil {
break
}
}
}
}()

<-cancelCtx.Done()
portProxy.Close()
return nil
}

0 comments on commit 2ee2138

Please sign in to comment.