Skip to content

Fixed reading by reader previous query messages - #3126

Merged
vonzshik merged 11 commits into
npgsql:mainfrom
vonzshik:ReaderGarbageReadingFix
Nov 15, 2020
Merged

vonzshik merged 11 commits into
npgsql:mainfrom
vonzshik:ReaderGarbageReadingFix

Conversation

@vonzshik

@vonzshik vonzshik commented Sep 6, 2020 •

Copy link
Copy Markdown
Contributor

Closes #2913
Closes #3289
Closes #2964

The idea is to move reader cleanup from a connector to the reader.

@roji
roji changed the base branch from dev to main September 17, 2020 14:33
@vonzshik
vonzshik force-pushed the ReaderGarbageReadingFix branch from ae45f2b to 9de9876 Compare October 4, 2020 09:18
@vonzshik
vonzshik force-pushed the ReaderGarbageReadingFix branch from 9de9876 to 2b42406 Compare October 26, 2020 18:15

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry for taking so long to review this - the approach looks great, see the comment on the Cleanup placement inside Read.

There's also the question of exceptions happening during sequential column reads - this wasn't handled before either, but we could handle it by cleaning up in GetFieldValueSequential as well.

Comment thread test/Npgsql.Tests/ReaderTests.cs Resolved
Comment thread test/Npgsql.Tests/ReaderTests.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
@vonzshik
vonzshik requested a review from roji November 9, 2020 15:29
roji
roji previously approved these changes Nov 9, 2020

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, but just want to make sure we still close the connection with CommandBehavior.CloseConnection when an error occurs.

Comment thread test/Npgsql.Tests/ReaderTests.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
@vonzshik

vonzshik commented Nov 9, 2020

Copy link
Copy Markdown
Contributor Author

There's also the question of exceptions happening during sequential column reads - this wasn't handled before either, but we could handle it by cleaning up in GetFieldValueSequential as well.

@roji not really - if there was an exception while reading sequentially, it will break the connection (which also closes the reader).

@roji

roji commented Nov 9, 2020

Copy link
Copy Markdown
Member

(we could even a test that confirms the close-on-error behavior, using mocking)

@vonzshik

Copy link
Copy Markdown
Contributor Author

As discussed offline, the current idea is to do the same, as we've done for the transactions. That is, if the reader is closed outside of dispose, we unbind it from the connector.

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As discussed offline, the current idea is to do the same, as we've done for the transactions. That is, if the reader is closed outside of dispose, we unbind it from the connector.

@vonzshik, shouldn't we unbind the reader only if the connection is closed with an undisposed reader, like we do with transactions? The way the code currently is, if a user just calls Close on a reader, we immediately unbind even if they do Dispose right afterwards (before closing the connection).

I'd expect BindIfNecessary to only get called from EndBindingScope and Reset, just like with transactions, no?

Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
@roji
roji dismissed their stale review November 13, 2020 14:14

Stale

@vonzshik
vonzshik requested a review from roji November 13, 2020 20:40
@vonzshik

Copy link
Copy Markdown
Contributor Author

@roji changed a little, now it should be a little bit more sane

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @vonzshik, the recycling looks much better now.

I thought about this thoroughly again, and noticed that we now call Cleanup on any error in Read/NextResult, whereas before we used to call it only on PostgresException (note also that we don't call Cleanup if an exception occurs in other reader methods, e.g. GetFieldValue (sequential), ColumnStream).

I thought about this some more, and I'm no longer sure why things are this way - rather than call Cleanup immediately when an exception occurs, why not defer and do it as usual only when the user closes/disposes the reader? This would make the flow simpler and wouldn't create an inconsistency between Read/NextResult and GetFieldValue/ColumnStream, etc.

If this works and I'm not missing anything, we'd also need to call Cleanup in NpgsqlCommand.ExecuteReader when an exception occurs (where we now call only EndUserAction).

What do you think?

Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
@vonzshik
vonzshik force-pushed the ReaderGarbageReadingFix branch 2 times, most recently from 603150d to d078adb Compare November 14, 2020 18:23
@vonzshik
vonzshik requested a review from roji November 14, 2020 19:04

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks really close to mergeable @vonzshik, and in general much better than before.

Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM - though see the last outstanding comment about refactoring cleanup flow in NpgsqlCommand.

@vonzshik
vonzshik force-pushed the ReaderGarbageReadingFix branch from c43e316 to 7cc58d6 Compare November 15, 2020 09:05
@vonzshik
vonzshik force-pushed the ReaderGarbageReadingFix branch from 7cc58d6 to 34f4774 Compare November 15, 2020 09:11
@vonzshik
vonzshik requested a review from roji November 15, 2020 09:47
@vonzshik

Copy link
Copy Markdown
Contributor Author

@roji seems to be stable now, just one final check

Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
@vonzshik
vonzshik merged commit 7bb2617 into npgsql:main Nov 15, 2020
@vonzshik
vonzshik deleted the ReaderGarbageReadingFix branch November 15, 2020 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants