PQconsumeinput() may close the fd
<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>
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
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
<div>21.10.2022, 17:40, "Tom Lane" <tgl@sss.pgh.pa.us>:</div><blockquote><p>Laurenz Albe <<a href="mailto:laurenz.albe@cybertec.at" rel="noopener noreferrer">laurenz.albe@cybertec.at</a>> 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>