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

Python 3.11 crashes in some cases when loguru.logger.exception called #775

Closed
gabus opened this issue Jan 6, 2023 · 8 comments
Closed
Labels
upstream This is not our bug

Comments

@gabus
Copy link

gabus commented Jan 6, 2023

I'm sorry as I can't provide example but after digging into code the only change which is needed

formatter_record["exception"] = "".join(lines)
change to
formatter_record["exception"] = "".join(str(lines))
in loguru._handler.Handler.emit

it seems python 3.11 doesn't like implicit casting.
Though I'm second guessing my solution - don't know why it works..

@Delgan
Copy link
Owner

Delgan commented Jan 6, 2023

Thank you for the report. If you ever find out which exception reproduces the problem, let me know. I know there were different changes in exception handling in Python 3.11 which are not yet unit tested by Loguru, I'll try to find out what can cause this.

@Delgan Delgan added the bug Something isn't working label Jan 6, 2023
@gabus
Copy link
Author

gabus commented Jan 6, 2023

Managed to create a snippet which breaks

from loguru import logger
import falcon


class Ping:
    def on_get(self, req, resp):
        raise falcon.HTTPUnauthorized("test", "test unauthorised")


class ErrorHandler:
    def handle(self, req, resp, ex, params):
        logger.debug(ex)                   # works
        logger.exception(ex)               # dies here


def create_application():
    app = falcon.App()
    app.add_route('/ping', Ping())
    app.add_error_handler(falcon.HTTPError, ErrorHandler().handle)
    return app


application = create_application()

To build and run use

python3.11 -m venv venv
source venv/bin/activate
pip install pipenv

pipenv install loguru
pipenv install gunicorn
pipenv install falcon

venv/bin/gunicorn -p 8000 --reload --pythonpath ./ test_loguru

curl 'http://127.0.0.1:8000/ping'

@Delgan
Copy link
Owner

Delgan commented Jan 6, 2023

Thanks for the snippet, @gabus.

Unfortunately, it seems I'm unable to reproduce the issue.
Which versions of gunicorn and falcon are you using?
What is the observed output of the code you shared?

@gabus
Copy link
Author

gabus commented Jan 9, 2023

Interesting!
Can it be anything to do with macOS python version? I'm on Monterey 12.6 mac with M1 chip.

This is my setup:

Python 3.11.1

gunicorn==20.1.0
falcon==3.1.1
loguru==0.6.0
pipenv==2022.12.19
virtualenv==20.17.1

Output is very vague. It just shows that worker died and new one has been spawned. Then in separate pop up python crash window appears asking action: Ignore, Restart or Close Pyhton.

[2022-12-22 13:02:20 +0000] [53154] [WARNING] Worker with pid 53323 was terminated due to signal 11
[2022-12-22 13:02:20 +0000] [53339] [INFO] Booting worker with pid: 53339

@xzmeng
Copy link

xzmeng commented Jan 9, 2023

I can reproduce it on macOS 13.1 with an M1 chip.

The process terminted with a segmentation violation when reading tb.tb_frame.

frame = tb.tb_frame.f_back

@xzmeng
Copy link

xzmeng commented Jan 9, 2023

I don't think it's a loguru's bug because accessing an instance attribute should never cause a core dump. (So maybe it's caused by Cython? I have tested with unCythonized falcon, and it works as expected.)

@Delgan
Copy link
Owner

Delgan commented Jan 9, 2023

Thanks @gabus and @xzmeng for the additional details.

I tested on Linux but have no access to MacOS computer unfortunately.

Are you both using Cython?
This recent pull request in python/cpython fixes a segfault in Python 3.11 when accessing frame.f_back, it does mention Cython as a environment where the bug happened: python/cpython#100182
I think this is what you faced @xzmeng.

@Delgan
Copy link
Owner

Delgan commented Aug 31, 2023

I'm closing this issue as it likely not a Loguru bug (as shown by ticket linked above). 👍

@Delgan Delgan closed this as completed Aug 31, 2023
@Delgan Delgan added upstream This is not our bug and removed bug Something isn't working labels Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream This is not our bug
Projects
None yet
Development

No branches or pull requests

3 participants