python-proxy icon indicating copy to clipboard operation
python-proxy copied to clipboard

How do I run a proxy server asynchronously?

Open plotski opened this issue 3 years ago • 0 comments

I would like to run a proxy server instance asynchronously with await instead of blockingly with run_until_complete() or run_forver().

It's kinda working, but I'm getting RuntimeError: Event loop is closed.

This should reproduce the issue:

import contextlib

import httpx
import pproxy
import pytest

@contextlib.asynccontextmanager
async def start_proxy():
    port = 60100
    url = f'http://localhost:{port}'

    args = {'rserver': [pproxy.DIRECT], 'verbose': print}
    server = await pproxy.Server(url).start_server(args)

    yield url

    server.close()
    await server.wait_closed()

@pytest.mark.asyncio
async def test_proxy(tmp_path):
    async with start_proxy() as proxy_url:
        async with httpx.AsyncClient(proxies=proxy_url) as client:
            print(await client.get('http://example.org'))

The test passes, but I'm getting this traceback:

Exception ignored in: <coroutine object BaseProtocol.channel at 0x7fd2304ab920>
Traceback (most recent call last):
  File "[...]/lib/python3.10/site-packages/pproxy/proto.py", line 72, in channel
    writer.close()
  File "[...]/3.10.1/lib/python3.10/asyncio/streams.py", line 338, in close
    return self._transport.close()
  File "[...]/3.10.1/lib/python3.10/asyncio/selector_events.py", line 700, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "[...]/3.10.1/lib/python3.10/asyncio/base_events.py", line 745, in call_soon
    self._check_closed()
  File "[...]/3.10.1/lib/python3.10/asyncio/base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Task was destroyed but it is pending!
task: <Task pending name='Task-9' coro=<BaseProtocol.channel() done, defined at [...]/lib/python3.10/site-packages/pproxy/proto.py:56> wait_for=<Future pending cb=[Task.task_wakeup()]>>

Am I doing it wrong?

plotski avatar Feb 17 '22 16:02 plotski