This is an automated email from the ASF dual-hosted git repository.

lidavidm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-adbc.git


The following commit(s) were added to refs/heads/main by this push:
     new e31e4df9e fix(c/driver/postgresql): prevent crash when server 
terminates connection (#3891)
e31e4df9e is described below

commit e31e4df9e99a0898287d39dc015b511cc4132f2c
Author: Mandukhai Alimaa <[email protected]>
AuthorDate: Sat Jan 24 23:14:03 2026 -0600

    fix(c/driver/postgresql): prevent crash when server terminates connection 
(#3891)
    
    Move the SetSqlState call inside the null check to only execute when
    sqlstate is valid.
    
    Closes #3878.
---
 c/driver/postgresql/error.h                       |  4 +++-
 python/adbc_driver_postgresql/tests/test_dbapi.py | 29 +++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/c/driver/postgresql/error.h b/c/driver/postgresql/error.h
index f24d41754..bf8a4b2e4 100644
--- a/c/driver/postgresql/error.h
+++ b/c/driver/postgresql/error.h
@@ -99,7 +99,9 @@ Status MakeStatus(PGresult* result, const char* 
format_string, Args&&... args) {
   }
 
   Status status(code, message);
-  status.SetSqlState(sqlstate);
+  if (sqlstate) {
+    status.SetSqlState(sqlstate);
+  }
   for (const auto& field : kDetailFields) {
     const char* value = PQresultErrorField(result, field.code);
     if (value) {
diff --git a/python/adbc_driver_postgresql/tests/test_dbapi.py 
b/python/adbc_driver_postgresql/tests/test_dbapi.py
index 5fe72cfa3..952389de2 100644
--- a/python/adbc_driver_postgresql/tests/test_dbapi.py
+++ b/python/adbc_driver_postgresql/tests/test_dbapi.py
@@ -557,3 +557,32 @@ def test_connect_conn_kwargs_db_schema(postgres_uri: str, 
postgres: dbapi.Connec
     with dbapi.connect(postgres_uri, conn_kwargs={schema_key: schema_name}) as 
conn:
         option_value = conn.adbc_connection.get_option(schema_key)
         assert option_value == schema_name
+
+
+def test_server_terminates_connection(postgres_uri: str) -> None:
+    """Test that driver handles server terminating the connection gracefully.
+
+    Reproduces: https://github.com/apache/arrow-adbc/issues/3878
+    When the server terminates a connection, the driver should return an error
+    instead of crashing with a segfault.
+    """
+    with dbapi.connect(postgres_uri) as conn1:
+        with dbapi.connect(postgres_uri) as conn2:
+            # Get the backend PID of conn2
+            with conn2.cursor() as cur:
+                cur.execute("SELECT pg_backend_pid()")
+                row = cur.fetchone()
+                assert row is not None
+                backend_pid = row[0]
+            conn2.commit()
+
+            # simulate a server side termination of a connection2
+            with conn1.cursor() as cur:
+                cur.execute(f"SELECT pg_terminate_backend({backend_pid})")
+            conn1.commit()
+
+            # Try to execute a query on the terminated connection
+            # This should raise an exception, NOT crash with a segfault
+            with pytest.raises(Exception):
+                with conn2.cursor() as cur:
+                    cur.execute("SELECT 1")

Reply via email to