Skip to content

urllib.request.urlopen leaks exceptions from socket and httplib.client #57945

Closed

Description

@jmoy
mannequin
BPO 13736
Nosy @orsenthil, @pitrou, @ezio-melotti, @merwok, @vadmium, @jmoy
Files
  • test_urllib_except.py: Test to show bug.
  • urllib_exception_leak.patch: Patch to fix issue urllib.request.urlopen leaks exceptions from socket and httplib.client #57945
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/orsenthil'
    closed_at = None
    created_at = <Date 2012-01-08.06:22:19.699>
    labels = ['type-bug', 'library']
    title = 'urllib.request.urlopen leaks exceptions from socket and httplib.client'
    updated_at = <Date 2015-11-16.22:14:15.896>
    user = 'https://github.com/jmoy'

    bugs.python.org fields:

    activity = <Date 2015-11-16.22:14:15.896>
    actor = 'martin.panter'
    assignee = 'orsenthil'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2012-01-08.06:22:19.699>
    creator = 'jmoy'
    dependencies = []
    files = ['24172', '24173']
    hgrepos = []
    issue_num = 13736
    keywords = ['patch']
    message_count = 7.0
    messages = ['150849', '150850', '151690', '151697', '151698', '152881', '254761']
    nosy_count = 6.0
    nosy_names = ['orsenthil', 'pitrou', 'ezio.melotti', 'eric.araujo', 'martin.panter', 'jmoy']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue13736'
    versions = ['Python 3.3']

    Linked PRs

    Activity

    1. jmoy commented on Jan 8, 2012

      jmoymannequin
      MannequinAuthor

      The documentation for urlopen says that it raises URLError on error.
      But there exist error conditions such as a socket timeout or a bad HTTP status line under which the exception from the underlying library leaks through urllib, thus breaking the promise in the documentation.

      I am attaching a test program that demonstrates this bug.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Jan 8, 2012
    3. jmoy commented on Jan 8, 2012

      jmoymannequin
      MannequinAuthor

      A patch to fix this issue. Catches exceptions from underlying libraries and reraises them as URLError.

      I put the class name of the underlying exception in the reason to make it more descriptive.

    4. added
      type-bugAn unexpected behavior, bug, or error
      on Jan 8, 2012
    5. self-assigned this
      on Jan 20, 2012
    6. pitrou commented on Jan 20, 2012

      @pitrou
      Member

      I think the general pattern of wrapping exceptions in other exceptions is rather unfortunate. It makes it harder to examine the original problem (for example the errno attribute) for no actual gain.

    7. jmoy commented on Jan 20, 2012

      jmoymannequin
      MannequinAuthor

      Antoine, could "raise ... from" be used here? Perhaps also using new subclasses of URLError to allow the exceptions to be caught with finer granularity. That way no information would be lost while at the same time not surprising clients who only catch URLError based on the documentation.

      At least one exception which I feel must be wrapped is socket.timeout. Since we allow a timeout argument to urlopen, it doesn't make sense for the fact of the timeout to be announced by an exception from another library.

    8. pitrou commented on Jan 20, 2012

      @pitrou
      Member

      Antoine, could "raise ... from" be used here?

      That's a possible compromise indeed. It's up to Senthil to decide
      anyway.

      Perhaps also using new subclasses of URLError to allow the exceptions
      to be caught with finer granularity. That way no information would be
      lost while at the same time not surprising clients who only catch
      URLError based on the documentation.

      I agree there is a problem with the documentation announcing that all
      exceptions will be wrapped. Honestly I would like it better if that
      guarantee were dropped. In the meantime I'm not sure what the best
      course of action is.

      At least one exception which I feel must be wrapped is socket.timeout.
      Since we allow a timeout argument to urlopen, it doesn't make sense
      for the fact of the timeout to be announced by an exception from
      another library.

      You still may be interested to know that the request failed because of a
      timeout rather than (say) an authentication failure, no?

    9. orsenthil commented on Feb 8, 2012

      @orsenthil
      Member

      I have stumbled upon a wrong impression as well by reading the docs, but usually in the code, I tend to catch the specific Exceptions, like timeout instead or URLError when it is known. I saw some libraries following similar pattern too. But that could be changed, if promise in the docs that URLError exception is raised is corrected.

      I think, the course of action for this bug could be.

      1. raise ... from .. for the appropriate Exception in 3.3 and appropriate doc changes.
      2. Doc changes in 2.7,3.2 which say indicate the possibility of other exceptions besides URLError.

      Having any important information in the msg part of the Exception is a bad idea, because it is seldom relied upon and can be changed anytime.

      Shall come out with a patch.

    10. vadmium commented on Nov 16, 2015

      @vadmium
      Member

      There is another patch in duplicate bpo-8823, which has simpler exception wrapping code, but does not add wrapping of reply exceptions.

      Personally, I wouldn’t endorse any new wrapping HTTP client exceptions, and even if wrapping more socket OS errors would be consistent, it could be a backwards compatibility problem. I think the best way forward is documentation: bpo-25633, bpo-22797.

    11. transferred this issue fromon Apr 10, 2022
    12. moved this from Todo to In Progress in Urllib issueson Oct 28, 2026
    13. orsenthil commented on Oct 28, 2026

      @orsenthil
      Member

      Addressing this as a docs issue, which seems most reasonable way to address this - #160830

    14. added a commit that references this issue on Oct 28, 2026
    15. moved this from In Progress to Done in Urllib issueson Oct 28, 2026
    16. added 2 commits that reference this issue on Oct 28, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions