question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Postgres connection state stays active when an exception occurs

See original GitHub issue

Issue

The connection state in pool is active even after the close/dispose methods are called. This issue is replicated only when you get an exception while executing a query or stored procedure.

Run the following code:

static void Main(string[] args) {
	NpgsqlCommand cmd = null;
	NpgsqlDataAdapter sda = null;
	NpgsqlConnection conn = null;
	try {
		string sql = "a_test";
		conn = new NpgsqlConnection("Server=localhost;Port=5432;Username=admin;Password=test;Database=majordb;SearchPath=dbs;CommandTimeout=60;MaxPoolSize=500;Connection Idle Lifetime=180;Connection Pruning Interval=5;");
		cmd = new NpgsqlCommand(sql);
		cmd.CommandType = CommandType.StoredProcedure;
		sda = new NpgsqlDataAdapter(cmd);
		cmd.Connection = conn;
		sda.Fill(dataTable);
	} 
	catch (Exception e) {

	}
	finally {
		if(null != sda)
		{
			try
			{
				sda.Dispose();                        
			}
			catch (Exception)
			{
			}
		}
		try
		{
			cmd.Connection.Close();
			cmd.Connection.Dispose();
		}
		catch (Exception)
		{
		}
		try
		{
			cmd.Dispose();
		}
		catch (Exception)
		{
		}
	}
}

To replicate the exception, I have create a test procedure that has pgsleep() greater than commandtimeout.

The Details

The issue details are also listed here in the below link: https://stackoverflow.com/questions/65629984/postgres-connection-state-is-active-when-an-exception-occurs

If the above code executes properly without any exception, the connection state in pool goes to idle, which is correct. But if an exception occurs while executing, like below:

"Npgsql.NpgsqlException (0x80004005): Exception while reading from stream ---> 
System.IO.IOException: Unable to read data from the transport connection: A connection attempt 
failed because the connected party did not properly respond after a period of time, or established 
connection failed because connected host has failed to respond."

The connection state in pool shows as active for about 5 mins or so, even though the close/dispose methods are called in finally block. This means the close/dispose did not properly executed by Npgsql. If the program keeps the connection state in pool active for every connection ran within 5 mins, then there can be an issue with MaxPoolSize error.

I wanted to see the connection state to idle, even when there is an exception. How do I do this.

Please note: I am not looking for a solution to the exception that I listed above. I am looking for a solution where the connection state is changed to idle from active when there is an exception while executing the above code.

To know if the connection state is active or not I used the following query:

SELECT
    pid,
    usename,
    application_name,
    datname,
    client_addr,
    rank() over (partition by client_addr order by backend_start ASC) as rank,
    state,
    state_change,
    current_timestamp,
    query,
    query_start,
    backend_start,
    FROM 
    pg_stat_activity
WHERE
    pid <> pg_backend_pid( )
AND
    application_name !~ '(?:psql)|(?:pgAdmin.+)'
AND
    datname = current_database() 
AND
    usename = current_user 

I have took screenshot for your reference for the above query. active1 active2 active3 active4 active5

Further technical details

Npgsql version: 4.1.7 PostgreSQL version: “PostgreSQL 11.8 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11), 64-bit”

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
vonzshikcommented, Jan 13, 2021

It’s not related to #3446. The problem here is, the query executes for far too long, we fail with a timeout and the connection is broken. The query still executes on the backend, as the cancellation was implemented with 5.0.

1reaction
vonzshikcommented, Jan 13, 2021

@roji it’s 4.1.7 - there is no cancellation on the backend on timeouts until 5.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostgreSQL: Documentation: 15: 43.9. Errors and Messages
EXCEPTION raises an error (which normally aborts the current transaction); the other levels only generate messages of different priority levels.
Read more >
How to deal with closed connections in database pool
This article explains how to overcome the "connection is closed" error that sometimes is seen on the mule logs when connecting to a...
Read more >
Troubleshoot common connection issues to Azure SQL ...
Provides steps to troubleshoot Azure SQL Database connection ... These exceptions can occur either because of connection or query issues.
Read more >
Transactions management
# If your program spends a long time in this state, the server will keep # a connection "idle in transaction", which is...
Read more >
Connection problems to PostgreSQL result in stuck threads ...
Solution · tcpKeepAlive: checks whether the connection is still running. · socketTimeout: terminates the connection after the specified time (in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found