From 488df785e046ca844e492b6c85a389649b5f72f4 Mon Sep 17 00:00:00 2001 From: carla Date: Mon, 8 Jun 2020 12:53:07 +0200 Subject: [PATCH] lndclient: close trackpayment channels once final state is reached When the server is finsihed sending updates in trackPayment, we get an EOF error. To allow clients to listen on these channels after the payment has succeeded, we close them to signal that we have finsihed sending updates. --- lndclient/router_client.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lndclient/router_client.go b/lndclient/router_client.go index bd93546..32ea08b 100644 --- a/lndclient/router_client.go +++ b/lndclient/router_client.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "encoding/hex" "fmt" + "io" "time" "github.com/btcsuite/btcutil" @@ -225,6 +226,8 @@ func (r *routerClient) TrackPayment(ctx context.Context, // trackPayment takes an update stream from either a SendPayment or a // TrackPayment rpc call and converts it into distinct update and error streams. +// Once the payment reaches a final state, the status and error channels will +// be closed to signal that we are finished sending into them. func (r *routerClient) trackPayment(ctx context.Context, stream routerrpc.Router_TrackPaymentV2Client) (chan PaymentStatus, chan error, error) { @@ -235,6 +238,17 @@ func (r *routerClient) trackPayment(ctx context.Context, for { payment, err := stream.Recv() if err != nil { + // If we get an EOF error, the payment has + // reached a final state and the server is + // finished sending us updates. We close both + // channels to signal that we are done sending + // values on them and return. + if err == io.EOF { + close(statusChan) + close(errorChan) + return + } + switch status.Convert(err).Code() { // NotFound is only expected as a response to