]> granicus.if.org Git - postgresql/commit
Fix potential deadlock with libpq non-blocking mode.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Mon, 23 Feb 2015 11:32:34 +0000 (13:32 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Mon, 23 Feb 2015 11:32:42 +0000 (13:32 +0200)
commitcdf813c59312298194b16eecc68aa50c9e2e1133
treeb4bd4ac9ac4267bb55125912bd7c22a014baa0dc
parentf389b6e0a7637de014e5ea1adbcfa50108bb4e58
Fix potential deadlock with libpq non-blocking mode.

If libpq output buffer is full, pqSendSome() function tries to drain any
incoming data. This avoids deadlock, if the server e.g. sends a lot of
NOTICE messages, and blocks until we read them. However, pqSendSome() only
did that in blocking mode. In non-blocking mode, the deadlock could still
happen.

To fix, take a two-pronged approach:

1. Change the documentation to instruct that when PQflush() returns 1, you
should wait for both read- and write-ready, and call PQconsumeInput() if it
becomes read-ready. That fixes the deadlock, but applications are not going
to change overnight.

2. In pqSendSome(), drain the input buffer before returning 1. This
alleviates the problem for applications that only wait for write-ready. In
particular, a slow but steady stream of NOTICE messages during COPY FROM
STDIN will no longer cause a deadlock. The risk remains that the server
attempts to send a large burst of data and fills its output buffer, and at
the same time the client also sends enough data to fill its output buffer.
The application will deadlock if it goes to sleep, waiting for the socket
to become write-ready, before the server's data arrives. In practice,
NOTICE messages and such that the server might be sending are usually
short, so it's highly unlikely that the server would fill its output buffer
so quickly.

Backpatch to all supported versions.
doc/src/sgml/libpq.sgml
src/interfaces/libpq/fe-misc.c