npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.54k stars 226 forks source link

Unable to write data to the transport connection: Operation canceled. #3161

Closed choby closed 5 months ago

choby commented 5 months ago

Applications often make errors after a recent upgrade to.NET 8:

Npgsql.NpgsqlException: Exception while writing to stream ---> System.IO.IOException: Unable to write data to the transport connection: Operation canceled. ---> System.Net.Sockets.SocketException: Operation canceled at Npgsql.Internal.NpgsqlWriteBuffer.d32.MoveNext() at offset 282 --- End of inner exception stack trace --- at Npgsql.Internal.NpgsqlWriteBuffer.d32.MoveNext() at offset 682 at Npgsql.NpgsqlCommand.<g__WriteExecute|100_0>d.MoveNext() at offset 1709 at Npgsql.NpgsqlCommand.d119.MoveNext() at offset 1155 at Npgsql.NpgsqlCommand.d107.MoveNext() at offset 146 at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.d14.MoveNext() at offset 779 at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.d17.MoveNext() at offset 279

When this error occurs, the database fails to log the error log most of the time, Occasionally, however, the following errors are recorded: ERROR: SAVEPOINT can only be used in transaction blocks

pgsql: 14 system: ubuntu 22.04 .net: 8.0 efcore 8.0 efcore.pg:8.0.2 abp:8.0.5

NinoFloris commented 5 months ago

Which versions of npgsql and efcore.pg is your project referencing?

choby commented 5 months ago

Which versions of npgsql and efcore.pg is your project referencing?

8.0.2

choby commented 5 months ago

There was another strange problem, after the initial upgrade to.NET 8, there were frequent ssl errors, which did not reappear after I set ssl mode to disable in the connection string

roji commented 5 months ago

@choby I'm not sure what we can do with such a partial bug report (just a stack trace). I'd need some sort of minimal repro, or at least full statement logging from the PG side to see what is being sent to the database.

choby commented 5 months ago

I found that if I used tojson() to map jsonb fields, I encountered this problem when I manually enabled multiple UnitOfWorks, and I didn't encounter this problem when I fell back on the tojson() method to HasColumnType("jsonb"). Also note that I made an http request in the UnitOfWork

choby commented 5 months ago

And this problem seems to exist only in certain scenarios, for example, I run it perfectly in debug mode on my macbook, but I run it 3-4 times out of 10 on ubuntu

roji commented 5 months ago

@choby thanks for the added detail, but the information above is still very partial, and I'm not sure what we can do to investigate this.

If you can reproduce the problem 3-4 times out of 10 on Ubuntu, please try putting together a minimal, runnable repro (i.e. console application) that does this - a good way is to simply run the code in a loop until it fails. If you manage to do that, please post that back here and we'll revisit. In the meantime I'll go ahead and close the issue as we can't investigate it without a repro.