@@ -34,10 +34,8 @@ type Client struct {
3434 readyForNextBlockChan chan bool
3535 onceStart sync.Once
3636 onceStop sync.Once
37+ started bool
3738 syncPipelinedRequestNext int
38-
39- // waitingForCurrentTipChan will process all the requests for the current tip until the channel
40- // is empty.
4139 //
4240 // want* only processes one request per message reply received from the server. If the message
4341 // request fails, it is the responsibility of the caller to clear the channel.
@@ -126,12 +124,8 @@ func (c *Client) Start() {
126124 "protocol" , ProtocolName ,
127125 "connection_id" , c .callbackContext .ConnectionId .String (),
128126 )
127+ c .started = true
129128 c .Protocol .Start ()
130- // Start goroutine to cleanup resources on protocol shutdown
131- go func () {
132- <- c .DoneChan ()
133- close (c .readyForNextBlockChan )
134- }()
135129 })
136130}
137131
@@ -151,6 +145,16 @@ func (c *Client) Stop() error {
151145 if err = c .SendMessage (msg ); err != nil {
152146 return
153147 }
148+ // Defer closing channel until protocol fully shuts down (only if started)
149+ if c .started {
150+ go func () {
151+ <- c .DoneChan ()
152+ close (c .readyForNextBlockChan )
153+ }()
154+ } else {
155+ // If protocol was never started, close channel immediately
156+ close (c .readyForNextBlockChan )
157+ }
154158 })
155159 return err
156160}
@@ -721,14 +725,22 @@ func (c *Client) handleRollForward(msgGeneric protocol.Message) error {
721725 if callbackErr != nil {
722726 if errors .Is (callbackErr , ErrStopSyncProcess ) {
723727 // Signal that we're cancelling the sync
724- c .readyForNextBlockChan <- false
728+ select {
729+ case <- c .DoneChan ():
730+ return protocol .ErrProtocolShuttingDown
731+ case c .readyForNextBlockChan <- false :
732+ }
725733 return nil
726734 } else {
727735 return callbackErr
728736 }
729737 }
730738 // Signal that we're ready for the next block
731- c .readyForNextBlockChan <- true
739+ select {
740+ case <- c .DoneChan ():
741+ return protocol .ErrProtocolShuttingDown
742+ case c .readyForNextBlockChan <- true :
743+ }
732744 return nil
733745}
734746
@@ -752,15 +764,23 @@ func (c *Client) handleRollBackward(msgGeneric protocol.Message) error {
752764 if callbackErr := c .config .RollBackwardFunc (c .callbackContext , msgRollBackward .Point , msgRollBackward .Tip ); callbackErr != nil {
753765 if errors .Is (callbackErr , ErrStopSyncProcess ) {
754766 // Signal that we're cancelling the sync
755- c .readyForNextBlockChan <- false
767+ select {
768+ case <- c .DoneChan ():
769+ return protocol .ErrProtocolShuttingDown
770+ case c .readyForNextBlockChan <- false :
771+ }
756772 return nil
757773 } else {
758774 return callbackErr
759775 }
760776 }
761777 }
762778 // Signal that we're ready for the next block
763- c .readyForNextBlockChan <- true
779+ select {
780+ case <- c .DoneChan ():
781+ return protocol .ErrProtocolShuttingDown
782+ case c .readyForNextBlockChan <- true :
783+ }
764784 return nil
765785}
766786
0 commit comments