rpcServer: ensure all payIntents are handled before exiting

This commit fixes a problem that could arise when handling a continuous
stream of payIntents. We would risk the client sending a set of payment
intents and closing the stream, but we wouldn't be sure the sendLoop had
read all messages on the `payChan` before exiting with the nil error
from the `errChan`.

Instead, we close the `payChan` to indicate that no more payments are
going to be received.
This commit is contained in:
Johan T. Halseth committed 2020-01-21 12:45:12 +01:00
1 parent c49ba0c5cb
commit 5b85721c04
1 file changed
+10 -2
+10 -2
View File
@@ -3704,7 +3704,7 @@ func (r *rpcServer) sendPayment(stream *paymentStream) error {
// stream, and we can exit normally.
nextPayment, err := stream.recv()
if err == io.EOF {
errChan <- nil
close(payChan)
return
} else if err != nil {
rpcsLog.Errorf("Failed receiving from "+
@@ -3755,6 +3755,7 @@ func (r *rpcServer) sendPayment(stream *paymentStream) error {
}
}()
sendLoop:
for {
select {
@@ -3766,7 +3767,14 @@ func (r *rpcServer) sendPayment(stream *paymentStream) error {
case <-r.quit:
return errors.New("rpc server shutting down")
case payIntent := <-payChan:
case payIntent, ok := <-payChan:
// If the receive loop is done, we break the send loop
// and wait for the ongoing payments to finish before
// exiting.
if !ok {
break sendLoop
}
// We launch a new goroutine to execute the current
// payment so we can continue to serve requests while
// this payment is being dispatched.