Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gHW44-0008Jp-35 for psycopg@arkaria.postgresql.org; Tue, 30 Oct 2018 15:36:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gHW42-00032X-78 for psycopg@arkaria.postgresql.org; Tue, 30 Oct 2018 15:36:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gHW41-00032D-Vv for psycopg@lists.postgresql.org; Tue, 30 Oct 2018 15:36:18 +0000 Received: from mail-lf1-x134.google.com ([2a00:1450:4864:20::134]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gHW3z-0002Ii-T6 for psycopg@postgresql.org; Tue, 30 Oct 2018 15:36:17 +0000 Received: by mail-lf1-x134.google.com with SMTP id n18so883487lfh.6 for ; Tue, 30 Oct 2018 08:36:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Kc/OC8aNzDQ+jy9N7PggaIBjgZ+2Nm1ZT3yFKemXw9g=; b=sY8xTx4jyPjGryCm1LFKNlIBZlf5wjQQ5lFJ3QJu7BjKQuIcisIMAivNV94CdIrSGp TkbyY8gn7H0zYpXOPtGlyCc3NBeHLpTgEjslnN0SHZ3x0DVP3w1tTpyGIbic+YquIro8 t8jhDtpx9tzysfXKyT3wHf8TmrBlOgJFZ89m2+zCFzSX1w8F0uK/ZSjvzXbdA5G4X+Ny kUSPCzMEwVPS+JHplljvrjAGbeBTtNOG2aa001vHvMZRzJzSEmBu1g52Sqv7wEv1bmT+ WaF7xxYWjUgpfCsC91+1Dt8ZO1bX1ITKOkzSmyATdMWeWt1hX2MxKWGqqnPtrkfuVZ3d VYrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Kc/OC8aNzDQ+jy9N7PggaIBjgZ+2Nm1ZT3yFKemXw9g=; b=qm4X1oPKa4cpkkkODq3pGqF6NECHlpuIi9fJ7DZ8lyXIDV80++u5BGk4eHP8djlGg+ s5QlZKPJioL2dG3rn1EdZtnKvemun+T9xRJSwN/Yy+ztxtwk9/vR9zaxzdBRGQGIStfa o+3+m+dx1/Jk7Jx7cDTLCh5uJuz1fdl6PoLidp16NsOUxTfnIpG7LPRhszwVtWF3/DU6 CDKxW1txUWAXPgUr1OJNiGsa5SVLs+KTKkTvXy6ogYUrxwN/M79sF+nge7JSUXiKCBAg kwN0XE659VhF05vExwPqGTTo8EUT7V+v6CRUVuxv9HaFWRK22L2wm4l3DSJs2VCaYX9I 1y3Q== X-Gm-Message-State: AGRZ1gII8DGJ/nNVy6A2EIo2F9KRR/YcFxbErF6iLwMiuAvqUtZPC7qg Cs8awEuRNDnUrMQBTm/rdMPGUDru4NGClQq14wM= X-Google-Smtp-Source: AJdET5eVPV9RXHF3dd5qUqA6uYBmeKsm8SXEtSPZOy5VYdpJ+jCpdDsPuzbnDpWbrwPqFqkyulYS92j+cwc9ha/zj/o= X-Received: by 2002:a19:2b54:: with SMTP id r81mr2305097lfr.34.1540913773398; Tue, 30 Oct 2018 08:36:13 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Daniele Varrazzo Date: Tue, 30 Oct 2018 15:36:01 +0000 Message-ID: Subject: Re: psycopg2 conn.poll() hangs when used with sshtunnel To: Akshay Joshi Cc: psycopg@postgresql.org Content-Type: multipart/alternative; boundary="00000000000017d91b057973f291" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --00000000000017d91b057973f291 Content-Type: text/plain; charset="UTF-8" On Tue, Oct 30, 2018 at 1:55 PM Akshay Joshi wrote: > What changes should I made to fix this? Or it should be fixed in > psycopg2 itself. > Actually, I have another observation. I was surprised to see that the whole test suite passed running the connection through an ssh tunnel: there are definitely failing queries in the test suite. So I made other tests and it seems the bad condition only happens using the `sshtunnel` module: If I open a tunnel manually with something like: ssh -L 36421:localhost:5432 -N localhost and point your script to port 36421 everything works ok. Conversely, I don't seem able to run the test suite through the tunnel open by the sshtunnel module. It doesn't hang, but the tunnel breaks in a test with the following reported on the sshtunnel side: 2018-10-30 15:24:25,550| ERROR | Socket exception: Bad file descriptor (9) 2018-10-30 15:24:25,551| ERROR | Could not establish connection from ('127.0.0.1', 33743) to remote side of the tunnel and the following tests fail to run as the connection is broken. Con-conversely, the patch I had in mind to fix #801, which very brutally is: ``` @@ -1136,6 +1136,13 @@ pq_get_last_result(connectionObject *conn) || status == PGRES_COPY_IN) { break; } + if (PQisBusy(conn->pgconn)) { + /* This happens connecting through ssl tunnel + * TODO: just kill this function. The loop should happen within + * the async/green machinery. */ + Dprintf("pq_get_last_result: we are busy"); + break; + } } return result; ``` doesn't work, and failing queries leave the connection in an inconsistent state. So, wrapping up, I think there is something you can do on your side, checking why the sshtunnel module behaves differently from a normal ssh tunnel, and if you use the latter (or configure sshtunnel to behave so) you shouldn't hit the problem. On our side I don't think we can fix #801 with a quick band aid, and we should rather do #802, but as things stands now I don't trust sshtunnel to do the right thing, and have less of an urgency to do so. Please let us know if you understand what is the difference between the module and the tunnel via `ssh -L`, thank you! -- Daniele > --00000000000017d91b057973f291 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Tue, Oct 30, 2018 at 1:55 PM Akshay= Joshi <akshay.joshi@en= terprisedb.com> wrote:
=C2=A0
=C2=A0= =C2=A0 What changes should I made to fix this? Or it should be fixed in ps= ycopg2 itself.

Actually, I have= another observation. I was surprised to see that the whole test suite pass= ed running the connection through an ssh tunnel: there are definitely faili= ng=C2=A0queries in the test suite. So I made other tests and it seems the b= ad condition only happens using the `sshtunnel` module: If I open a tunnel = manually with something like:

=C2=A0 =C2=A0=C2=A0s= sh -L 36421:localhost:5432 -N localhost

and point = your script to port 36421 everything works ok.

Conversely, I don't seem able to run the test suite through= the tunnel open by the sshtunnel module. It doesn't hang, but the tunn= el breaks in a test with the following reported on the sshtunnel side:

=C2=A0 =C2=A0 2018-10-30 15:24:25,550| ERROR=C2= =A0 =C2=A0| Socket exception: Bad file descriptor (9)
=C2=A0 =C2= =A0 2018-10-30 15:24:25,551| ERROR=C2=A0 =C2=A0| Could not establish connec= tion from ('127.0.0.1', 33743) to remote side of the tunnel

and the following tests fail to run as the connecti= on is broken.


Con-conversely, the p= atch I had in mind to fix #801, which very brutally is:

```
@@ -1136,6 +1136,13 @@ pq_get_last_result(connecti= onObject *conn)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0|| status =3D=3D PGRES_COPY_IN) {
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0break;
=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0}
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (PQisBusy(conn->pgcon= n)) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 /* This happens = connecting through ssl tunnel
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0* TODO: just kill this function. The loop should happen withi= n
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0* the async/gr= een machinery. */
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Dpri= ntf("pq_get_last_result: we are busy");
+=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 }=
=C2=A0 =C2=A0 =C2=A0}
=C2=A0
=C2=A0 =C2=A0 = =C2=A0return result;
```

doesn'= ;t work, and failing queries leave the connection in an inconsistent state.=


So, wrapping up, I think there is = something you can do on your side, checking why the sshtunnel module behave= s differently from a normal ssh tunnel, and if you use the latter (or confi= gure sshtunnel to behave so) you shouldn't hit the problem. On our side= I don't think we can fix #801 with a quick band aid, and we should rat= her do #802, but as things stands now I don't trust sshtunnel to do the= right thing, and have less of an urgency to do so.

Please let us know if you understand what is the difference between the m= odule and the tunnel via `ssh -L`, thank you!

-- D= aniele
--00000000000017d91b057973f291--