urllib3: trying to close a response hangs if a blocking read is happening on another thread
Subject
Thread A starts Thread B to read a chunked HTTP response. At some point, regardless of whether the server has more data, I want to break the connection from the client side. Thread B is blocked trying to read the socket, so I’m trying to call HTTPResponse.close() from Thread A. This call hangs.
I’m not sure if this is a urllib3 issue, or a limitation of the underlying Python synchronous I/O model, or something I am doing wrong.
Environment
- Python 3.7.13, 3.10.6
- platform: Darwin-22.2.0-x86_64-i386-64bit, Linux-5.10.104-linuxkit-x86_64-with-debian-10.13
- urllib3 1.26.13
Steps to Reproduce
Unzip this archive and run main.py.
This starts a simple HTTP server on a daemon thread; makes a request to it from another thread, which receives a little bit of data and then blocks waiting for more; then, from the main thread, tries to close the response.
Expected Behavior
I hoped that closing the response would cause the underlying socket to be closed, causing the client thread to get a read error and terminate. For this demo app, the output would look like this:
main [MainThread] starting server
server [Thread-1] listening on port 10000
main [MainThread] starting client
client [Thread-2] opening connection
main [MainThread] sleeping a while
server [Thread-1] starting stream
127.0.0.1 - - [05/Jan/2023 13:38:11] "GET / HTTP/1.1" 200 -
server [Thread-1] wrote 10 bytes
client [Thread-2] reading data
client [Thread-2] read 10 bytes
main [MainThread] about to close response
main [MainThread] closed response
client [Thread-2] failed: <some kind of I/O error>
client [Thread-2] no longer reading
main [MainThread] about to clear connection pool
main [MainThread] cleared connection pool
main [MainThread] exiting
Actual Behavior
The HTTPResponse.close() call hangs. The output looks like this, and then the process waits indefinitely:
main [MainThread] starting server
server [Thread-1] listening on port 10000
main [MainThread] starting client
client [Thread-2] opening connection
main [MainThread] sleeping a while
server [Thread-1] starting stream
127.0.0.1 - - [05/Jan/2023 13:38:11] "GET / HTTP/1.1" 200 -
server [Thread-1] wrote 10 bytes
client [Thread-2] reading data
client [Thread-2] read 10 bytes
main [MainThread] about to close response
The only way I have found to make it not hang is to make the server send some more data; then, the blocking read on the client read unblocks, and the client is able to notice that the HTTPResponse is no longer in a valid state. But then that no longer corresponds to the use case I’m talking about, where the server does not have more data yet but I still want to break the connection.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 18 (4 by maintainers)
Commits related to this issue
- Release 2.12.904 (#188) 2.12.904 (2024-12-22) ===================== - Fixed an issue when trying to force load Websocket over HTTP/2 or HTTP/3. - Ensured WebSocket via HTTP/2 with improved CI pipeli... — committed to jawah/urllib3.future by Ousret 2 years ago
- Bump urllib3 from 2.2.3 to 2.3.0 (#10202) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://gith... — committed to aio-libs/aiohttp by dependabot[bot] 2 years ago
- build(deps): bump urllib3 from 2.2.3 to 2.3.0 (#149) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="h... — committed to Jij-Inc/Playground by github-actions[bot] 2 years ago
- Bump urllib3 from 2.2.3 to 2.3.0 (#220) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://github... — committed to martimlobao/regybox by dependabot[bot] 2 years ago
- Dependency Updates: Bump urllib3 from 2.2.3 to 2.3.0 in /repo/archlinux/index (#92) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</summary>... — committed to wxx9248/repo.wxx9248.top by dependabot[bot] 2 years ago
- Bump the all group with 2 updates (#137) Bumps the all group with 2 updates: [click](https://github.com/pallets/click) and [urllib3](https://github.com/urllib3/urllib3). Updates `click` from 8.1... — committed to jmertic/landscape-tools by dependabot[bot] 2 years ago
- Bump the all group with 2 updates (#96) Bumps the all group with 2 updates: [urllib3](https://github.com/urllib3/urllib3) and [click](https://github.com/pallets/click). Updates `urllib3` from 2.... — committed to jmertic/lfx-landscape-tools by dependabot[bot] 2 years ago
- Dependency Updates: Bump urllib3 from 2.2.3 to 2.3.0 in /script (#14) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</summary> <p><em>Source... — committed to wxx9248/Arch-Linux-Kernel-Build by dependabot[bot] 2 years ago
- Bump urllib3 from 2.2.2 to 2.3.0 (#15) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.2 to 2.3.0. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://git... — committed to jhmartin/PySpy by jhmartin 2 years ago
- Bump urllib3 from 2.2.3 to 2.3.0 (#10241) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://gith... — committed to aio-libs/aiohttp by dependabot[bot] 2 years ago
- Bump the dependencies group in /.config with 9 updates (#4349) Bumps the dependencies group in /.config with 9 updates: | Package | From | To | | --- | --- | --- | | [pydoclint](https://github.com/j... — committed to ansible/molecule by dependabot[bot] 2 years ago
- Bump the pip-deps group with 5 updates (#66) Bumps the pip-deps group with 5 updates: | Package | From | To | | --- | --- | --- | | [charset-normalizer](https://github.com/jawah/charset_normalizer) ... — committed to miniscruff/scopie by dependabot[bot] 2 years ago
- chore(deps): Bump urllib3 from 2.2.2 to 2.3.0 in /experimenter/tests (#11983) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.2 to 2.3.0. <details> <summary>Release notes</summary> <p><e... — committed to mozilla/experimenter by dependabot[bot] 2 years ago
- [Dependency] Bump urllib3 from 2.2.3 to 2.3.0 in /.setup/pip (#11295) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</summary> <p><em>Sou... — committed to Submitty/Submitty by dependabot[bot] 2 years ago
- chore(deps-dev): bump urllib3 from 2.2.3 to 2.3.0 in /_integration-tests/utils/agent (#483) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</... — committed to DataDog/orchestrion by dependabot[bot] 2 years ago
- Bump urllib3 from 2.2.3 to 2.3.0 in /api (#348) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="ht... — committed to azimuth-cloud/azimuth by dependabot[bot] 2 years ago
- build(deps): bump urllib3 from 2.2.3 to 2.3.0 in /tools/publish (#2699) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</summary> <p><em>Sour... — committed to bazel-contrib/rules_python by dependabot[bot] 2 years ago
- build(deps): bump urllib3 from 2.2.3 to 2.3.0 in /docs (#2698) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</summary> <p><em>Sourced from ... — committed to bazel-contrib/rules_python by dependabot[bot] 2 years ago
- Bump the llvm-docs-requirements group in /llvm/docs with 7 updates (#17770) Bumps the llvm-docs-requirements group in /llvm/docs with 7 updates: | Package | From | To | | --- | --- | --- | | [babel]... — committed to intel/llvm by dependabot[bot] 2 years ago
- chore(deps): bump urllib3 from 2.2.3 to 2.3.0 (#232) Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.2.3 to 2.3.0. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="h... — committed to Aiven-Open/kio by dependabot[bot] 2 years ago
Right, I read your comment when you posted it but then mostly ignored the thread and started again from first principles. It’s a good sign that we landed on the same solution!
@eight04, @pquentin et al.
I did some more testing, and it looks like the problem is the underlying io.BufferReader isn’t realizing the socket is closed, so it’s waiting on that.
In previous comments, we had mentioned trying
but that didn’t allow us to re-use the connection pool. I think calling
works. At least my local testing seems to indicate it is working. However, I have no idea what sort of ramifications there might be to doing something like this. Also I’m reaching deep into implementation details, so this is also clearly a fragile solution.
Can someone with more expertise on this repository take a look and let me know if this is a viable solution to the problem, and if we can get this fix introduced into the official library somewhere?
@eight04 your example works but isn’t quite the same as issue we are experiencing.
In your example, the
t.get()call is going to block until ther.streamthrows an exception. And that doesn’t occur until the server tries to send some new bytes down the line and then the stream moves on to the NEXT iteration.If I replace the httpbin example with an SSE server that sends some initial payload but then waits a minute before sending the next payload, your example will block until that minute has lapsed and the new data has been pushed down.
Is there a way we can avoid this blocking behavior?
Here’s something I just came up with that appears to work, but it’s pretty hacky and I’m not sure how portable it is:
When I substitute this for
resp.close(), it has the desired effect of immediately closing the socket so that the current blocking read errors out.