Skip to content

Connection timeout doesn't work for chunk-encoded requests #4402

@isobit

Description

@isobit

Original issue was #1422, but I was able to reproduce in 2.18.4.

Expected Result

Chunk-encoded requests timeout after the specified timeout if the server does not respond.

Actual Result

Request holds connection open and never times out.

Reproduction Steps

First, run a test server that accepts POST requests and waits 10 seconds before responding:

from http.server import BaseHTTPRequestHandler, HTTPServer
import time

class Handler(BaseHTTPRequestHandler):
    def do_POST(self):
        time.sleep(10)
        self.send_response(200)
        self.end_headers()

if __name__ == '__main__':
    httpd = HTTPServer(('', 8000), Handler)
    print('Listening on :8000')
    httpd.serve_forever()

Then, in another REPL, verify timeout works for non-chunk-encoded requests:

import requests
requests.post('http://localhost:8000', timeout=2.0, data='hello'.encode('utf-8'))

A timeout exception should be thrown.

Then, verify timeout does not work for chunk-encoded requests:

def gen():
    yield 'hello'.encode('utf-8')
requests.post('http://localhost:8000', timeout=2.0, data=gen())

A <Response [200]> will be returned, indicating that the timeout did not work properly (a timeout exception should have been thrown instead).

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.6"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.6.3"
  },
  "platform": {
    "release": "4.4.0-43-Microsoft",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.18.4"
  },
  "system_ssl": {
    "version": "1000207f"
  },
  "urllib3": {
    "version": "1.22"
  },
  "using_pyopenssl": false
}

Workaround

Insert the following on line 454 of requests/adapters.py:

low_conn.timeout = timeout.connect_timeout

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions