{"id":"b791abf6ad7a75fb","repo":"jackc/pgx","slug":"write-to-connection-for-cancellation-w","errorCode":null,"errorMessage":"write to connection for cancellation: %w","messagePattern":"write to connection for cancellation: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pgconn/pgconn.go","lineNumber":1159,"sourceCode":"\t\t\ttlsCancelConn = tls.Client(cancelConn, pgConn.tlsConfig)\n\t\t} else {\n\t\t\ttlsCancelConn, err = startTLS(cancelConn, pgConn.tlsConfig)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"tls error on cancel connection: %w\", err)\n\t\t\t}\n\t\t}\n\t\tcancelConn = tlsCancelConn\n\t\tdefer cancelConn.Close()\n\t}\n\n\tbuf := make([]byte, 12+len(pgConn.secretKey))\n\tbinary.BigEndian.PutUint32(buf[0:4], uint32(len(buf)))\n\tbinary.BigEndian.PutUint32(buf[4:8], 80877102)\n\tbinary.BigEndian.PutUint32(buf[8:12], pgConn.pid)\n\tcopy(buf[12:], pgConn.secretKey)\n\n\tif _, err := cancelConn.Write(buf); err != nil {\n\t\treturn fmt.Errorf(\"write to connection for cancellation: %w\", err)\n\t}\n\n\t// Wait for the cancel request to be acknowledged by the server.\n\t// It copies the behavior of the libpq: https://github.com/postgres/postgres/blob/REL_16_0/src/interfaces/libpq/fe-connect.c#L4946-L4960\n\t_, _ = cancelConn.Read(buf)\n\n\treturn nil\n}\n\n// WaitForNotification waits for a LISTEN/NOTIFY message to be received. It returns an error if a notification was not\n// received.\nfunc (pgConn *PgConn) WaitForNotification(ctx context.Context) error {\n\tif err := pgConn.lock(); err != nil {\n\t\treturn err\n\t}\n\tdefer pgConn.unlock()\n\n\tif ctx != context.Background() {","sourceCodeStart":1141,"sourceCodeEnd":1177,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/pgconn/pgconn.go#L1141-L1177","documentation":"Error \"write to connection for cancellation: %w\" thrown in jackc/pgx.","triggerScenarios":"Thrown at pgconn/pgconn.go:1159 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","schemaVersion":2}