Notification event not fired for multiple notifications #1562

Closed
WAmadeus opened this Issue May 19, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@WAmadeus

WAmadeus commented May 19, 2017

I have the following class to subscribe to events:

public class Event : IEvent
{
	private CancellationTokenSource m_Token;
	private List<string> m_Events;
	private Action<string, string> m_Callback;

	public Event(List<string> events, Action<string, string> callback)
	{
		m_Token = new CancellationTokenSource();
		m_Events = events;
		m_Callback = callback;
		new Thread(run).Start();
	}

	public void Dispose()
	{
		m_Token.Cancel();
		m_Token.Dispose();
	}

	private async void run()
	{
		using (var connection = new NpgsqlConnection(DatabaseController.Instance.StringConexaoBancoDadosLeitura))
		{
			connection.Open();
			connection.Notification += (s, e) =>
				{
					Log.Trace.WriteLine($"NOTIFY {e.Condition} {e.AdditionalInformation}");
					m_Callback(e.Condition, e.AdditionalInformation);
				};
			foreach (var item in m_Events)
			{
				using (var command = new NpgsqlCommand($"LISTEN {item};", connection))
				{
					command.ExecuteNonQuery();
				}
			}
			while (!m_Token.IsCancellationRequested)
			{
				try
				{
					await connection.WaitAsync(m_Token.Token);
				}
				catch (OperationCanceledException)
				{
					break;
				}
				catch
				{
				}
			}
		}
	}
}

A notification is generated by a trigger for each INSERT/UPDATE/DELETE on a table. The problem is when multiple operations on that table are performed in a short time, only 1 notification is received.

Npgsql version: 3.2.2
PostgreSQL version: 9.6
Operating system: Windows 10 64bits

@roji

This comment has been minimized.

Show comment
Hide comment
@roji

roji May 23, 2017

Member

I played around with this a little and have noticed some strange behavior when generating notifications with NOTIFY: sending two notifications in the same command (NOTIFY x; NOTIFY x) only sends a single one, whereas splitting that into two commands causes two notifications to be properly generated. This may be a PostgreSQL behavior quirk.

Any chance you can fire up wireshark and inspect exactly what's going on at the PostgreSQL protocol level? If we see just one notification message delivered on the wire, we know it's a PostgreSQL-side issue. Otherwise, it's an Npgsql issue.

Member

roji commented May 23, 2017

I played around with this a little and have noticed some strange behavior when generating notifications with NOTIFY: sending two notifications in the same command (NOTIFY x; NOTIFY x) only sends a single one, whereas splitting that into two commands causes two notifications to be properly generated. This may be a PostgreSQL behavior quirk.

Any chance you can fire up wireshark and inspect exactly what's going on at the PostgreSQL protocol level? If we see just one notification message delivered on the wire, we know it's a PostgreSQL-side issue. Otherwise, it's an Npgsql issue.

@WAmadeus

This comment has been minimized.

Show comment
Hide comment
@WAmadeus

WAmadeus May 24, 2017

Thanks for this info!
I'll try and let you know.

Thanks for this info!
I'll try and let you know.

@roji roji removed the waiting for answer label Jun 8, 2018

@roji

This comment has been minimized.

Show comment
Hide comment
@roji

roji Jun 9, 2018

Member

Closing for age and inactivity, but will reopen if there's a need.

Member

roji commented Jun 9, 2018

Closing for age and inactivity, but will reopen if there's a need.

@roji roji added the invalid label Jun 9, 2018

@roji roji closed this Jun 20, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment