Skip to content

fix: dispose enumerators in collection mappings - #1041

Merged
DocSvartz merged 1 commit into
MapsterMapper:developmentfrom
ZainnQureshii:fix/dispose-collection-enumerators
Nov 24, 2026
Merged

DocSvartz merged 1 commit into
MapsterMapper:developmentfrom
ZainnQureshii:fix/dispose-collection-enumerators

Conversation

@ZainnQureshii

Copy link
Copy Markdown

Summary

Addresses #667

Mapster.Tool's generated Map and MapToTarget extensions use the same collection expression builder as runtime mappings (src/Mapster.Tool/Generators.cs:673, src/Mapster.Tool/Generators.cs:685, src/Mapster/TypeAdapterConfig.cs:335). The old loop advanced the enumerator without disposing it. For example, when Adapt<IEnumerable, List<int>>() throws while converting an item, a non-generic iterator's finally can be skipped, leaving a reader or cursor open.

The loop now disposes the enumerator in a finally block. It calls Dispose on a non-null IEnumerator<T> directly. For a non-generic IEnumerator, it uses as IDisposable and disposes the result when the cast succeeds, as C# foreach does (src/Mapster/Utils/ExpressionEx.cs:333). Regenerate existing extensions with the updated tool to pick up the fix.

Collection, array, and multidimensional array mappings reach the shared builder through ForLoop when they enumerate. Dictionary merges call ForEach directly (src/Mapster/Adapters/CollectionAdapter.cs:213, src/Mapster/Adapters/ArrayAdapter.cs:124, src/Mapster/Adapters/MultiDimensionalArrayAdapter.cs:151, src/Mapster/Adapters/DictionaryAdapter.cs:158). The indexed paths acquire no enumerator for their loop. MapsterQueryable hands its provider's enumerator to the caller, which owns its lifetime (src/Mapster.EFCore/MapsterQueryable.cs:22, src/Mapster.EFCore/MapsterQueryable.cs:32).

The checked-in course sample endpoint uses a LINQ projection. The benchmark’s generated enumeration loop reads the in-memory List<Address> assigned to WorkAddresses (src/Sample.CodeGen/Controllers/SchoolController.cs:30, src/Benchmark/TestAdaptHelper.cs:77, src/Benchmark/CustomerMapper.g.cs:72).

Verification

  • dotnet test src/Mapster.Tests/Mapster.Tests.csproj --framework net10.0 --filter FullyQualifiedName~MapToListDisposes --verbosity minimal ran both disposal regressions. The non-generic case failed against the generic-only patch because its cleanup flag stayed false. Both passed with this patch and observed the expected FormatException (src/Mapster.Tests/WhenMappingCollections.cs:252, src/Mapster.Tests/WhenMappingCollections.cs:273).
  • dotnet build ./src/Mapster.sln and dotnet test ./src/Mapster.sln passed on development, with 1,335 tests passed and 18 skipped across .NET 8, 9, and 10.
  • Generated, compiled, and ran both extensions using Mapster.Tool's extension command. Generic and non-generic disposable enumerators recorded one Dispose call on successful mapping and on conversion failure. Both extensions also mapped an ArrayList with a non-disposable enumerator.
  • Scoped dotnet format style and dotnet format whitespace each exited 2. Neither reported a diagnostic on a changed line. git diff --check passed.

Prepared with AI assistance (Codex and Claude Code) and reviewed with GitHub Copilot.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DocSvartz DocSvartz linked an issue Nov 24, 2026 that may be closed by this pull request
@DocSvartz
DocSvartz merged commit df2406d into MapsterMapper:development Nov 24, 2026
2 checks passed
@DocSvartz

Copy link
Copy Markdown
Contributor

@ZainnQureshii Thanks!
Added in v10.0.15-pre05

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generic IEnumerator not disposed

2 participants