IO.for_fd(conn.socket) may cause EBADF on the connection
When IO.for_fd(conn.socket) is used for instance to IO.select() on the database connection, it might happen that the allocated IO gets GC'ed and the sockets gets closed therefore. A subsequent SQL statement will then fail. Contrary the PG::Connection might be closed before the IO gets GC'ed. This is the cause for the spec errors in: https://travis-ci.org/ged/ruby-pg/builds/4386997
On Ruby-1.9.3 the IO should generally be build with IO#autoclose=false. But on Ruby-1.8.7 there is no autoclose. The only way seems to store the IO object somewhere to avoid GC'ing, there.
How should we deal with this issue? My ideas are:
We could run the test cases with IO.for_fd only on 1.9.3 with autoclose=false and describe the issue in conn.socket.
We could add a new method conn.io that returns a IO object, that doesn't get GC'ed before PQfinish. Maybe someday someone will figure out how we could bind a socket file descriptor to an IO on Windows, so that it gets usable there, too.
We could ensure that the IO is not GC'ed within the specs.