Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n3ccE-0006Pl-P5 for pgsql-odbc@arkaria.postgresql.org; Sat, 01 Jan 2022 11:32:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n3ccB-0003ik-IY for pgsql-odbc@arkaria.postgresql.org; Sat, 01 Jan 2022 11:31:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n2v41-0001gR-E8 for pgsql-odbc@lists.postgresql.org; Thu, 30 Dec 2021 13:01:49 +0000 Received: from mail-yb1-xb31.google.com ([2607:f8b0:4864:20::b31]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n2v3x-0000W8-75 for pgsql-odbc@postgresql.org; Thu, 30 Dec 2021 13:01:48 +0000 Received: by mail-yb1-xb31.google.com with SMTP id k69so56348141ybf.1 for ; Thu, 30 Dec 2021 05:01:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=mime-version:from:date:message-id:subject:to; bh=lJilLatZ6nAOD+yUqiWyvzOhbls8osrgyX/KHvtIyEU=; b=mzOR0EN4HTWM0zBpMqhDI76bkM96oRZU7KeZqIq/8JIfvHzDZcGWc0Y4fbQzCapcxd Ea5oiwH0mhFGxGeEUgexWXsfX6LHS5tNwTvd7FkC3wTzSKQ8FBAIPE/5zyHcNGAiArYX IdrkDvbU6+YymlH98/LL766dcVwN8W6ZXz3EY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=lJilLatZ6nAOD+yUqiWyvzOhbls8osrgyX/KHvtIyEU=; b=qSX+PT8pIirNNqeuJ1MGIjeKyicmZMVwc88S9280MMKz90jh5Lqcc4788NzWd42Dnu eA5MQStSBzG+PAGHzTJyp5+4fYYj2Z9H2SrSXS4u/L3gYl+xABtsdn0gKyLEjMfmDO++ G++uEKINXcmOxSte+D9JQxyL+1etVSzGMYXuHxycjRVKphMwnljwIXvoJWEQo1Ro7w3S AYLR7wF1OG1CKcmLypPMO7OZinP44AdZKVwWhzzV26NKtAlhkwzDWzbxgrxH4qFUc/Fd Xdqv+lrnYWM6AHuL1iZo6L1bqw1pI+ITtkIPtWcQaXLDrzsGloGgaTvp7RNtmBrxFgO/ MBCw== X-Gm-Message-State: AOAM532btsxgvaZc1RCvAeQOssbPkajfomovRTCyf7u+rcfVfpAJsVi7 mxY2pyYy7WF5WTlcVipBD6HQu9IF+ksipbSz2zCMwTqtngeFHe+g X-Google-Smtp-Source: ABdhPJxj2vba3xjWHJIgl5NJ7F5QY2UE8fEfxEh5YvFVEiWvQasFZX8rN7GT+YPL7Qeocwl1XlDnJSoWyhn49ykMOW8= X-Received: by 2002:a25:d889:: with SMTP id p131mr33338684ybg.475.1640869302909; Thu, 30 Dec 2021 05:01:42 -0800 (PST) MIME-Version: 1.0 From: Nicolae Vartolomei Date: Thu, 30 Dec 2021 13:01:32 +0000 Message-ID: Subject: BUG: CC_send_query_append incomplete error handling To: pgsql-odbc@postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, I noticed a bug where psqlodbc doesn't handle an underlying error correctly. The interaction is roughly as follows: Initiate a connection, run few queries, run `ss -K dport postgresql` to kill the psqlodbc connection to postgres, then run another query. The expected behaviour is for query to fail and to return an error like "08S01". Instead, the following error is generated: ``` pgapi30.c[PGAPI_GetDiagRec]43: entering type=3 rec=1 environ.c[ER_ReturnError]202: entering status = 1, msg = #no connection to the server... environ.c[ER_ReturnError]259: szSqlState = 'HY000',len=58, szError='no connection to the server ``` This makes it a bit tricky to properly handle the exception on the client. I'm browsing through this code base for the first time, and the incomplete error handling seems to be around these lines in connection.c ``` if (!PQsendQuery(self->pqconn, query_buf.data)) { char *errmsg = PQerrorMessage(self->pqconn); QLOG(0, "\nCommunication Error: %s\n", SAFE_STR(errmsg)); CC_set_error(self, CONNECTION_COMMUNICATION_ERROR, errmsg, func); goto cleanup; } ``` I think this need a call to `CC_on_abort(self, CONN_DEAD);` in order to clean up the connection so that future calls would get trapped by `SC_connection_lost_check` call which also propagates the correct error to `SQLGetDiagRec` with a statement handle. Latest tested version is from this package https://packages.ubuntu.com/focal/odbc-postgresql Let me know if a test case for reproducing the issue is needed. The issue affects this code https://github.com/ClickHouse/ClickHouse/blob/86040a15d80c9371639ee0fac2879da749612a3f/programs/odbc-bridge/ODBCConnectionFactory.h#L76-L96 (nanodbc->unixODBC->psqlodbc) --- nvartolomei