tornadoweb / tornadoweb/tornado

HTTPError creation raises an exception when connection is severed by the remote server during a long HTTPRequest

Open
#691 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

web
Dominant language
Python
Stars
22.2k
Forks
5.6k
Avg merge
3h 42m
Merged PRs (30d)
16

Description

When the remote server is shutdown before an HTTPRequest completes, the HTTPError exception fails to instantiate due to a TypeError in the string formatting of the error message. This prevents the real exception from being rethrown and passed in to the HTTPResponse object.

A non-integer code argument is passed at the exception instance creation. This prevents HTTPClient from creating the appropriate 5xx HTTPError to pass to the client for handling, as the compiler raises its own exception at trying to format a non-integer into an integer format place holder.

Apart from fixing the offending code that tries to create the HTTPError instance with non-integer code parameter (I couldn't actually track that one down to the source), I suggest to change the format string to use non-type specific formatting as per the mini string formatting language spec. This will at least prevent the runtime from choking at Exception creation.

Suggested fix:

In httpclient.py

class HTTPError(Exception):
    ....
    def __init__(self, code, message=None, response=None):
        self.code = code
        message = message or httputil.responses.get(code, "Unknown")
        self.response = response
        Exception.__init__(self, "HTTP %d: %s" % (self.code, message))

change

Exception.__init__(self, "HTTP %d: %s" % (self.code, message))

to

Exception.__init__(self, "HTTP {}: {}" % (self.code, message))

letting Python manage the types.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in httpclient.py with HTTPError.init and trace how HTTPClient creates it when a remote server disconnects during an HTTPRequest. Verify that the error can be instantiated with the received code and that the underlying exception is passed to HTTPResponse without another formatting failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, networking
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.