{"record":{"id":"85be6ba2b9bf3315","repo":"dotnet/yarp","slug":"session-affinity-is-disabled-for-cluster-cluster-85be6b","errorCode":null,"errorMessage":"Session affinity is disabled for cluster {cluster.ClusterId}.","messagePattern":"Session affinity is disabled for cluster (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ReverseProxy/SessionAffinity/BaseHashCookieSessionAffinityPolicy.cs","lineNumber":58,"sourceCode":"            return;\n        }\n\n        // Affinity key is set on the response only if it's a new affinity.\n        if (!context.Items.ContainsKey(AffinityKeyId))\n        {\n            var affinityKey = GetDestinationHash(destination);\n            var affinityCookieOptions = AffinityHelpers.CreateCookieOptions(config.Cookie, context.Request.IsHttps, _timeProvider);\n\n            // CodeQL [SM02373] - Whether CookieOptions.Secure is used depends on YARP configuration, and session affinity may be used in non-HTTPS setups. Hash-based affinity policies do not intend to provide privacy protection. See https://learn.microsoft.com/aspnet/core/fundamentals/servers/yarp/session-affinity#key-protection.\n            context.Response.Cookies.Append(config.AffinityKeyName, affinityKey, affinityCookieOptions);\n        }\n    }\n\n    public AffinityResult FindAffinitizedDestinations(HttpContext context, ClusterState cluster, SessionAffinityConfig config, IReadOnlyList<DestinationState> destinations)\n    {\n        if (!config.Enabled.GetValueOrDefault())\n        {\n            throw new InvalidOperationException($\"Session affinity is disabled for cluster {cluster.ClusterId}.\");\n        }\n\n        var affinityHash = context.Request.Cookies[config.AffinityKeyName];\n        if (affinityHash is null)\n        {\n            return new(null, AffinityStatus.AffinityKeyNotSet);\n        }\n\n        foreach (var d in destinations)\n        {\n            var hashValue = GetDestinationHash(d);\n\n            if (affinityHash == hashValue)\n            {\n                context.Items[AffinityKeyId] = affinityHash;\n                return new(d, AffinityStatus.OK);\n            }\n        }","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/dotnet/yarp/blob/bd11867bee7df522e7fd3effb08a9c85fd616908/src/ReverseProxy/SessionAffinity/BaseHashCookieSessionAffinityPolicy.cs#L40-L76","documentation":"Thrown by BaseHashCookieSessionAffinityPolicy.FindAffinitizedDestinations when `config.Enabled` is not true. Read-side counterpart to error 48 for hash-cookie policies; includes `cluster.ClusterId` in the message for diagnostics.","triggerScenarios":"FindAffinitizedDestinations called on a cluster whose SessionAffinityConfig.Enabled is false/null, with a hash-cookie policy in use.","commonSituations":"Hash-cookie affinity policy registered globally; some clusters do not enable affinity. Cluster config edited and `Enabled` lost.","solutions":["Enable affinity on the cluster: `SessionAffinity.Enabled = true`.","Ensure affinity lookup only runs against enabled clusters.","Verify the ClusterState/SessionAffinityConfig pair is the intended one."],"exampleFix":"// before\nSessionAffinity = new SessionAffinityConfig { Policy = \"HashCookie\", AffinityKeyName = \"Yarp.Affinity\" }\n// after\nSessionAffinity = new SessionAffinityConfig { Enabled = true, Policy = \"HashCookie\", AffinityKeyName = \"Yarp.Affinity\" }","handlingStrategy":"validation","validationCode":"if (!config.Enabled.GetValueOrDefault())\n    throw new InvalidOperationException(\"Enable SessionAffinity before FindAffinitizedDestinations.\");","typeGuard":"static bool AffinityEnabled(SessionAffinityConfig? config) =>\n    config is not null && config.Enabled.GetValueOrDefault();","tryCatchPattern":null,"preventionTips":["Enable affinity on clusters that opt into hash-cookie policies.","Scope the affinity lookup to enabled clusters.","Run startup config validation for affinity consistency."],"tags":["session-affinity","config","yarp"],"backgroundTag":null,"analyzedSha":"bd11867bee7df522e7fd3effb08a9c85fd616908","analyzedAt":"2026-08-13T21:29:49.359Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}