@@ -2317,7 +2317,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
2317
2317
Some ( ( _cp_id, chan_id) ) => Some ( chan_id. clone ( ) ) ,
2318
2318
} ;
2319
2319
let chan_update_opt = if let Some ( forwarding_id) = forwarding_id_opt {
2320
- let chan = channel_state. by_id . get_mut ( & forwarding_id) . unwrap ( ) ;
2320
+ let chan = match channel_state. by_id . get_mut ( & forwarding_id) {
2321
+ None => {
2322
+ // Channel was removed. The short_to_chan_info and by_id maps have
2323
+ // no consistency guarantees.
2324
+ break Some ( ( "Don't have available channel for forwarding as requested." , 0x4000 | 10 , None ) ) ;
2325
+ } ,
2326
+ Some ( chan) => chan
2327
+ } ;
2321
2328
if !chan. should_announce ( ) && !self . default_configuration . accept_forwards_to_priv_channels {
2322
2329
// Note that the behavior here should be identical to the above block - we
2323
2330
// should NOT reveal the existence or non-existence of a private channel if
@@ -2544,7 +2551,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
2544
2551
} ,
2545
2552
None => { } ,
2546
2553
}
2547
- } else { unreachable ! ( ) ; }
2554
+ } else {
2555
+ // The channel was likely removed after we fetched the id from the
2556
+ // `short_to_chan_info` map, but before we successfully locked the `by_id` map.
2557
+ // This can occur as no consistency guarantees exists between the two maps.
2558
+ return Err ( APIError :: ChannelUnavailable { err : "No channel available with first hop!" . to_owned ( ) } ) ;
2559
+ }
2548
2560
return Ok ( ( ) ) ;
2549
2561
} ;
2550
2562
@@ -3133,9 +3145,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3133
3145
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3134
3146
let channel_state = & mut * channel_state_lock;
3135
3147
if short_chan_id != 0 {
3136
- let forward_chan_id = match self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & short_chan_id) {
3137
- Some ( ( _cp_id, chan_id) ) => chan_id. clone ( ) ,
3138
- None => {
3148
+ macro_rules! forwarding_channel_not_found {
3149
+ ( ) => {
3139
3150
for forward_info in pending_forwards. drain( ..) {
3140
3151
match forward_info {
3141
3152
HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
@@ -3222,6 +3233,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3222
3233
}
3223
3234
}
3224
3235
}
3236
+ }
3237
+ }
3238
+ let forward_chan_id = match self . short_to_chan_info . read ( ) . unwrap ( ) . get ( & short_chan_id) {
3239
+ Some ( ( _cp_id, chan_id) ) => chan_id. clone ( ) ,
3240
+ None => {
3241
+ forwarding_channel_not_found ! ( ) ;
3225
3242
continue ;
3226
3243
}
3227
3244
} ;
@@ -3351,7 +3368,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3351
3368
} ) ;
3352
3369
}
3353
3370
} else {
3354
- unreachable ! ( ) ;
3371
+ forwarding_channel_not_found ! ( ) ;
3372
+ continue ;
3355
3373
}
3356
3374
} else {
3357
3375
for forward_info in pending_forwards. drain ( ..) {
@@ -4301,7 +4319,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4301
4319
return ClaimFundsFromHop :: MonitorUpdateFail ( counterparty_node_id, res, None ) ;
4302
4320
} ,
4303
4321
}
4304
- } else { unreachable ! ( ) ; }
4322
+ } else { return ClaimFundsFromHop :: PrevHopForceClosed }
4305
4323
}
4306
4324
4307
4325
fn finalize_claims ( & self , mut sources : Vec < HTLCSource > ) {
@@ -5217,7 +5235,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
5217
5235
try_chan_entry ! ( self , chan. get_mut( ) . channel_update( & msg) , chan) ;
5218
5236
}
5219
5237
} ,
5220
- hash_map:: Entry :: Vacant ( _) => unreachable ! ( )
5238
+ hash_map:: Entry :: Vacant ( _) => return Ok ( NotifyOption :: SkipPersist )
5221
5239
}
5222
5240
Ok ( NotifyOption :: DoPersist )
5223
5241
}
0 commit comments