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 1n4bKp-00076g-8I for pgsql-odbc@arkaria.postgresql.org; Tue, 04 Jan 2022 04:22:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n4bKm-00018R-Ew for pgsql-odbc@arkaria.postgresql.org; Tue, 04 Jan 2022 04:22:04 +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 1n4bKm-00018H-4m for pgsql-odbc@lists.postgresql.org; Tue, 04 Jan 2022 04:22:04 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n4bKh-0001a4-VD for pgsql-odbc@postgresql.org; Tue, 04 Jan 2022 04:22:03 +0000 Received: by mail-ed1-x52b.google.com with SMTP id o6so143428068edc.4 for ; Mon, 03 Jan 2022 20:21:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=AiW10OsgeEcfqp89fNu83n/wDekrj0ObDJDzIAqkMkc=; b=MB4IUNpAOq+1y5OoVZsavYVwKMVE4hX5C/hVfGEwNDMxjliBa8cXiCu+gLdaq88w9w YVit6Jhdc7uX/Oi66h0IfuFoca5txuTp1raV2c/xSKfYpNZnxyqIpeoK/R6KIdYl+Ek8 PjPxBfas9rWHRavaC7gye6qjlYHmlSXpNB4IBZbtFGRu6yPGZC55CU1hDN0mXwYXaYGt BfLS7RqxDqYr4FVPPkQFVgeDmkbUG4vBl7VJSELwE2YKnOhIxMsMpy4bG5bO/yC10dmd 0Du5KMeH2JOSykpeNMhAEOYY7pYxVv/kOZK9Cz7XaXnX6faC904c4XkVpMgcthz4yDBj nyOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=AiW10OsgeEcfqp89fNu83n/wDekrj0ObDJDzIAqkMkc=; b=BTuYqDTWERnD3hL8VHttSSaqeB1tbLb4jqqJ4omud68h0IeN2WOPneeNGoWTuZGZST vVOIECT5Gw0pbSMa4TVntDcI4+Yn0luaS83Rwo2NfKd33OFVa4ZUpijRkUEbQRb9ltUF GHX7bkaVNlT82zcVgWdDNINUv1JYy9Mp7l2Pzu4pZTlcosIzgUGkDCpjYDlqM5X3zcKP fOW7/Z4p6IVT9xXwyAV6cJ1JQtVS9YL9aC5QnU4O0mzxcMsyq9qtP5rSKltOHmifNi/G 7nFjIs8UlIuKWbuWX4QgL8oEZWG/mPrb31MN0YoYYsxAOsiX8ISabGBD8NSYzFXjZshF GPdw== X-Gm-Message-State: AOAM533t14IyrWOxXPzj7THz+CVuEexuB3+MbA5GpPpcSbBFHPGrb941 t9SsgtyDzectSU8hTfz+EM575RBxLXnmeH9ZIns= X-Google-Smtp-Source: ABdhPJy6WNEita1dLLKC87/Prr1uu7rs7Ah9y7PT/UJjbYlyDkDXqq/DvCbqdOtUs1nAbkCTkH8Vs1MYdunKtrAgPJ8= X-Received: by 2002:a05:6402:2891:: with SMTP id eg17mr47265377edb.33.1641270119035; Mon, 03 Jan 2022 20:21:59 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: "Inoue,Hiroshi" Date: Tue, 4 Jan 2022 13:21:49 +0900 Message-ID: Subject: Re: BUG: CC_send_query_append incomplete error handling To: Nicolae Vartolomei Cc: "pgsql-odbc@postgresql.org" Content-Type: multipart/alternative; boundary="0000000000006cf88105d4b9fa62" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000006cf88105d4b9fa62 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Nicolae, Sorry for the late reply. I would take care of this issue. regards, Hhiroshi Inoue 2022=E5=B9=B41=E6=9C=881=E6=97=A5(=E5=9C=9F) 20:32 Nicolae Vartolomei : > 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=3D3 rec=3D1 > environ.c[ER_ReturnError]202: entering status =3D 1, msg =3D #no > connection to the server... > environ.c[ER_ReturnError]259: szSqlState =3D 'HY000',len=3D58, > szError=3D'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 =3D 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/86040a15d80c9371639ee0fac28= 79da749612a3f/programs/odbc-bridge/ODBCConnectionFactory.h#L76-L96 > (nanodbc- > > >unixODBC->psqlodbc) > > --- > > nvartolomei > > > --0000000000006cf88105d4b9fa62 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Nicolae,

Sorry for the late reply.
I would take care of this issue.

regards,=
Hhiroshi Inoue

2022=E5=B9=B41=E6=9C=881=E6=97=A5(=E5=9C=9F)= 20:32 Nicolae Vartolomei <nv@cloud= flare.com>:
Hi,

I noticed a bug where psqlodbc doesn't handle an underlying error corre= ctly.

The interaction is roughly as follows:

Initiate a connection, run few queries, run `ss -K dport postgresql` to kil= l the
psqlodbc connection to postgres, then run another query.

The expected behaviour is for query to fail and to return an error like &qu= ot;08S01".

Instead, the following error is generated:
```
pgapi30.c[PGAPI_GetDiagRec]43: entering type=3D3 rec=3D1
environ.c[ER_ReturnError]202: entering status =3D 1, msg =3D #no
connection to the server...
environ.c[ER_ReturnError]259:=C2=A0 =C2=A0 =C2=A0 szSqlState =3D 'HY000= ',len=3D58,
szError=3D'no connection to the server
```

This makes it a bit tricky to properly handle the exception on the client.<= br>
I'm browsing through this code base for the first time,
and the incomplete error handling seems to be around these lines in connect= ion.c

```
if (!PQsendQuery(self->pqconn, query_buf.data))
{
char *errmsg =3D 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=C2=A0 this code
https://github.com/ClickHou= se/ClickHouse/blob/86040a15d80c9371639ee0fac2879da749612a3f/programs/odbc-b= ridge/ODBCConnectionFactory.h#L76-L96
(nanodbc-
>unixODBC->psqlodbc)

---

nvartolomei


--0000000000006cf88105d4b9fa62--