Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Throw ObjectDisposedException on assignment to Disposed NpgsqlCommand - #6048

#6048
Merged
NinoFloris merged 1 commit into
npgsql:mainnpgsql/npgsql:mainfrom
kevbot18:allowDisposedkevbot18/npgsql:allowDisposedCopy head branch name to clipboard
Nov 20, 2025
Merged

Throw ObjectDisposedException on assignment to Disposed NpgsqlCommand#6048
NinoFloris merged 1 commit into
npgsql:mainnpgsql/npgsql:mainfrom
kevbot18:allowDisposedkevbot18/npgsql:allowDisposedCopy head branch name to clipboard

Conversation

@kevbot18

@kevbot18 kevbot18 commented Mar 14, 2025

Copy link
Copy Markdown
Contributor

Checks for Disposed state when assigning Connection or CommandText on NpgsqlCommand and throws an ObjectDisposedException if disposed, instead of raising an open data reader exception.

@kevbot18
kevbot18 requested review from roji and vonzshik as code owners March 14, 2025 01:27
@vonzshik

Copy link
Copy Markdown
Contributor

But why? Almost always you're not supposed to interact with a disposed object, especially changing a field there.

@kevbot18

kevbot18 commented Mar 14, 2025

Copy link
Copy Markdown
Contributor Author

I agree it doesn't really make much sense to modify after disposed. The primary reason I'm proposing the change is so interaction with a disposed connection don't throw InvalidOperationException: An open data reader exists for this command. Originally I planned to throw an ObjectDisposedException but realized other libraries didn't and figured I'd follow that.

Would you prefer it differentiate between Disposed and InProgress and throw ObjectDisposedException or InvalidOperationException accordingly?

@NinoFloris

Copy link
Copy Markdown
Member

I would rather we just throw an ObjectDisposedException here. I'd be happy to merge that change.

@NinoFloris

Copy link
Copy Markdown
Member

@kevbot18 friendly ping. Could you adapt your changes to make the code throw an ObjectDisposedException?

@kevbot18 kevbot18 changed the title Do not throw open data reader exception for CommandState.Disposed Throw ObjectDisposedException on assignment to Disposed NpgsqlCommand Nov 20, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves error handling when properties are assigned to a disposed NpgsqlCommand by throwing an ObjectDisposedException instead of the previous "An open data reader exists" error message.

Key Changes:

  • Added explicit disposal state checks in CommandText and Connection property setters
  • Throws ObjectDisposedException when attempting to assign to these properties on a disposed command

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Npgsql/NpgsqlCommand.cs Updated CommandText setter to use switch statement and Connection setter to use switch expression, both checking for CommandState.Disposed
test/Npgsql.Tests/CommandTests.cs Added test to verify ObjectDisposedException is thrown when assigning to disposed command properties

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Npgsql/NpgsqlCommand.cs
@NinoFloris
NinoFloris merged commit b8b7f34 into npgsql:main Nov 20, 2025
19 checks passed
@kevbot18
kevbot18 deleted the allowDisposed branch November 23, 2025 03:29
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.

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.