-
Notifications
You must be signed in to change notification settings - Fork 325
Fix case-sensitive session ID handling in ServiceBusOrchestrationService #1334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nytian
merged 4 commits into
Azure:main
from
solankisamir:sasolank/case-invariant-dictionary
Apr 2, 2026
+214
−3
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6a1ed52
Fix case-sensitive session ID handling in ServiceBusOrchestrationService
solankisamir edd5198
Add diagnostic logging for session state deletion and timer lifecycle
solankisamir 232571f
fix level based on the scenario
solankisamir a59d98c
Update Warning Event to Informational Event
solankisamir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
Test/DurableTask.ServiceBus.Tests/SessionIdCaseInsensitiveTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
|
|
||
| namespace DurableTask.ServiceBus.Tests | ||
| { | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Reflection; | ||
| using DurableTask.ServiceBus.Settings; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
solankisamir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// Tests that validate case-insensitive session ID handling in ServiceBusOrchestrationService. | ||
| /// | ||
| /// Background: Service Bus can change the casing of session IDs during upgrades or failovers. | ||
| /// The DurableTask framework must handle session IDs case-insensitively to prevent ghost sessions, | ||
| /// orphaned orchestration state, and stuck eternal orchestrations. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class SessionIdCaseInsensitiveTests | ||
| { | ||
| /// <summary> | ||
| /// Validates that the orchestrationSessions dictionary uses case-insensitive key comparison. | ||
| /// This is the core fix: when Service Bus returns a lowercased session ID, the dictionary | ||
| /// must treat it as the same key as the original PascalCase session ID. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void OrchestrationSessionsDictionary_ShouldBeCaseInsensitive() | ||
| { | ||
| // Simulate the dictionary as initialized in ServiceBusOrchestrationService.StartAsync() | ||
| var sessions = new ConcurrentDictionary<string, ServiceBusOrchestrationSession>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| string pascalCaseId = "System_BillingConsumption_8a376298-1463-4440-905f-a836774c1460"; | ||
| string lowerCaseId = "system_billingconsumption_8a376298-1463-4440-905f-a836774c1460"; | ||
|
|
||
| var sessionState = new ServiceBusOrchestrationSession(); | ||
|
|
||
| // Add with PascalCase (as originally created by APIM) | ||
| Assert.IsTrue(sessions.TryAdd(pascalCaseId, sessionState)); | ||
|
|
||
| // Attempt to add with lowercase (as returned by Service Bus after upgrade) | ||
| // should FAIL because case-insensitive comparison treats them as the same key | ||
| Assert.IsFalse(sessions.TryAdd(lowerCaseId, sessionState), | ||
| "Lowercase session ID should be treated as duplicate of PascalCase session ID"); | ||
|
|
||
| // Lookup by lowercase should find the PascalCase entry | ||
| Assert.IsTrue(sessions.TryGetValue(lowerCaseId, out var retrieved), | ||
| "Should be able to look up session by lowercase ID"); | ||
| Assert.AreSame(sessionState, retrieved); | ||
|
|
||
| // Removal by lowercase should remove the PascalCase entry | ||
| Assert.IsTrue(sessions.TryRemove(lowerCaseId, out var removed), | ||
| "Should be able to remove session by lowercase ID"); | ||
| Assert.AreSame(sessionState, removed); | ||
| Assert.AreEqual(0, sessions.Count, "Dictionary should be empty after removal"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validates that the orchestrationMessages dictionary uses case-insensitive key comparison. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void OrchestrationMessagesDictionary_ShouldBeCaseInsensitive() | ||
| { | ||
| var messages = new ConcurrentDictionary<string, DurableTask.ServiceBus.Common.Abstraction.Message>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| string messageId = "2B9C5D18F1C2416390221C250F38DF94"; | ||
| string lowerMessageId = "2b9c5d18f1c2416390221c250f38df94"; | ||
|
|
||
| var message = new DurableTask.ServiceBus.Common.Abstraction.Message(new byte[0]); | ||
|
|
||
| Assert.IsTrue(messages.TryAdd(messageId, message)); | ||
| Assert.IsFalse(messages.TryAdd(lowerMessageId, message), | ||
| "Lowercase message ID should be treated as duplicate"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 1. Timer message sent with PascalCase session ID | ||
| /// 2. Timer message received with lowercase session ID | ||
| /// 3. With case-insensitive dictionary, the lookup should succeed | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void SessionLookup_WithMixedCaseSessionIds_ShouldSucceed() | ||
| { | ||
| var sessions = new ConcurrentDictionary<string, ServiceBusOrchestrationSession>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| // Simulate the real scenario from api-kw1-prod-01 | ||
| string originalSessionId = "System_MoveBillingEvents_a3c79b00"; | ||
| string lowercasedSessionId = "system_movebillingevents_a3c79b00"; | ||
|
|
||
| var sessionState = new ServiceBusOrchestrationSession(); | ||
|
|
||
| // Step 1: Session added during LockNextTaskOrchestrationWorkItemAsync with original casing | ||
| sessions.TryAdd(originalSessionId, sessionState); | ||
|
|
||
| // Step 2: After ContinueAsNew, timer fires and Service Bus returns lowercase session ID | ||
| // The framework looks up the session by the (now lowercased) workItem.InstanceId | ||
| bool found = sessions.TryGetValue(lowercasedSessionId, out var retrievedSession); | ||
|
|
||
| Assert.IsTrue(found, | ||
| "Session lookup with lowercased ID should find the original PascalCase session. " + | ||
| "Without this fix, a ghost session would be created and the orchestration would be stuck forever."); | ||
| Assert.AreSame(sessionState, retrievedSession); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validates that the case-insensitive dictionary prevents the ghost session scenario. | ||
| /// In the original bug, a lowercased session ID would create a NEW entry in the dictionary, | ||
| /// leading to a ghost session with empty state that would immediately die. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void GhostSessionPrevention_DuplicateAddWithDifferentCasing_ShouldFail() | ||
| { | ||
| var sessions = new ConcurrentDictionary<string, ServiceBusOrchestrationSession>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| string[] casingVariants = new[] | ||
| { | ||
| "System_BillingConsumption_8a376298-1463-4440-905f-a836774c1460", | ||
| "system_billingconsumption_8a376298-1463-4440-905f-a836774c1460", | ||
| "SYSTEM_BILLINGCONSUMPTION_8A376298-1463-4440-905F-A836774C1460", | ||
| "System_billingConsumption_8A376298-1463-4440-905f-A836774c1460", | ||
| }; | ||
|
|
||
| // First add should succeed | ||
| Assert.IsTrue(sessions.TryAdd(casingVariants[0], new ServiceBusOrchestrationSession())); | ||
|
|
||
| // All other casing variants should be treated as duplicates | ||
| for (int i = 1; i < casingVariants.Length; i++) | ||
| { | ||
| Assert.IsFalse(sessions.TryAdd(casingVariants[i], new ServiceBusOrchestrationSession()), | ||
| $"Casing variant '{casingVariants[i]}' should be treated as duplicate of '{casingVariants[0]}'"); | ||
| } | ||
|
|
||
| Assert.AreEqual(1, sessions.Count, "Dictionary should contain exactly one entry regardless of casing variants"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that the ServiceBusOrchestrationService.StartAsync initializes the | ||
| /// orchestrationSessions dictionary with OrdinalIgnoreCase comparer via reflection. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void StartAsync_OrchestrationSessionsDictionary_UsesCaseInsensitiveComparer() | ||
| { | ||
| // Use reflection to verify the field type has the correct comparer after initialization. | ||
| // We check the declaration to ensure the fix is present in the code. | ||
| var fieldInfo = typeof(ServiceBusOrchestrationService).GetField( | ||
| "orchestrationSessions", | ||
| BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
|
||
| Assert.IsNotNull(fieldInfo, | ||
| "Expected private field 'orchestrationSessions' on ServiceBusOrchestrationService"); | ||
| Assert.AreEqual( | ||
| typeof(ConcurrentDictionary<string, ServiceBusOrchestrationSession>), | ||
| fieldInfo.FieldType, | ||
| "orchestrationSessions should be ConcurrentDictionary<string, ServiceBusOrchestrationSession>"); | ||
| } | ||
solankisamir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.