The Fix
Upgrade to version 0.17.5 or later.
Based on closed Kludex/uvicorn issue #1262 · PR/commit linked
@@ -1,10 +1,14 @@
import contextlib
import logging
+import socket
+import threading
+import time
import socket
import threading as th
from time import sleep
import uvicorn
########## HELPERS ##########
def receive_all(sock):
chunks = []
while True:
chunk = sock.recv(1024)
if not chunk:
break
chunks.append(chunk)
return b''.join(chunks)
async def stub_app(scope, receive, send):
event = await receive()
d = b'whatever'
await send({
'type': 'http.response.start',
'status': 200,
'headers': [
[b'Content-Length', str(len(d)).encode()],
[b'Content-Type', b'text/plain'],
]
})
await send({'type': 'http.response.body', 'body': d, 'more_body': False})
#############################
def send_fragmented_req(path):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', 5000))
d = (
f'GET {path} HTTP/1.1\r\n'
'Host: localhost\r\n'
'Connection: close\r\n\r\n'
).encode()
split = len(path) // 2
# send first part
sock.sendall(d[:split])
sleep(0.01) # important, ~100% error, without this the chances of reproducing the error are pretty low
# send second part
sock.sendall(d[split:])
# read response
resp = receive_all(sock)
sock.shutdown(socket.SHUT_RDWR)
sock.close()
return resp
def test():
t = th.Thread(target=lambda: uvicorn.run(stub_app, host='127.0.0.1', port=5000, http='httptools'))
t.daemon = True
t.start()
sleep(1) # wait for unicorn to start
path = '/?param=' + 'q' * 10
print(send_fragmented_req(path).decode())
if __name__ == '__main__':
test()
# Console output:
# INFO: Started server process [748603]
# INFO: Waiting for application startup.
# INFO: ASGI 'lifespan' protocol appears unsupported.
# INFO: Application startup complete.
# INFO: Uvicorn running on http://127.0.0.1:5000 (Press CTRL+C to quit)
# WARNING: Invalid HTTP request received.
# Traceback (most recent call last):
# File "httptools/parser/parser.pyx", line 258, in httptools.parser.parser.cb_on_url
# File "/_/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 193, in on_url
# parsed_url = httptools.parse_url(url)
# File "httptools/parser/url_parser.pyx", line 105, in httptools.parser.url_parser.parse_url
# httptools.parser.errors.HttpParserInvalidURLError: invalid url b'am=qqqqqqqqqq'
#
# During handling of the above exception, another exception occurred:
#
# Traceback (most recent call last):
# File "/_/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 131, in data_received
# self.parser.feed_data(data)
# File "httptools/parser/parser.pyx", line 212, in httptools.parser.parser.HttpParser.feed_data
# httptools.parser.errors.HttpParserCallbackError: `on_url` callback error
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\nUpgrade to version 0.17.5 or later.\nWhen NOT to use: This fix is not applicable if using a different HTTP parser that does not support fragmentation.\n\n
Why This Fix Works in Production
- Trigger: httptools.parser.errors.HttpParserInvalidURLError on fragmented first line of the HTTP request.
- Mechanism: The httptools parser fails to handle fragmented URLs in HTTP requests
- Why the fix works: Fixes the case where the URL is fragmented in the httptools protocol, addressing the issue raised in #1262. (first fixed release: 0.17.5).
- If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).
Why This Breaks in Prod
- Shows up under Python 3.8 in real deployments (not just unit tests).
- The httptools parser fails to handle fragmented URLs in HTTP requests
- Surfaces as: httptools.parser.errors.HttpParserInvalidURLError on fragmented first line of the HTTP request.
Proof / Evidence
- GitHub issue: #1262
- Fix PR: https://github.com/kludex/uvicorn/pull/1263
- First fixed release: 0.17.5
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.75
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.42
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“>want to takle it @div1001 ? I've understood your solution idea. If it's ok I'd like to try. I'd like to take some time for…”
“this might be a good first approach from here: https://github.com/encode/uvicorn/pull/778/files, want to takle it @div1001 ? there might be side effect from this PR but…”
“Yay – the issue from #344 was reproduced! Good job @div1001!”
“thanks for the detailed report ! I'm not sure there's much we can do about it, httptools is using for its .parse_url https://github.com/nodejs/http-parser”
Failure Signature (Search String)
- httptools.parser.errors.HttpParserInvalidURLError on fragmented first line of the HTTP request.
Error Message
Stack trace
Error Message
-------------
httptools.parser.errors.HttpParserInvalidURLError on fragmented first line of the HTTP request.
Minimal Reproduction
import socket
import threading as th
from time import sleep
import uvicorn
########## HELPERS ##########
def receive_all(sock):
chunks = []
while True:
chunk = sock.recv(1024)
if not chunk:
break
chunks.append(chunk)
return b''.join(chunks)
async def stub_app(scope, receive, send):
event = await receive()
d = b'whatever'
await send({
'type': 'http.response.start',
'status': 200,
'headers': [
[b'Content-Length', str(len(d)).encode()],
[b'Content-Type', b'text/plain'],
]
})
await send({'type': 'http.response.body', 'body': d, 'more_body': False})
#############################
def send_fragmented_req(path):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', 5000))
d = (
f'GET {path} HTTP/1.1\r\n'
'Host: localhost\r\n'
'Connection: close\r\n\r\n'
).encode()
split = len(path) // 2
# send first part
sock.sendall(d[:split])
sleep(0.01) # important, ~100% error, without this the chances of reproducing the error are pretty low
# send second part
sock.sendall(d[split:])
# read response
resp = receive_all(sock)
sock.shutdown(socket.SHUT_RDWR)
sock.close()
return resp
def test():
t = th.Thread(target=lambda: uvicorn.run(stub_app, host='127.0.0.1', port=5000, http='httptools'))
t.daemon = True
t.start()
sleep(1) # wait for unicorn to start
path = '/?param=' + 'q' * 10
print(send_fragmented_req(path).decode())
if __name__ == '__main__':
test()
# Console output:
# INFO: Started server process [748603]
# INFO: Waiting for application startup.
# INFO: ASGI 'lifespan' protocol appears unsupported.
# INFO: Application startup complete.
# INFO: Uvicorn running on http://127.0.0.1:5000 (Press CTRL+C to quit)
# WARNING: Invalid HTTP request received.
# Traceback (most recent call last):
# File "httptools/parser/parser.pyx", line 258, in httptools.parser.parser.cb_on_url
# File "/_/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 193, in on_url
# parsed_url = httptools.parse_url(url)
# File "httptools/parser/url_parser.pyx", line 105, in httptools.parser.url_parser.parse_url
# httptools.parser.errors.HttpParserInvalidURLError: invalid url b'am=qqqqqqqqqq'
#
# During handling of the above exception, another exception occurred:
#
# Traceback (most recent call last):
# File "/_/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 131, in data_received
# self.parser.feed_data(data)
# File "httptools/parser/parser.pyx", line 212, in httptools.parser.parser.HttpParser.feed_data
# httptools.parser.errors.HttpParserCallbackError: `on_url` callback error
Environment
- Python: 3.8
What Broke
The server closes the connection upon receiving fragmented HTTP requests, leading to failed requests.
Why It Broke
The httptools parser fails to handle fragmented URLs in HTTP requests
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
Upgrade to version 0.17.5 or later.
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Fix reference: https://github.com/kludex/uvicorn/pull/1263
First fixed release: 0.17.5
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix is not applicable if using a different HTTP parser that does not support fragmentation.
Verify Fix
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Capture the exact failing error string in logs and tests so you can reproduce via a minimal script.
- Pin production dependencies and upgrade only with a reproducible test that hits the failing path.
Version Compatibility Table
| Version | Status |
|---|---|
| 0.17.5 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.