@@ -935,21 +935,30 @@ func (pgConn *PgConn) CancelRequest(ctx context.Context) error {
935
935
buf := make ([]byte , 16 )
936
936
binary .BigEndian .PutUint32 (buf [0 :4 ], 16 )
937
937
binary .BigEndian .PutUint32 (buf [4 :8 ], 80877102 )
938
- binary .BigEndian .PutUint32 (buf [8 :12 ], uint32 ( pgConn .pid ) )
939
- binary .BigEndian .PutUint32 (buf [12 :16 ], uint32 ( pgConn .secretKey ) )
940
- _ , err = cancelConn . Write ( buf )
941
- if err != nil {
942
- return err
938
+ binary .BigEndian .PutUint32 (buf [8 :12 ], pgConn .pid )
939
+ binary .BigEndian .PutUint32 (buf [12 :16 ], pgConn .secretKey )
940
+
941
+ if _ , err := cancelConn . Write ( buf ); err != nil {
942
+ return fmt . Errorf ( "write to connection for cancellation: %w" , err )
943
943
}
944
944
945
- _ , err = cancelConn .Read (buf )
946
- if err != io .EOF {
947
- return err
945
+ if _ , err := cancelConn .Read (buf ); err != nil {
946
+ if errors .Is (err , io .EOF ) || isConnectionClosedError (err ) {
947
+ return nil
948
+ }
949
+
950
+ return fmt .Errorf ("unexpected error while reading connection response: %w" , err )
948
951
}
949
952
950
953
return nil
951
954
}
952
955
956
+ // isConnectionClosedError checks the error message to identify
957
+ // the specific "forcibly closed" error that occurs on Windows.
958
+ func isConnectionClosedError (err error ) bool {
959
+ return strings .Contains (err .Error (), "An existing connection was forcibly closed by the remote host" )
960
+ }
961
+
953
962
// WaitForNotification waits for a LISTON/NOTIFY message to be received. It returns an error if a notification was not
954
963
// received.
955
964
func (pgConn * PgConn ) WaitForNotification (ctx context.Context ) error {
0 commit comments