jackc/pgx · error

write to connection for cancellation: %w

Error message

write to connection for cancellation: %w

What it means

Error "write to connection for cancellation: %w" thrown in jackc/pgx.

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)

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: /data/errors/b791abf6ad7a75fb.json. Report an issue: GitHub.