sfackler / rust-postgres

Native PostgreSQL driver for the Rust programming language
Apache License 2.0
3.43k stars 436 forks source link

unexpected EOF on client connection #1034

Closed rudolphfroger closed 1 year ago

rudolphfroger commented 1 year ago

When a process terminates then tokio-postgres closes the connection. PostgreSQL reports: DEBUG: unexpected EOF on client connection.

I started looking into this because I got a similar message from pgbouncer. When directly connecting to PostgreSQL and with DEBUG1 logging turned on this behaviour can also be seen, so it's not related to pgbouncer.

When I add drop(client); as a final statement in the main function then the unexpected EOF often goes away, but sometimes it needs a bit more time after dropping the client (e.g. by adding a very short sleep). My hypothesis is that the message used to signal the server that the connection will be closed hasn't been (fully) sent when the process terminates. Maybe something additional needs to happen when a connection is dropped to ensure the last bytes have also been sent.

The issue can be reproduced using the first example from the tokio-postges docs (top example from tokio-postgres/src/lib.rs). I've tried connecting using a unix domain sockets and TCP and I could reproduce this in both cases. I've configured PostgreSQL logging with log_min_messages = 'DEBUG1' to be able to see this message. I'm using PostgreSQL 14 and FreeBSD 13.1.

sfackler commented 1 year ago

If you want to avoid that message you'll need to wait for the connection future to complete after dropping the client.

rudolphfroger commented 1 year ago

Ah, of course. Thanks!