npgsql: NotSupportedException when using async notifications

Dear Team,

I’m using Npgsql 2.1.3 and get the exception when being subscribed for Listen/Notify events. The exception happens when some command is invoked and async notification arrives at the same time, but it does not happen always.

Here goes the message and stacktrace:

An unhandled exception of type ‘System.NotSupportedException’ occurred in Npgsql.dll

Additional information: Cannot write to a BufferedStream while the read buffer is not empty if the underlying stream is not seekable. Ensure that the stream underlying this BufferedStream can seek or avoid interleaving read and write operations on this BufferedStream.

   at System.IO.BufferedStream.WriteByte(Byte value)
   at Npgsql.NpgsqlQuery.WriteToStream(Stream outputStream)
   at Npgsql.NpgsqlReadyState.Query(NpgsqlConnector context, NpgsqlQuery query)
   at Npgsql.NpgsqlConnector.Query(NpgsqlQuery query)
   at Npgsql.NpgsqlCommand.GetReader(CommandBehavior cb)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior cb)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
   at NpgsqlNotifyTest.Program.<>c__DisplayClass6.<Main>b__3() in d:\NpgsqlNotifyTest\NpgsqlNotifyTest\Program.cs:line 27
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Please use following code to reproduce the issue (it takes a couple of seconds - just leave it running).

using System.Data;
using System.Diagnostics;
using System.Threading;
using Npgsql;

namespace NpgsqlNotifyTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var builder = new NpgsqlConnectionStringBuilder { Host = "127.0.0.1", Database = "postgres", UserName = "postgres" };


            var thread1 = new Thread(() =>
            {
                var connection1 = new NpgsqlConnection(builder);
                connection1.Open();
                connection1.Notification += (sender, e) => Debug.WriteLine("NOTIFICATION RECEIVED [{1}] {0}", e.Condition, e.PID);
                var command1 = new NpgsqlCommand { Connection = connection1, CommandText = "LISTEN marek" };
                command1.ExecuteNonQuery();

                command1.CommandText = "select * from pg_class";
                while (true)
                {
                    var da = new NpgsqlDataAdapter(command1);
                    da.Fill(new DataTable());
                    Thread.Sleep(1000);
                }
            });
            thread1.Start();


            var thread2 = new Thread(() =>
            {
                var connection2 = new NpgsqlConnection(builder);
                connection2.Open();
                var command2 = new NpgsqlCommand { Connection = connection2, CommandText = "NOTIFY marek" };
                while (true)
                {
                    command2.ExecuteNonQuery();
                    Thread.Sleep(100);
                }
            });
            thread2.Start();

            thread1.Join();
        }
    }
}

Thanks in advance for checking above issue if it’s a bug.

Regards, Marek

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 23 (12 by maintainers)

Most upvoted comments

I’m also getting this error when attempting to use Npgsql from a HangFire background job.