Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
daredevil82 opened this issue Jan 11, 2019 · 5 comments
Closed

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

daredevil82 opened this issue Jan 11, 2019 · 5 comments

Comments

@daredevil82
Copy link

daredevil82 commented Jan 11, 2019

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?

@aio-libs-bot
Copy link

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are #3198 (Test Client Loop Error with Actual App), #2375 (add a way to run an app asynchron), #1121 (Deprecate debug parameter from app.make_hander()), #2148 (Failed tests), and #2255 (HTTPS test_client doesn't run app with SSL context).

@asvetlov
Copy link
Member

pdb (idpb, pdbpp and family) just works.

You are running in docker, it can be a problem.
pdb needs stdin, stdout, and stderr to work.

@daredevil82
Copy link
Author

/shrug

I only get this issue with aiohttp, not with flask/django/other non-async dockerized python projects.

@asvetlov
Copy link
Member

Please double check your environment.
aiohttp is not special to debugging, I can run and debug it without problems in docker/kubernetis, at least with standard pdb and pdbpp.
Didn't use ipdb for a while.

I'm going to close the issue because it is not a feature request or bug report but a usage question of debugger tool even not provided by aiohttp itself.
Sorry, your problem is not related to the project and cannot be fixed by aiohttp source code change.

@lock
Copy link

lock bot commented Jan 11, 2020

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Jan 11, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Jan 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants