• Tom Lane's avatar
    Try to read data from the socket in pqSendSome's write_failed paths. · 7247e243
    Tom Lane authored
    Even when we've concluded that we have a hard write failure on the
    socket, we should continue to try to read data.  This gives us an
    opportunity to collect any final error message that the backend might
    have sent before closing the connection; moreover it is the job of
    pqReadData not pqSendSome to close the socket once EOF is detected.
    
    Due to an oversight in 1f39a1c0, pqSendSome failed to try to collect
    data in the case where we'd already set write_failed.  The problem was
    masked for ordinary query operations (which really only make one write
    attempt anyway), but COPY to the server would continue to send data
    indefinitely after a mid-COPY connection loss.
    
    Hence, add pqReadData calls into the paths where pqSendSome drops data
    because of write_failed.  If we've lost the connection, this will
    eventually result in closing the socket and setting CONNECTION_BAD,
    which will cause PQputline and siblings to report failure, allowing
    the application to terminate the COPY sooner.  (Basically this restores
    what happened before 1f39a1c0.)
    
    There are related issues that this does not solve; for example, if the
    backend sends an error but doesn't drop the connection, we did and
    still will keep pumping COPY data as long as the application sends it.
    Fixing that will require application-visible behavior changes though,
    and anyway it's an ancient behavior that we've had few complaints about.
    For now I'm just trying to fix the regression from 1f39a1c0.
    
    Per a complaint from Andres Freund.  Back-patch into v12 where
    1f39a1c0 came in.
    
    Discussion: https://postgr.es/m/20200603201242.ofvm4jztpqytwfye@alap3.anarazel.de
    7247e243
fe-misc.c 32.1 KB