@@ -780,16 +780,19 @@ func (b *Batcher) PresignSweepsGroup(ctx context.Context, inputs []Input,
780780// times, but the sweeps (including the order of them) must be the same. If
781781// notifier is provided, the batcher sends back sweeping results through it.
782782func (b * Batcher ) AddSweep (ctx context.Context , sweepReq * SweepRequest ) error {
783- // If the batcher is shutting down, quit now.
784- select {
785- case <- b .quit :
786- return ErrBatcherShuttingDown
787-
788- default :
783+ // If the batcher or the caller is shutting down, quit now.
784+ err := b .addSweepExitErr (ctx )
785+ if err != nil {
786+ return err
789787 }
790788
791789 sweeps , err := b .fetchSweeps (ctx , * sweepReq )
792790 if err != nil {
791+ exitErr := b .addSweepExitErr (ctx )
792+ if exitErr != nil {
793+ err = exitErr
794+ }
795+
793796 return fmt .Errorf ("fetchSweeps failed: %w" , err )
794797 }
795798
@@ -803,6 +806,11 @@ func (b *Batcher) AddSweep(ctx context.Context, sweepReq *SweepRequest) error {
803806
804807 completed , err := b .store .GetSweepStatus (ctx , sweep .outpoint )
805808 if err != nil {
809+ exitErr := b .addSweepExitErr (ctx )
810+ if exitErr != nil {
811+ err = exitErr
812+ }
813+
806814 return fmt .Errorf ("failed to get the status of sweep %v: %w" ,
807815 sweep .outpoint , err )
808816 }
@@ -816,6 +824,11 @@ func (b *Batcher) AddSweep(ctx context.Context, sweepReq *SweepRequest) error {
816824 // on-chain confirmations to prevent issues caused by reorgs.
817825 parentBatch , err = b .store .GetParentBatch (ctx , sweep .outpoint )
818826 if err != nil {
827+ exitErr := b .addSweepExitErr (ctx )
828+ if exitErr != nil {
829+ err = exitErr
830+ }
831+
819832 return fmt .Errorf ("unable to get parent batch for " +
820833 "sweep %x: %w" , sweep .swapHash [:6 ], err )
821834 }
@@ -827,6 +840,11 @@ func (b *Batcher) AddSweep(ctx context.Context, sweepReq *SweepRequest) error {
827840
828841 minRelayFeeRate , err := b .wallet .MinRelayFee (ctx )
829842 if err != nil {
843+ exitErr := b .addSweepExitErr (ctx )
844+ if exitErr != nil {
845+ err = exitErr
846+ }
847+
830848 return fmt .Errorf ("failed to get min relay fee: %w" , err )
831849 }
832850
@@ -839,6 +857,11 @@ func (b *Batcher) AddSweep(ctx context.Context, sweepReq *SweepRequest) error {
839857 b .chainParams ,
840858 )
841859 if err != nil {
860+ exitErr := b .addSweepExitErr (ctx )
861+ if exitErr != nil {
862+ err = exitErr
863+ }
864+
842865 return fmt .Errorf ("inputs with primarySweep %v were " +
843866 "not presigned (call PresignSweepsGroup " +
844867 "first): %w" , sweep .outpoint , err )
@@ -861,7 +884,23 @@ func (b *Batcher) AddSweep(ctx context.Context, sweepReq *SweepRequest) error {
861884
862885 case <- b .quit :
863886 return ErrBatcherShuttingDown
887+
888+ case <- ctx .Done ():
889+ return b .addSweepExitErr (ctx )
890+ }
891+ }
892+
893+ // addSweepExitErr returns the terminal error to use when AddSweep races with
894+ // shutdown or caller cancellation.
895+ func (b * Batcher ) addSweepExitErr (ctx context.Context ) error {
896+ select {
897+ case <- b .quit :
898+ return ErrBatcherShuttingDown
899+
900+ default :
864901 }
902+
903+ return ctx .Err ()
865904}
866905
867906// testRunInEventLoop runs a function in the event loop blocking until
0 commit comments