✅ BackgroundService blocking on Startup

I have a service that spins up a few background services, but when one of them is being started up it is blocking the Host startup and preventing any background services registered later on from starting up. The gist of what it's doing in the Execute block is this, which is pretty similar to the other services, but whereas where they will return the Task when the host calls StartAsync, this one will continue executing.
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Starting processing");
while (!stoppingToken.IsCancellationRequested)
{
if (_queueService.IsEmpty)
{
// Wait until some messages to process come through
await Task.Delay(_emptyQueueDelay, stoppingToken);
continue;
}

IEnumerable<StreamMessage<MessageType>> messages = _queueService
.DequeueUpTo(_processingBatchMaxSize);

await ProcessMessages(messages);
}
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Starting processing");
while (!stoppingToken.IsCancellationRequested)
{
if (_queueService.IsEmpty)
{
// Wait until some messages to process come through
await Task.Delay(_emptyQueueDelay, stoppingToken);
continue;
}

IEnumerable<StreamMessage<MessageType>> messages = _queueService
.DequeueUpTo(_processingBatchMaxSize);

await ProcessMessages(messages);
}
}
Relatively new to working with Background Services, but I can't see where the issue might be 😅
5 Replies
dance?
dance?3w ago
i find it hard to believe that this is blocking startup either some these services is a IHostedService instead of a BackgroundService or there's a lock somewhere maybe
Jamie Brown
Jamie BrownOP3w ago
I can't see a reason why it should be, but it seems tied to this class in particular. All services are a BackgroundService behaviour I see in Host is in this block:
await ForeachService(_hostedServices, cancellationToken, concurrent, abortOnFirstException, exceptions,
async (service, token) =>
{
await service.StartAsync(token).ConfigureAwait(false);

if (service is BackgroundService backgroundService)
{
_ = TryExecuteBackgroundServiceAsync(backgroundService);
}
}).ConfigureAwait(false);
await ForeachService(_hostedServices, cancellationToken, concurrent, abortOnFirstException, exceptions,
async (service, token) =>
{
await service.StartAsync(token).ConfigureAwait(false);

if (service is BackgroundService backgroundService)
{
_ = TryExecuteBackgroundServiceAsync(backgroundService);
}
}).ConfigureAwait(false);
The other two services will call start and then continue, but in the StartAsync call for this service, it will call ExecuteAsync but never make it past it. Seemingly not picking up the task?
public virtual Task StartAsync(CancellationToken cancellationToken)
{
// Create linked token to allow cancelling executing task from provided token
_stoppingCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

// Store the task we're executing
_executeTask = ExecuteAsync(_stoppingCts.Token);

// If the task is completed then return it, this will bubble cancellation and failure to the caller
if (_executeTask.IsCompleted)
{
return _executeTask;
}

// Otherwise it's running
return Task.CompletedTask;
}
public virtual Task StartAsync(CancellationToken cancellationToken)
{
// Create linked token to allow cancelling executing task from provided token
_stoppingCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

// Store the task we're executing
_executeTask = ExecuteAsync(_stoppingCts.Token);

// If the task is completed then return it, this will bubble cancellation and failure to the caller
if (_executeTask.IsCompleted)
{
return _executeTask;
}

// Otherwise it's running
return Task.CompletedTask;
}
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Jamie Brown
Jamie BrownOP3w ago
Okay sorted it, thank you! You were right about it blocking, I was using async/await in there but everything it awaits is completing synchronously so I guess that's where my issue is cropping up 😅 !close
Accord
Accord3w ago
Closed!

Did you find this page helpful?