Skip to content

[Code Quality] ServiceManager.GetAllServices — trackedTasks ConcurrentBag is declared and joined but never populated (dead safety gate) #986

@Christophe-Rogiers

Description

@Christophe-Rogiers

Severity: Info

File: src/Servy.Core/Services/ServiceManager.cs:883-967

Code:

```csharp
public List GetAllServices(CancellationToken cancellationToken = default(CancellationToken))
{
var results = new ConcurrentBag();
var services = _serviceControllerProvider.GetServices();

// Even in a sync-first approach, we track any underlying async tasks
// spawned by the provider or previous refresh attempts to protect the handle.
var trackedTasks = new ConcurrentBag<Task>();
...
finally
{
    // Safety Gate: Ensure any background tasks (from previous or forked logic)
    // are observed before we drop the SCM handle.
    if (trackedTasks.Count > 0)
    {
        try { Task.WaitAll(trackedTasks.ToArray(), 5000); } catch { /* Ignore */ }
    }

    scmHandle?.Dispose();
}

}
```

Explanation:

`trackedTasks` is allocated and inspected in the `finally` block, but nothing ever calls `trackedTasks.Add(...)`. The `Count > 0` check is therefore always false and `Task.WaitAll` never runs. Either the synchronous design has obsoleted the safety gate (in which case the bag and finally branch should be removed), or there is a missing `trackedTasks.Add(...)` call somewhere in the parallel body that should be capturing async work spawned by `PopulateNativeDetails`. As written, the comments imply a guarantee the code does not actually provide.

Suggested fix:

Either delete the dead bag and the corresponding `if (trackedTasks.Count > 0)` block, or wire the parallel body to actually push into it (e.g. when `PopulateNativeDetails` returns a `Task`). The comment about "async tasks spawned by the provider" should not survive without the corresponding code.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions