@@ -141,7 +141,16 @@ type FSM struct {
141141
142142 blockNtfnChan chan uint32
143143
144+ // quitChan stops after the FSM stops consuming blockNtfnChan.
145+ quitChan chan struct {}
146+
147+ // finalizedDepositChan is used to signal that the deposit has been
148+ // finalized and the FSM can be removed from the manager's memory.
144149 finalizedDepositChan chan wire.OutPoint
150+
151+ // spentChan is used to signal that the FSM should stop processing
152+ // block notifications and exit.
153+ spentChan chan struct {}
145154}
146155
147156// NewFSM creates a new state machine that can action on all static address
@@ -167,7 +176,9 @@ func NewFSM(ctx context.Context, deposit *Deposit, cfg *ManagerConfig,
167176 params : params ,
168177 address : address ,
169178 blockNtfnChan : make (chan uint32 ),
179+ quitChan : make (chan struct {}),
170180 finalizedDepositChan : finalizedDepositChan ,
181+ spentChan : make (chan struct {}),
171182 }
172183
173184 depositStates := depoFsm .DepositStatesV0 ()
@@ -191,19 +202,22 @@ func NewFSM(ctx context.Context, deposit *Deposit, cfg *ManagerConfig,
191202
192203 depoFsm .ActionEntryFunc = depoFsm .updateDeposit
193204
194- go func () {
205+ go func (fsm * FSM ) {
206+ defer close (fsm .quitChan )
207+
195208 for {
196209 select {
197- case currentHeight := <- depoFsm .blockNtfnChan :
198- depoFsm .handleBlockNotification (
199- ctx , currentHeight ,
200- )
210+ case currentHeight := <- fsm .blockNtfnChan :
211+ fsm .handleBlockNotification (ctx , currentHeight )
212+
213+ case <- fsm .spentChan :
214+ return
201215
202216 case <- ctx .Done ():
203217 return
204218 }
205219 }
206- }()
220+ }(depoFsm )
207221
208222 return depoFsm , nil
209223}
0 commit comments