Skip to content

How to debug local running app (not tests)? #3522

@daredevil82

Description

@daredevil82

How do you use ipdb to debug a local running application? The only reference in the docs are focused on tests, which are less than helpful in my case.

class AggregateView(BaseView):  # BaseView inherits from `aiohttp.web.View`
    async def post(self):
        import ipdb
        ipdb.set_trace()
        requests = await self.request.json()
        logger.debug(f'Making [{len(requests)}] service calls at [{datetime.now()}]')
        queries = []
        async with ClientSession(cookie_jar=CookieJar(unsafe=True)) as session:
            for req in requests:
                queries.append(self._retrieve(req, session))
            
            logger.debug(f'Async calls created [{datetime.now()}]')
            results = await asyncio.gather(*queries)
            logger.debug(f'Async calls completed and aggregated [{datetime.now()}]')
        
        return web.json_response(
                status=207,
                data=results
        )

Running with command line docker-compose run --rm --service-ports aiohttp results in this output:

     89         import ipdb; ipdb.set_trace()
---> 90         ipdb.set_trace()
     91         requests = await self.request.json().

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/prompt_toolkit/application/application.py", line 652, in _run_async2
    result = yield f
  File "/usr/local/lib/python3.6/site-packages/prompt_toolkit/eventloop/coroutine.py", line 86, in step_next
    new_f = coroutine.send(None)
  File "/usr/local/lib/python3.6/site-packages/prompt_toolkit/application/application.py", line 591, in _run_async
    with self.input.raw_mode():
  File "/usr/local/lib/python3.6/site-packages/prompt_toolkit/input/vt100.py", line 231, in __enter__
    os.write(self.fileno, b'\x1b[?1l')
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 390, in start
    resp = await self._request_handler(request)
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_app.py", line 366, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_urldispatcher.py", line 740, in _iter
    resp = await method()
  File "/orm/service/api/views.py", line 90, in post
    ipdb.set_trace()
  File "/orm/service/api/views.py", line 90, in post
    ipdb.set_trace()
  File "/usr/local/lib/python3.6/bdb.py", line 51, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/local/lib/python3.6/bdb.py", line 69, in dispatch_line
    self.user_line(frame)
  File "/usr/local/lib/python3.6/pdb.py", line 261, in user_line
    self.interaction(frame, None)
  File "/usr/local/lib/python3.6/site-packages/IPython/core/debugger.py", line 297, in interaction
    OldPdb.interaction(self, frame, traceback)
  File "/usr/local/lib/python3.6/pdb.py", line 352, in interaction
    self._cmdloop()
  File "/usr/local/lib/python3.6/pdb.py", line 321, in _cmdloop
    self.cmdloop()
  File "/usr/local/lib/python3.6/site-packages/IPython/terminal/debugger.py", line 88, in cmdloop
    line = self.pt_app.prompt() # reset_current_buffer=True)
  File "/usr/local/lib/python3.6/site-packages/prompt_toolkit/shortcuts/prompt.py", line 733, in prompt
    return run_sync()
  File "/usr/local/lib/python3.6/site-packages/prompt_toolkit/shortcuts/prompt.py", line 722, in run_sync
    return self.app.run(inputhook=self.inputhook, pre_run=pre_run2)
  File "/usr/local/lib/python3.6/site-packages/prompt_toolkit/application/application.py", line 699, in run
    return run()
  File "/usr/local/lib/python3.6/site-packages/prompt_toolkit/application/application.py", line 673, in run
    return f.result()
  File "/usr/local/lib/python3.6/site-packages/prompt_toolkit/eventloop/future.py", line 149, in result
    raise self._exception
  File "/usr/local/lib/python3.6/site-packages/prompt_toolkit/eventloop/coroutine.py", line 90, in step_next
    new_f = coroutine.throw(exc)
  File "/usr/local/lib/python3.6/site-packages/prompt_toolkit/application/application.py", line 654, in _run_async2
    assert not self._is_running
AssertionError
[14:38:31]● POST /aggregate 500 3.5KB 623ms

my web application is started with:

from dashboard_service import aiohttp_settings as settings
from api.routes import setup_routes


async def create_app():
    app = web.Application()
    app['config'] = settings
    setup_routes(app)
    return app


def main():
    logging.basicConfig(level=logging.DEBUG)
    app = create_app()
    web.run_app(app, host=settings.HOST, port=settings.PORT)

Other things I've tried are from https://docs.python.org/3.6/library/asyncio-dev.html, including

echo $PYTHONASYNCIODEBUG
1

With this kind of project, how do we get ipdb breakpoints to work with no exception stack traces?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions