Skip to content

Commit a86774a

Browse files
author
Ariel Silahian
committed
Improve queue shutdown and error handling in worker loop
Wrapped pause and channel wait logic in try-catch blocks to handle cancellation, channel closure, and unexpected exceptions. This ensures the background worker exits gracefully and improves robustness during shutdown scenarios.
1 parent d07a6b6 commit a86774a

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

VisualHFT.Commons/Helpers/HelperCustomQueue.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,40 @@ private async Task RunConsumer()
229229
// Check if paused - wait for resume signal (already on background thread)
230230
if (Volatile.Read(ref _isPaused))
231231
{
232-
_pauseEvent.Wait(_token);
232+
try
233+
{
234+
_pauseEvent.Wait(_token);
235+
}
236+
catch (OperationCanceledException)
237+
{
238+
// Expected when stopping - exit gracefully
239+
break;
240+
}
233241
continue;
234242
}
235243

244+
236245
// Wait for items to be available - Channel's native async waiting
237-
if (!await _reader.WaitToReadAsync(_token))
238-
break; // Channel completed or cancelled
246+
// Should handle the cancellation gracefully
247+
try
248+
{
249+
if (!await _reader.WaitToReadAsync(_token))
250+
break;
251+
}
252+
catch (OperationCanceledException)
253+
{
254+
// Expected when stopping - exit gracefully
255+
break;
256+
}
257+
catch (ChannelClosedException)
258+
{
259+
// Channel was closed - exit gracefully
260+
break;
261+
}
262+
catch (Exception)
263+
{
264+
break;
265+
}
239266

240267
// Process all available items in batch
241268
int processedCount = 0;

0 commit comments

Comments
 (0)