Skip to content

Handle async cancelled error explicitly - #811

Closed
T-256 wants to merge 24 commits into
encode:masterfrom
T-256:HandleCancelled
Closed

Handle async cancelled error explicitly#811
T-256 wants to merge 24 commits into
encode:masterfrom
T-256:HandleCancelled

Conversation

@T-256

@T-256T-256 commented Sep 19, 2023

Copy link
Copy Markdown
Contributor

Summary

Discussed in #805

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@TungliesTunglies left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We'd need to change in both httpcore/_async and httpcore/_sync and ensure them match.

@T-256

Copy link
Copy Markdown
ContributorAuthor

We'd need to change in both httpcore/_async and httpcore/_sync and ensure them match.

As what's said in #806, I think this could be ignored in unasync.py to avoid extra unuseful load. (Task behaviors not defined on threads.)
But still I'm waiting to get proper response from team to choose final implementation for sync part.

@karpetrosyan

Copy link
Copy Markdown
Contributor

Is this solution respects such cases?

because the user may catch the keyboardinterrupt in his code and continue to use httpcore.ConnectionPool.

@T-256

T-256 commented Sep 20, 2023

Copy link
Copy Markdown
ContributorAuthor

Is this solution respects such cases?

No, #812 will respect.

Actually this PR can expose why current cleanup system is not well implemented.

@karpetrosyan

Copy link
Copy Markdown
Contributor

No, #812 will respect.

Do we want to marge this PR when we know it will break programs that handle BaseExceptions like KeyboardInterrupt?

@T-256

Copy link
Copy Markdown
ContributorAuthor

Do we want to marge this PR when we know it will break programs that handle BaseExceptions like KeyboardInterrupt?

Yes. because still some of cleanups doesn't handle BaseException in codebase:

  • exceptExceptionasexc:
    self._connect_failed=True
    raiseexc
    elifnotself._connection.is_available():

  • exceptExceptionasexc:
    # If we get a network error we should:
    #
    # 1. Save the exception and just raise it immediately on any future reads.
    # (For example, this means that a single read timeout or disconnect will
    # immediately close all pending streams. Without requiring multiple
    # sequential timeouts.)
    # 2. Mark the connection as errored, so that we don't accept any other
    # incoming requests.
    self._read_exception=exc
    self._connection_error=True
    raiseexc

  • exceptExceptionasexc: # pragma: nocover
    # If we get a network error we should:
    #
    # 1. Save the exception and just raise it immediately on any future write.
    # (For example, this means that a single write timeout or disconnect will
    # immediately close all pending streams. Without requiring multiple
    # sequential timeouts.)
    # 2. Mark the connection as errored, so that we don't accept any other
    # incoming requests.
    self._write_exception=exc
    self._connection_error=True
    raiseexc

  • exceptExceptionasexc:
    self._connect_failed=True
    raiseexc
    elifnotself._connection.is_available(): # pragma: nocover

  • exceptExceptionasexc: # pragma: nocover
    awaitself.aclose()
    raiseexc
    returnAnyIOStream(ssl_stream)

  • exceptExceptionasexc: # pragma: nocover
    awaitself.aclose()
    raiseexc
    returnTrioStream(ssl_stream)

  • plus, same things in _sync part.

And also notice few BaseException was introduced in #726, so before that we were not supporting KeyboardInterrupt:

  • First, Before: no error handling, After: BaseException handling.
  • Second, Before: Exception handling, After: BaseException handling.

@T-256
T-256 marked this pull request as ready for review September 20, 2023 14:29
@T-256

Copy link
Copy Markdown
ContributorAuthor

Should we consider convert exception handlings in #811 (comment) to EXCEPTION_OR_CANCELLED?

Comment threadhttpcore/_synchronization.py Outdated
@stale

staleBot commented Apr 26, 2025

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stalestaleBot added the wontfix This will not be worked on label Apr 26, 2025
@stalestaleBot closed this Jun 27, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wontfixThis will not be worked on

Development

Successfully merging this pull request may close these issues.

4 participants

@T-256@karpetrosyan@Tunglies@lovelydinosaur