jackc/pgx · error
write to connection for cancellation
Error message
write to connection for cancellation: %w
What it means
The cancel request payload could not be written to the cancellation connection — the write syscall failed, likely because the connection closed or was interrupted. The cancel signal was not delivered.
Solutions
- Check if the server closed the connection (wrapped error detail)
- Retry the cancel request via Context cancellation instead
- Verify network stability between client and server
- Treat as best-effort; the query may complete normally
Defensive patterns
Strategy: try-catch
When it happens
Trigger: Thrown at pgconn/pgconn.go:1159 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04).
Data as JSON: /api/errors/b791abf6ad7a75fb.
Report an issue: GitHub.
Appendix: source
Thrown at pgconn/pgconn.go:1159
tlsCancelConn = tls.Client(cancelConn, pgConn.tlsConfig)
} else {
tlsCancelConn, err = startTLS(cancelConn, pgConn.tlsConfig)
if err != nil {
return fmt.Errorf("tls error on cancel connection: %w", err)
}
}
cancelConn = tlsCancelConn
defer cancelConn.Close()
}
buf := make([]byte, 12+len(pgConn.secretKey))
binary.BigEndian.PutUint32(buf[0:4], uint32(len(buf)))
binary.BigEndian.PutUint32(buf[4:8], 80877102)
binary.BigEndian.PutUint32(buf[8:12], pgConn.pid)
copy(buf[12:], pgConn.secretKey)
if _, err := cancelConn.Write(buf); err != nil {
return fmt.Errorf("write to connection for cancellation: %w", err)
}
// Wait for the cancel request to be acknowledged by the server.
// It copies the behavior of the libpq: https://github.com/postgres/postgres/blob/REL_16_0/src/interfaces/libpq/fe-connect.c#L4946-L4960
_, _ = cancelConn.Read(buf)
return nil
}
// WaitForNotification waits for a LISTEN/NOTIFY message to be received. It returns an error if a notification was not
// received.
func (pgConn *PgConn) WaitForNotification(ctx context.Context) error {
if err := pgConn.lock(); err != nil {
return err
}
defer pgConn.unlock()
if ctx != context.Background() {View on GitHub (pinned to ec1a0befd2)