Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Engine.release method to release connection in any way #756

Merged
merged 4 commits into from
Dec 7, 2020
Merged

Conversation

Pliner
Copy link
Member

@Pliner Pliner commented Dec 5, 2020

The goal of the PR is to actualise the fix made in #558 by @vmagamedov, but with slightly reworked tests.

Fixes #332, #665, #666 as well.

It looks like both exceptions(psycopg2.InterfaceError and psycopg2.OperationalError) could be raised, see https://github.com/aio-libs/aiopg/pull/756/checks?check_run_id=1503163697, initially only the first one was expected in the tests.

Tests are written using intermediate tcp proxy to allow disrupt a connection and cover two cases:

  1. When a connection has disrupted before a request(SELECT 1; for instance) is executed, so rollback could not be executed.
  2. When a connection has disrupted before begin is executed.

@Pliner Pliner force-pushed the fix-332 branch 3 times, most recently from 4ec2b5a to 65ab4da Compare December 5, 2020 10:12
@codecov
Copy link

codecov bot commented Dec 5, 2020

Codecov Report

Merging #756 (ce9aaa3) into master (532a092) will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #756      +/-   ##
==========================================
- Coverage   92.77%   92.76%   -0.02%     
==========================================
  Files          13       13              
  Lines        1565     1562       -3     
  Branches      180      179       -1     
==========================================
- Hits         1452     1449       -3     
  Misses         81       81              
  Partials       32       32              
Impacted Files Coverage Δ
aiopg/sa/engine.py 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 532a092...ce9aaa3. Read the comment docs.

@Pliner Pliner marked this pull request as ready for review December 5, 2020 10:34
@Pliner Pliner requested a review from asvetlov December 5, 2020 10:34
@Pliner Pliner mentioned this pull request Dec 5, 2020
@Pliner Pliner changed the title Fixes Engine.release method to release connection in any way Fix Engine.release method to release connection in any way Dec 5, 2020
with pytest.raises(
(psycopg2.InterfaceError, psycopg2.OperationalError)
):
with pytest.warns(ResourceWarning, match='Invalid transaction status'):

Choose a reason for hiding this comment

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

Is it ok to spam users with both warning and exception? Maybe it is better just to raise an exception? Connection reset is not a user's fault, so I think that this warning doesn't help them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for noticing this.

I'm a newbie in the aiopg codebase and don't know the reasons why these warnings are reported in such a way.

@asvetlov Could you share your opinion please?

Copy link
Member

Choose a reason for hiding this comment

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

My initial thought was preventing a programming error and signal this situation early.
I agree that the connection releasing can be done for network reasons, there no need for extra checks.
I see two options:

  1. Drop exception in 'release' as PR does, https://github.com/aio-libs/aiopg/blob/master/aiopg/pool.py#L242-L248 warning also can be dropped. It can lead to reports about unfinished transactions by next acquired connection.
    Satisfactory but confusing.
  2. Raise an exception only if the underlying socket is not closed but swallow the transaction status for disconnected peers. It prevents false positives from network errors but can report a programming error fast. Connection for the unclosed transaction should be dropped immediately, the connection is in an undefined incorrect state and cannot be recovered. Perhaps a warning generated in the pool can be replaced with a strict error also.

The 1) is easier, 2) is harder to implement but maybe more correct.
I don't want to decide what to do; I have no time to contribute anyway. Please choose what you want.

Copy link
Member Author

@Pliner Pliner Dec 7, 2020

Choose a reason for hiding this comment

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

Okay, got you, thanks.

I've opened a new issue to fix it a bit later, because it looks like merging of the PR with incorrect warnings in quite rare case is much better than suffering from broken pool.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense

with pytest.raises(sa.InvalidRequestError):
engine.release(conn)
with pytest.warns(ResourceWarning, match='Invalid transaction status'):
await engine.release(conn)

Choose a reason for hiding this comment

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

Maybe it is better to raise an exception here instead of a warning. Looks like a user's bug to release a connection in the middle of transaction.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added this comment to the issue as well.

while self.connections:
writer = self.connections.pop()
writer.close()
if hasattr(writer, "wait_closed"):
Copy link
Member Author

Choose a reason for hiding this comment

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

In python 3.6 there is no such a method

@Pliner Pliner merged commit 9c79fdd into master Dec 7, 2020
@Pliner Pliner deleted the fix-332 branch December 7, 2020 14:12
sanderegg added a commit to sanderegg/osparc-simcore that referenced this pull request Apr 12, 2021
pcrespov pushed a commit to ITISFoundation/osparc-simcore that referenced this pull request Apr 13, 2021
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.

Error when canceling during transaction begin.
3 participants