Skip to content

Commit

Permalink
Copy the keys from framework session state (#559)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
twsouthwick authored Dec 11, 2024
1 parent 70011af commit 91322f1
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ public bool IsAbandoned
}
}

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

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

Expand Down

0 comments on commit 91322f1

Please sign in to comment.