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

Connection._on_close() not being called if Connection.closing is externally cancelled #79

Open
wbenny opened this issue Mar 10, 2020 · 0 comments

Comments

@wbenny
Copy link
Contributor

wbenny commented Mar 10, 2020

  • Create a consumer connection
  • Create a task T1 with await connection.closing
  • When T1 gets externally cancelled (not by close() method, but e.g. when Ctrl+C is pressed during asyncio.run), the _on_close() method is never called.

I can call this method manually, like this:

async def _worker():
    try:
        await connection.closing
    except asyncio.CancelledError:
        connection._on_close()

But I don't think calling private methods should be "the best solution".

I thought I could get around this by calling await connection.close() (which results in calling _on_close()) in the CancelledError handler, however, there's a check for is_closed right at the top (and this check is also present in the __closer() callback.

    async def close(self, exc=asyncio.CancelledError()):
        if self.is_closed:
            return

... because is_closed returns true, the _on_close() will never get called.

My proposed solution would be to add_done_callback on the closing future, that calls _on_close, but this would also require to do a better check in the callbacks methods (instead of just is_closed).

I hope my explanation wasn't too much confusing, hit me back if you'd like me to clarify something.

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

No branches or pull requests

1 participant