Skip to content

Setting NpgsqlReadBuffer.Timeout property to Timeout.InfiniteTimeSpan gets overridden in setter #6122

@cokert

Description

@cokert

I'm not 100% sure if this is intentional or not, but if you were to set the Timeout property of an NpgsqlReadBuffer instance to Timeout.InfiniteTimeSpan, the result is value will be set to TimeSpan.Zero since the value of the InfiniteTimeSpan struct is -1ms.

Link to source, relevant block:

internal TimeSpan Timeout
{
    get => _preTranslatedTimeout;
    set
    {
        if (_preTranslatedTimeout != value)
        {
            _preTranslatedTimeout = value;

            if (value == TimeSpan.Zero)
                value = InfiniteTimeSpan;
            else if (value < TimeSpan.Zero)
                value = TimeSpan.Zero;

            Debug.Assert(_underlyingSocket != null);

            _underlyingSocket.ReceiveTimeout = (int)value.TotalMilliseconds;
            Cts.Timeout = value;
        }
    }
}

This can be worked around by just setting Timeout to TimeSpan.Zero, but this isn't at all obvious. You could check for value != Timeout.InfiniteTimeSpan, but that might have unintended consequences if someone were specifying -1ms for the timeout value expecting it to result in TimeSpan.Zero? Maybe there just needs to be some XMLDoc comments added to NpgsqlBinaryImporter.Timeout pointing this out?

Maybe relevant but probably not backstory on how I wound up here:

I ran into this because I was working on bulk inserts using this lib and would get timeouts if I inserted enough data. After running with local versions of both npgsql and the EFCore.BulkExtensions.MIT library, I figured out that the timeout was coming from NpgsqlBinaryImporter.Complete(). I played around with manually setting NpgsqlBinaryImporter.Timeout to Timeout.InfiniteTimeSpan immediately before calling Complete() but saw no effect and dug more and saw that I really needed to set it to TimeSpan.Zero to have the effect I wanted.

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions