Skip to content

ConnectionResetError: Cannot write to closing transport #27

@soyasoya5

Description

@soyasoya5

Python version: Python 3.8.0

Sample Code

import aiohttp
import asyncio
from aiohttp_socks import ProxyConnector

class Example:
    async def create_session(self):
        connector = ProxyConnector.from_url('socks4://13.0.0.2:1080')
        self.s = aiohttp.ClientSession(connector=connector)

    async def close_session(self):
        await self.s.close()

    async def send_request(self):
        async with self.s.get('https://google.com') as r:
            print(r.status)

async def task_helper(example, s_time):
    await asyncio.sleep(s_time)
    await example.send_request()

async def main():
    example = Example()
    try:
        await example.create_session()
        tasks = [asyncio.create_task(task_helper(example, i)) for i in range(0,15,3)]
        await asyncio.gather(*tasks)
    finally:
        await example.close_session()

if __name__ == '__main__':
    asyncio.run(main())

Error

An open stream object is being garbage collected; call "stream.close()" explicitly.
Traceback (most recent call last):
  File "/home/user/pyvenv/aiohttp/lib/python3.8/site-packages/aiohttp/client.py", line 542, in _request
    resp = await req.send(conn)
  File "/home/user/pyvenv/aiohttp/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 668, in send
    await writer.write_headers(status_line, self.headers)
  File "/home/user/pyvenv/aiohttp/lib/python3.8/site-packages/aiohttp/http_writer.py", line 119, in write_headers
    self._write(buf)
  File "/home/user/pyvenv/aiohttp/lib/python3.8/site-packages/aiohttp/http_writer.py", line 67, in _write
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test.py", line 31, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.8/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete
    return future.result()
  File "test.py", line 26, in main
    await asyncio.gather(*tasks)
  File "test.py", line 19, in task_helper
    await example.send_request()
  File "test.py", line 14, in send_request
    async with self.s.get('https://google.com') as r:
  File "/home/user/pyvenv/aiohttp/lib/python3.8/site-packages/aiohttp/client.py", line 1117, in __aenter__
    self._resp = await self._coro
  File "/home/user/pyvenv/aiohttp/lib/python3.8/site-packages/aiohttp/client.py", line 554, in _request
    raise ClientOSError(*exc.args) from exc
aiohttp.client_exceptions.ClientOSError: Cannot write to closing transport

I believe the socks proxy works, as the same async code works using httpx, and synchronous code works using requests-socks

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