PQconsumeinput() may close the fd

Started by Vasily Kulikovover 3 years ago4 messagesgeneral
Jump to latest
#1Vasily Kulikov
segoon@yandex-team.ru

<div>Hello,</div><div> </div><div>There is an event loop in my program (libev) and I do PQconsumeInput() if there is something ready in the PG socket fd. The problem is that sometimes PQconsumeInput() may close the connection and I learn it by checking for PQsocket() afterwards only. AFAICS, there is no way to learn it beforehand. The core issue is that I have to deregister socket fd in the event loop, but the fd is already closed. It is already not good because we're still working with already invalid fd, but worse, in multithreaded env other thread might open a socket with the same fd number and mess all the things.</div><div> </div><div>So, what to do in this case? Should I use other API that may not close fd? Or only select/poll API may be used with every time fd registration, but not epoll? Or maybe I'm wrong in some other way?</div><div> </div><div>Thanks in advance.</div>

#2Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Vasily Kulikov (#1)
Re: PQconsumeinput() may close the fd

On Fri, 2022-10-21 at 07:27 +0300, Vasily Kulikov wrote:

The problem is that sometimes PQconsumeInput() may close the connection

The function does nothing of the kind. Is there an error response from
the function? Perhaps something else, like a timeout in a network component,
closes the connections afterwards.

Can you find any corresponding messages in the PostgreSQL server log?

Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Laurenz Albe (#2)
Re: PQconsumeinput() may close the fd

Laurenz Albe <laurenz.albe@cybertec.at> writes:

On Fri, 2022-10-21 at 07:27 +0300, Vasily Kulikov wrote:

The problem is that sometimes PQconsumeInput() may close the connection

The function does nothing of the kind.

No, he's right: if we detect EOF then we'll close the socket.
However, not doing so wouldn't be an improvement. I believe the
socket would still be read-ready so you'd have an infinite loop.

What I'd suggest doing is checking for PQstatus(conn) == CONNECTION_BAD,
or else directly rechecking PQsocket() each time, rather than assuming
the socket is still there.

regards, tom lane

#4Vasily Kulikov
segoon@yandex-team.ru
In reply to: Tom Lane (#3)
Re: PQconsumeinput() may close the fd

<div>21.10.2022, 17:40, "Tom Lane" &lt;tgl@sss.pgh.pa.us&gt;:</div><blockquote><p>Laurenz Albe &lt;<a href="mailto:laurenz.albe@cybertec.at" rel="noopener noreferrer">laurenz.albe@cybertec.at</a>&gt; writes:</p><blockquote> On Fri, 2022-10-21 at 07:27 +0300, Vasily Kulikov wrote:<blockquote> The problem is that sometimes PQconsumeInput() may close the connection</blockquote></blockquote><p>What I'd suggest doing is checking for PQstatus(conn) == CONNECTION_BAD,<br />or else directly rechecking PQsocket() each time, rather than assuming<br />the socket is still there.</p></blockquote><div>I've chosen this one. Thanks for the help!</div>