@@ -237,6 +237,10 @@ protected void Handle(TransactionsMessage msg)
237237 IOwnedReadOnlyList < Transaction > iList = msg . Transactions ;
238238 if ( ! BackgroundTaskScheduler . TryScheduleBackgroundTask ( new TransactionsRequest ( iList , 0 ) , _handleSlow , "Transactions" ) )
239239 {
240+ foreach ( Transaction tx in iList )
241+ {
242+ tx . ClearPreHash ( ) ;
243+ }
240244 iList . Dispose ( ) ;
241245 }
242246 }
@@ -245,43 +249,49 @@ protected virtual ValueTask HandleSlow(TransactionsRequest request, Cancellation
245249 {
246250 IOwnedReadOnlyList < Transaction > transactions = request . Transactions ;
247251 ReadOnlySpan < Transaction > transactionsSpan = transactions . AsSpan ( ) ;
252+
253+ int currentIdx = request . StartIndex ;
254+ bool isTrace = Logger . IsTrace ;
255+ bool isTransferred = false ;
256+
248257 try
249258 {
250- int startIdx = request . StartIndex ;
251- bool isTrace = Logger . IsTrace ;
252-
253- for ( int i = startIdx ; i < transactionsSpan . Length ; i ++ )
259+ while ( currentIdx < transactionsSpan . Length )
254260 {
255261 if ( cancellationToken . IsCancellationRequested )
256262 {
257- if ( i == startIdx )
263+ if ( currentIdx == request . StartIndex )
258264 {
259265 // Cancelled before processing any transaction — dispose and bail out.
260266 // Rescheduling would just loop (cancelled again immediately).
261- transactions . Dispose ( ) ;
262267 return ValueTask . CompletedTask ;
263268 }
264269
265- // Reschedule remaining transactions with a different start index
266- if ( ! BackgroundTaskScheduler . TryScheduleBackgroundTask ( new TransactionsRequest ( transactions , i ) , _handleSlow , "Transactions" ) )
270+ if ( BackgroundTaskScheduler . TryScheduleBackgroundTask ( new TransactionsRequest ( transactions , currentIdx ) , _handleSlow , "Transactions" ) )
267271 {
268- transactions . Dispose ( ) ;
272+ isTransferred = true ;
269273 }
274+
270275 return ValueTask . CompletedTask ;
271276 }
272277
273- PrepareAndSubmitTransaction ( transactionsSpan [ i ] , isTrace ) ;
278+ PrepareAndSubmitTransaction ( transactionsSpan [ currentIdx ] , isTrace ) ;
279+ currentIdx ++ ;
274280 }
275-
276- transactions . Dispose ( ) ;
277281 }
278- catch
282+ finally
279283 {
280- transactions . Dispose ( ) ;
281- throw ;
284+ if ( ! isTransferred )
285+ {
286+ while ( currentIdx < transactionsSpan . Length )
287+ {
288+ transactionsSpan [ currentIdx ] . ClearPreHash ( ) ;
289+ currentIdx ++ ;
290+ }
291+ transactions . Dispose ( ) ;
292+ }
282293 }
283294
284-
285295 return ValueTask . CompletedTask ;
286296 }
287297
0 commit comments