"Randomly" Hundereds of OperationCanceledExceptions when requesting endpoints of .NET 6 API

Luuk777w 1 Reputation point
2022-03-08T20:45:07.697+00:00

I have a .NET 6 WebAPI, and I am using a Postgres database (with npgsql). (also, I am hosting the database in my dev environment in a docker container, using version 12 of Postgres)
When I visit an individual endpoint, everything works like it is supposed to.
Also, all my unit tests AND integration tests succeed, so it seems like my code is working correctly.

For my front end I am using Next.JS, and I am querying the data with SWR and Gaxios.
Each page will do about 15 requests to the API, and this is normally very fast (less than 500ms).
However, sometimes it is not, and it takes multiple seconds (10+)

The problem is that the API starts throwing hundreds and sometimes even thousands of OperationCanceledExceptions.
I can be browsing around on the front-end, and everything is just fine for 10 minutes, and then out of nowhere, 1100 OperationCanceledExceptions.

I have some logs: https://gist.github.com/luuk777w/15e82b2777da12ac97ffbf32c614c3c0
And this one with debug on: https://gist.github.com/luuk777w/a079ae19e4abf9a25987540c8c907176

My idea is that the problem somewhere lies with npgsql or something.
When using Historical Debugging most of the time it points to my repository or something else database related.

Just to give an idea of where it points to:

    public class EfRepository<T> : IAsyncRepository<T> where T : class, IAggregateRoot
    {
        protected readonly AppDbContext _dbContext;

        public EfRepository(AppDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public virtual async Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default)
        {
            var keyValues = new object[] { id };

            return await _dbContext.Set<T>().FindAsync(keyValues, cancellationToken) ?? await Task.FromResult<T>(null!);
        }

        public virtual async Task<T> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
        {
            var keyValues = new object[] { id };

            return await _dbContext.Set<T>().FindAsync(keyValues, cancellationToken) ?? await Task.FromResult<T>(null!);
        }

        // ......
    }
}

Most of the time it points to the return line of GetByIdAsync, and sometimes it can't load the file.

Anyone any idea what the problem could be, or where to search?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,242 questions
Azure Database for PostgreSQL
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,966 Reputation points
    2022-03-11T17:43:30.2+00:00

    your node library has a 1 second timeout (by default) on network requests. you might bump this up.

    note: webapi does not reliably detect the connection close (from the client timeout), until it sends data back. this is probably causing you cascade effect.

    0 comments No comments