Repository navigation
Exception when used locally in 4.0.0. Kestrel related? #401
Description
Activity
- changed the title
[-]Exception when used locally[/-][+]Exception when used locally in 4.0.0[/+]on Jun 9, 2026 - changed the title
[-]Exception when used locally in 4.0.0[/-][+]Exception when used locally in 4.0.0. Kestrel related?[/+]on Jun 9, 2026 Are you using a custom
IRequestParser, customOnParseCommandsAsync, or any middleware that branches/re-executes the pipeline for image requests?The default parser creates a new
CommandCollectionper request, and normal ASP.NET Core middleware execution should be sequential for a single request. The exception implies that theCommandCollectionchanged betweenContains("autoorient")andInsert(...), or that the same collection/context is being reused or mutated concurrently.HttpContextitself is not thread-safe, but this does not by itself suggest Kestrel is invoking our middleware concurrently for the same request.Very good analysis and description!
Based on your reply I investigated further and I think I found the issue. In our code we use the
PresetOnlyQueryCollectionRequestParserthat I contributed. I looked at its code and found this:
ImageSharp.Web/src/ImageSharp.Web/Commands/PresetOnlyQueryCollectionRequestParser.cs
Line 44 in 592083f
if (requestedPreset is not null && this.presets.TryGetValue(requestedPreset, out CommandCollection? collection)) - At construction the
PresetOnlyQueryCollectionRequestParserparses the presets from configuration, into aDictionary<string, CommandCollection> - In
ParseRequestCommandsit returns the matchedCommandCollectionfrom thatDictionary. - Therefore the returned
CommandCollectionis shared among different requests that use the same preset. - It should instead return a copy / clone of the
CommandCollection.
Reacted by James Jackson-South- At construction the
I tested my theory by copying the
PresetOnlyQueryCollectionRequestParserand adjusting the code to:if (requestedPreset is not null && presets.TryGetValue(requestedPreset, out var collection)) { CommandCollection copy = []; // I didn't see a better way to create a copy, so I used a loop foreach (var pair in collection) copy.Add(pair.Key, pair.Value!); return copy; }
This indeed fixes the issue!
Reacted by James Jackson-SouthI think we should create a
DeepClonemethod.I think we should create a
DeepClonemethod.Makes sense.
Btw, I'm currently not able to contribute a PR for this issue.
Prerequisites
DEBUGandRELEASEmodeImageSharp.Web version
4.0.0
Other Six Labors packages and versions
ImageSharp 4.0.0
Environment (Operating system, version and so on)
Windows 11, Kestrel
.NET version
10.0
Description
Since 4.0.0, when running ImageSharp.Web locally or in the CI/CD pipelines the following exception is thrown intermittently. On our production system (Azure App Service, Windows) it is not thrown at all.
Based on the stacktrace, it seems like the following line is causing the issue:
ImageSharp.Web/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs
Line 43 in 592083f
I have no clue why, could it be a concurrency issue?
Steps to Reproduce
Unknown at this point.
Images
Not applicable.