Thread: 08P01: unexpected EOF on client connection

08P01: unexpected EOF on client connection

From
Tomas Vondra
Date:
Hi,

I've run into some strange troubles with a quite simple web application
that stores data in a PostgreSQL database. It is a simple image gallery,
displaying multiple thumbnails (stored in a bytea column) on a single
page generated by PHP. The problem is some of the images are not displayed.

I've found that the postgresql.log contains these messages (many of them):

...
LOG:  08006: could not receive data from client: Connection reset by peer
LOCATION:  pq_recvbuf, pqcomm.c:738
LOG:  08P01: unexpected EOF on client connection
LOCATION:  SocketBackend, postgres.c:307
LOG:  08P01: unexpected EOF on client connection
LOCATION:  SocketBackend, postgres.c:307
LOG:  08006: could not receive data from client: Connection reset by peer
LOCATION:  pq_recvbuf, pqcomm.c:738
LOG:  08006: could not receive data from client: Connection reset by peer
LOCATION:  pq_recvbuf, pqcomm.c:738
...

Due to this, there are no data written to the output, so the browser
receives only "HTTP/1.x 200 OK" but does not receive the image itself
(and thus cannot display it).

The script fetching the data from a database is something this:

$conn = pg_connect(...);
$res = pg_query("SELECT mime, thumbnail_data FROM images WHERE filename
= ....");
$row = pg_fetch_assoc($row);
header('Content-Type: ' . $row['mime']);
echo pg_unescape_bytea($row['thumbnail_data']);

And it fails for some reason with the log messages listed above.

This behavior does not depend on a browser (I've tried that on multiple
ones), PHP version or Apache2 version (I've several more version). I've
noticed this on PostgreSQL 8.2.12 and 8.3.6 (on Linux x86).

It obviously does not depend on a number of images, as my testing
database contains only a few tiny images, and the "empty" images are
different each time (so there are no corrupted data or something like that).

Do you have any idea why this happens?

regards
Tomas

Re: 08P01: unexpected EOF on client connection

From
Craig Ringer
Date:
Tomas Vondra wrote:

> $conn = pg_connect(...);
> $res = pg_query("SELECT mime, thumbnail_data FROM images WHERE filename
> = ....");
> $row = pg_fetch_assoc($row);
> header('Content-Type: ' . $row['mime']);
> echo pg_unescape_bytea($row['thumbnail_data']);

PHP?

Try running your script in a PHP debugger (from the command line or, if
your server supports it, while hosted) and see at what point exactly
things go pear shaped. Check your web server error log for any client
library or PHP errors, too.

Even just adding a bunch of logging statements into your script then
looking at your web server error log might help you a little.

--
Craig Ringer

Re: 08P01: unexpected EOF on client connection

From
Tomas Vondra
Date:
Hi,

> Tomas Vondra wrote:
>
>> $conn = pg_connect(...);
>> $res = pg_query("SELECT mime, thumbnail_data FROM images WHERE filename
>> = ....");
>> $row = pg_fetch_assoc($row);
>> header('Content-Type: ' . $row['mime']);
>> echo pg_unescape_bytea($row['thumbnail_data']);
>
> PHP?
>
> Try running your script in a PHP debugger (from the command line or, if
> your server supports it, while hosted) and see at what point exactly
> things go pear shaped. Check your web server error log for any client
> library or PHP errors, too.

Running in a PHP debugger would be quite difficult in this case, as the
script is executed concurrently - once for each image displayed on the
page and requested by the browser.

> Even just adding a bunch of logging statements into your script then
> looking at your web server error log might help you a little.

I've added some logging into the script - basically there are 4
interesting places, and for a page with 69 images, the results are these
(number of executions that reach the point):

1) before connect : 69 cases
2) after connect / before select : 32 cases
3) after select : 24 cases
4) at the end : 24 cases

So only about 30% of the images is actually drawn. About 50% of the
executions fail right at the connection, and another 8 fail when
fetching the data :-(

Anyway it seems there's something wrong with my development box. I've
tried to run this app on a production server and everything works fine
there (all the images displayed, no 'unexpected EOF' errors etc).

The production box is not exactly the same, but in general the versions
of PHP, PostgreSQL and Apache do match. OK, this is probably the
punishment for living on the edge as I use gentoo (instead of debian,
installed on the server). But how to find out the rotten piece?

regards
Tomas

Re: 08P01: unexpected EOF on client connection

From
Jasen Betts
Date:
On 2009-05-01, Tomas Vondra <tv@fuzzy.cz> wrote:
> Hi,
>
> I've run into some strange troubles with a quite simple web application
> that stores data in a PostgreSQL database. It is a simple image gallery,
> displaying multiple thumbnails (stored in a bytea column) on a single
> page generated by PHP. The problem is some of the images are not displayed.
>
> $conn = pg_connect(...);
> $res = pg_query("SELECT mime, thumbnail_data FROM images WHERE filename
>= ....");
> $row = pg_fetch_assoc($row);
> header('Content-Type: ' . $row['mime']);
> echo pg_unescape_bytea($row['thumbnail_data']);

This is cut-n-paste from a working site (pg 7.4, PHP 4.3.9)

 $res=pg_query("select * from images where id='$id' ;");
 $row=pg_fetch_assoc($res);
 header("content-type: $row[type]");
 echo pg_unescape_bytea($row['data']);

the date on the file is NOV 19 2006.
wasn't I a crap programmer back then :)