@@ -226,6 +226,19 @@ func (p *paymentLifecycle) resumePayment(ctx context.Context) ([32]byte,
226226 // critical error during path finding.
227227lifecycle:
228228 for {
229+ // Before we attempt any new shard, we'll check to see if we've
230+ // gone past the payment attempt timeout or if the context was
231+ // canceled. If the context is done, the payment is marked as
232+ // failed and we reload the latest payment state to reflect
233+ // this.
234+ //
235+ // NOTE: This can be called several times if there are more
236+ // attempts to be resolved after the timeout or context is
237+ // cancelled.
238+ if err := p .checkContext (ctx ); err != nil {
239+ return exitWithErr (err )
240+ }
241+
229242 // We update the payment state on every iteration.
230243 currentPayment , ps , err := p .reloadPayment ()
231244 if err != nil {
@@ -241,19 +254,11 @@ lifecycle:
241254
242255 // We now proceed our lifecycle with the following tasks in
243256 // order,
244- // 1. check context.
245- // 2. request route.
246- // 3. create HTLC attempt.
247- // 4. send HTLC attempt.
248- // 5. collect HTLC attempt result.
257+ // 1. request route.
258+ // 2. create HTLC attempt.
259+ // 3. send HTLC attempt.
260+ // 4. collect HTLC attempt result.
249261 //
250- // Before we attempt any new shard, we'll check to see if we've
251- // gone past the payment attempt timeout, or if the context was
252- // cancelled, or the router is exiting. In any of these cases,
253- // we'll stop this payment attempt short.
254- if err := p .checkContext (ctx ); err != nil {
255- return exitWithErr (err )
256- }
257262
258263 // Now decide the next step of the current lifecycle.
259264 step , err := p .decideNextStep (payment )
0 commit comments