Skip to content

Commit 91322f1

Browse files
authored
Copy the keys from framework session state (#559)
It appears that some session providers on the framework side may not be able to iterate over the keys while grabbing items as it may throw an InvalidOperationException with the error: Collection was modified after the enumerator was instantiated. Even though it appears that this should not happen, we'll copy the keys over before grabbing items from the collection so this can't happen.
1 parent 70011af commit 91322f1

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices/SessionState/Serialization/HttpSessionStateBaseWrapper.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,17 @@ public bool IsAbandoned
5454
}
5555
}
5656

57-
public IEnumerable<string> Keys => State.Keys.Cast<string>();
57+
public IEnumerable<string> Keys
58+
{
59+
get
60+
{
61+
// If the underlying storage mechanism is SessionStateItemCollection then there can be issues around the enumerator causing side effects.
62+
// There are precautions in the implementation to prevent this, but we have seen an exception around this in practice (see dotnet/systemweb-adapters#556).
63+
// However, there is no simple way to check if this is the case, so we'll preemptively create a copy.
64+
// See https://referencesource.microsoft.com/#System.Web/State/SessionStateItemCollection.cs,435 for the warnings in the implementation.
65+
return [.. State.Keys.Cast<string>()];
66+
}
67+
}
5868

5969
public void Clear() => State.Clear();
6070

0 commit comments

Comments
 (0)