Skip to content

SingleConsumerUnboundedChannel should allow access to reader count #53355

Open
@Danielku15

Description

@Danielku15

Description

The SingleConsumerUnboundedChannel currently does not allow accessing the item count. This is strange because if you have multiple consumers via UnboundedChannel the access is possible. It feels like a bug to me that you cannot access the count if you have a single consumer but you can if you have multiple ones.

Configuration

.net 5.0.203
Windows 10 Enterprise 2004
x64

Also seems an issue according to the latest source state.

Regression?

From a pure code perspective no. This was never implemented and is sort of a missing feature.

Other information

Code wise it should be fairly easy to add the same count implementation to SingleConsumerUnboundedChannel like in UnboundedChannel .

UnboundedChannel overrides the count and cancount in the reader implementation

public override bool CanCount => true;
public override int Count => _parent._items.Count;

SingleConsumerUnboundedChannel does not override the members

{
_parent = parent;
_readerSingleton = new AsyncOperation<T>(parent._runContinuationsAsynchronously, pooled: true);
_waiterSingleton = new AsyncOperation<bool>(parent._runContinuationsAsynchronously, pooled: true);
}
public override Task Completion => _parent._completion.Task;
public override ValueTask<T> ReadAsync(CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)

But I have to admit that I am not fully aware of potential consequences. There were some concerns by @stephentoub because of the queue not being secured by locks or interlocked operations. The UnboundedChannel uses a Deque which holds the current size as member. So accessing the size is safe. But the SingleConsumerUnboundedChannel uses a SingleProducerConsumerQueue where the count it only internal and requires looping.

From this perspective it might really rather be a feature request than a bug to support the count.

According to the git history the following PR and issue are related to the change adding the count member to UnboundedChannel.
#312
#26706

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions