Skip to content

fix #1355: retrieve DbSet to List before iteration to to prevent npgsql crash#1356

Merged
aharwood2 merged 4 commits into
devfrom
1355-seed-readers-pgsql
Jun 11, 2026
Merged

fix #1355: retrieve DbSet to List before iteration to to prevent npgsql crash#1356
aharwood2 merged 4 commits into
devfrom
1355-seed-readers-pgsql

Conversation

@sjmf

@sjmf sjmf commented May 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #1355.

Iterating directly over a DbSet while issuing further queries on the same DbContext inside the loop fails on PostgreSQL.

Npgsql rejects the second command because a reader is already open on the connection. SQLite silently tolerates this; SQL Server likely just requires MultipleActiveResultSets=True in the connection string to avoid the same issue.

The behaviour was initially visible in the database seeding routines, but the pattern also exists elsewhere.

Fixed by retrieving the outer collections into lists with .ToList() before entering the affected loops in SeedOwnedSkillsForPeople, SeedCompetencyAssessments, and SeedTimesheets. No behaviour change on any provider: just fixes the npgsql crash.

I've also included fixes for TimesheetService and GetSkillsForProject, which use the same pattern.

Tested by running:

  • SEED_DUMMY_DATA=TRUE DB_PROVIDER=sqlite dotnet run: seeding completes without error (no regression)
  • SEED_DUMMY_DATA=TRUE DB_PROVIDER=postgresql dotnet run: seeding completes without error

…vent `NpgsqlOperationInProgressException` on PostgreSQL.

Iterating directly over DbSet inside a loop that issues further queries
on the same context fails on PostgreSQL (no MARS support). Fixed by
calling ToList() before the affected foreach loops in
SeedOwnedSkillsForPeople, SeedCompetencyAssessments, and SeedTimesheets.
@sjmf

sjmf commented May 30, 2026

Copy link
Copy Markdown
Contributor Author

I would suggest renaming the Check Repository Ruleset / check-ruleset (pull_request) check. It implies a code failing, however, to my understanding it is just complaining because no-one has reviewed the PR yet.

Naming the actions pipeline stage something like "PR is reviewed by a CoP member" could help?

@sjmf

sjmf commented Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

Pause on this until I can add the fix from #1355 (comment) please!

@sjmf

sjmf commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

^ that's the fix for that, but I'm going to review the rest of the codebase for this pattern too.

@sjmf

sjmf commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

Done. This PR is ready for review!

@sjmf sjmf added the database 🔥 Relates to the DB itself or modification of the schema label Jun 2, 2026
@sjmf sjmf changed the title fix: retrieve contexts to Lists in SeedHelper before iteration to pre… fix #1355: retrieve contexts to Lists before iteration to to prevent npgsql crash Jun 2, 2026
@sjmf sjmf changed the title fix #1355: retrieve contexts to Lists before iteration to to prevent npgsql crash fix #1355: retrieve DbSet to List before iteration to to prevent npgsql crash Jun 5, 2026
@aharwood2 aharwood2 added this to the v1.14.1 milestone Jun 8, 2026
@sjmf sjmf requested review from gdonval and philiptharrison June 9, 2026 10:00
@aharwood2 aharwood2 merged commit 5fc9a07 into dev Jun 11, 2026
12 checks passed
@aharwood2 aharwood2 deleted the 1355-seed-readers-pgsql branch June 11, 2026 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

database 🔥 Relates to the DB itself or modification of the schema

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: PostgreSQL provider does not support MARS (Multiple Active Result Sets): NpgsqlOperationInProgressException

2 participants