@@ -2130,6 +2130,62 @@ func TestConnCopyFromPrecanceled(t *testing.T) {
21302130 ensureConnValid (t , pgConn )
21312131}
21322132
2133+ // https://github.com/jackc/pgx/issues/2364
2134+ func TestConnCopyFromConnectionTerminated (t * testing.T ) {
2135+ t .Parallel ()
2136+
2137+ ctx , cancel := context .WithTimeout (context .Background (), 120 * time .Second )
2138+ defer cancel ()
2139+
2140+ pgConn , err := pgconn .Connect (ctx , os .Getenv ("PGX_TEST_DATABASE" ))
2141+ require .NoError (t , err )
2142+ defer closeConn (t , pgConn )
2143+
2144+ closerConn , err := pgconn .Connect (ctx , os .Getenv ("PGX_TEST_DATABASE" ))
2145+ require .NoError (t , err )
2146+ defer closeConn (t , closerConn )
2147+
2148+ _ , err = pgConn .Exec (ctx , `create temporary table foo(
2149+ a int4,
2150+ b varchar
2151+ )` ).ReadAll ()
2152+ require .NoError (t , err )
2153+
2154+ r , w := io .Pipe ()
2155+ go func () {
2156+ for i := 0 ; i < 5_000 ; i ++ {
2157+ a := strconv .Itoa (i )
2158+ b := "foo " + a + " bar"
2159+ _ , err := w .Write ([]byte (fmt .Sprintf ("%s,\" %s\" \n " , a , b )))
2160+ if err != nil {
2161+ return
2162+ }
2163+ time .Sleep (time .Millisecond )
2164+ }
2165+ }()
2166+
2167+ time .AfterFunc (500 * time .Millisecond , func () {
2168+ err := closerConn .ExecParams (ctx , "select pg_terminate_backend($1)" , [][]byte {[]byte (fmt .Sprintf ("%d" , pgConn .PID ()))}, nil , nil , nil ).Read ().Err
2169+ require .NoError (t , err )
2170+ })
2171+
2172+ copySql := "COPY foo FROM STDIN WITH (FORMAT csv)"
2173+ if pgConn .ParameterStatus ("crdb_version" ) != "" {
2174+ copySql = "COPY foo FROM STDIN WITH CSV"
2175+ }
2176+ ct , err := pgConn .CopyFrom (ctx , r , copySql )
2177+ assert .Equal (t , int64 (0 ), ct .RowsAffected ())
2178+ assert .Error (t , err )
2179+ fmt .Println (err )
2180+
2181+ assert .True (t , pgConn .IsClosed ())
2182+ select {
2183+ case <- pgConn .CleanupDone ():
2184+ case <- time .After (5 * time .Second ):
2185+ t .Fatal ("Connection cleanup exceeded maximum time" )
2186+ }
2187+ }
2188+
21332189func TestConnCopyFromGzipReader (t * testing.T ) {
21342190 t .Parallel ()
21352191
0 commit comments