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

aexception doesn't pass the exception info #488

Closed
codepainters opened this issue Feb 9, 2023 · 4 comments
Closed

aexception doesn't pass the exception info #488

codepainters opened this issue Feb 9, 2023 · 4 comments

Comments

@codepainters
Copy link
Contributor

This simple test code

import asyncio
import structlog
log = structlog.get_logger()


async def kaboom():
    try:
        print(1 / 0)
    except Exception:
        await log.aexception('kaboomed')


if __name__ == '__main__':
    asyncio.new_event_loop().run_until_complete(kaboom())

...fails with...

  // top of stack removed for brevity
  File "/home/czajnik/.cache/pypoetry/virtualenvs/poor-xCdm2f7w-py3.11/lib/python3.11/site-packages/structlog/dev.py", line 424, in __call__
    self._exception_formatter(sio, exc_info)
  File "/home/czajnik/.cache/pypoetry/virtualenvs/poor-xCdm2f7w-py3.11/lib/python3.11/site-packages/structlog/dev.py", line 187, in rich_traceback
    Traceback.from_exception(*exc_info, show_locals=True)
  File "/home/czajnik/.cache/pypoetry/virtualenvs/poor-xCdm2f7w-py3.11/lib/python3.11/site-packages/rich/traceback.py", line 335, in from_exception
    rich_traceback = cls.extract(
                     ^^^^^^^^^^^^
  File "/home/czajnik/.cache/pypoetry/virtualenvs/poor-xCdm2f7w-py3.11/lib/python3.11/site-packages/rich/traceback.py", line 405, in extract
    exc_type=safe_str(exc_type.__name__),
                      ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '__name__'. Did you mean: '__ne__'?

Process finished with exit code 1

I believe the problem is here:

async def aexception(self: FilteringBoundLogger, event: str, **kw: Any) -> Any:
kw.setdefault("exc_info", True)
ctx = contextvars.copy_context()
return await asyncio.get_running_loop().run_in_executor(
None,
lambda: ctx.run(lambda: self.error(event, **kw)),
)

Code spawned in the executor can no longer retrieve original exception information.

As a workaround I do the following:

        await log.aexception('kaboomed', exc_info=sys.exc_info())

But why can't it be a default value for exc_info?

@SerGeRybakov
Copy link

@codepainters @hynek, hi!
I use 22.3.0 and have the same problem with no exc_info in aexception.
Is that merge #489 already released in any version or not? If still not, when it shall be released?
Thanks!

@leechristensen
Copy link

Same bug for me. Looks like the PyPI package has not been updated. Any plans for a release above 22.3.0?

@hynek
Copy link
Owner

hynek commented Apr 5, 2023

I'll try to get a release out before PyCon US.

I would like to add the set of async methods to standard library logger before I do so.

Closing since this is fixed and we only forgot to close it.

@hynek hynek closed this as completed Apr 5, 2023
@hynek
Copy link
Owner

hynek commented Apr 6, 2023

23.1.0 is on PyPI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants