uwsgi icon indicating copy to clipboard operation
uwsgi copied to clipboard

Segmentation Fault when enabling --py-call-uwsgi-fork-hooks with Python 3.12 + Sentry SDK

Open Den-Varenik opened this issue 4 months ago • 1 comments

Description

Running uWSGI with both --enable-threads and --py-call-uwsgi-fork-hooks (as recommended by [Sentry’s WSGI docs](https://docs.sentry.io/platforms/python/integrations/wsgi/#behavior)) causes a Segmentation Fault at startup.

Without --py-call-uwsgi-fork-hooks, uWSGI starts, but Sentry warns about unsupported preforking/thread mode.


Environment

  • OS: macOS 14.6 (Darwin Kernel 24.6.0, arm64, Apple Silicon M2 Pro)
  • Python: 3.12.3
  • uWSGI: 2.0.30 (compiled with Apple LLVM 17.0.0, clang-1700.0.13.5)
  • sentry-sdk: 2.37.1
  • flask: 3.1.2

Steps to Reproduce

  1. Install dependencies:

    pip install flask==3.1.2 sentry-sdk==2.37.1 uwsgi==2.0.30
    
  2. Create app.py:

    from flask import Flask
    import sentry_sdk
    from sentry_sdk.integrations.flask import FlaskIntegration
    
    app = Flask(__name__)
    
    sentry_sdk.init(
        dsn="your_sdk_dsn",
        integrations=[FlaskIntegration()],
        traces_sample_rate=0.2,
        profiles_sample_rate=0.1,
        environment="test",
        send_default_pii=False,
    )
    
    @app.route("/")
    def hello_world():
        return "Hello World!"
    
    if __name__ == "__main__":
        app.run()
    
  3. Run uWSGI:

    uwsgi \
      --http=0.0.0.0:8080 \
      --chdir=$(pwd) \
      --mount /=app:app \
      --processes=4 \
      --threads=2 \
      --max-requests=1000 \
      --master \
      --die-on-term \
      --enable-threads \
      --py-call-uwsgi-fork-hooks
    

Expected Behavior

  • uWSGI should start normally with both options enabled.
  • Sentry integration should work without warnings or crashes.

Actual Behavior

  • uWSGI crashes immediately with a segmentation fault.

Backtrace excerpt:

!!! uWSGI process 92612 got Segmentation Fault !!!
*** backtrace of 92612 ***
0   uwsgi                               0x0000000104e091a0 uwsgi_backtrace + 52
1   uwsgi                               0x0000000104e096a4 uwsgi_segfault + 56
2   libsystem_platform.dylib            0x00000001965b96a4 _sigtramp + 56
3   libpython3.12.dylib                 0x000000010591a6b0 _PyObject_Malloc + 32
4   libpython3.12.dylib                 0x0000000105942290 PyUnicode_New + 200
5   libpython3.12.dylib                 0x000000010594bc84 unicode_decode_utf8 + 244
6   libpython3.12.dylib                 0x00000001059e8bd8 PyImport_ImportModule + 16
7   uwsgi                               0x0000000104e1ef5c uwsgi_uwsgi_loader + 32
8   uwsgi                               0x0000000104e1e730 init_uwsgi_app + 852
9   uwsgi                               0x0000000104e1b938 uwsgi_python_mount_app + 172
10  uwsgi                               0x0000000104e0d330 uwsgi_init_all_apps + 384
11  uwsgi                               0x0000000104e0cc28 uwsgi_start + 2944
12  uwsgi                               0x0000000104e0b3c0 uwsgi_setup + 7076
13  uwsgi                               0x0000000104e09818 main + 12
14  dyld                                0x00000001961deb98 start + 6076
*** end of backtrace ***

Notes

  • Without --py-call-uwsgi-fork-hooks, Sentry logs:

    IMPORTANT: We detected the use of uWSGI in preforking mode without thread support. 
    This might lead to crashing workers. Please run uWSGI with both 
    "--enable-threads" and "--py-call-uwsgi-fork-hooks" for full support.
    
  • With the flag, uWSGI segfaults immediately.

  • Reproducible with a minimal Flask + Sentry app on Python 3.12.


Question for Maintainers

  • Is this a bug in uWSGI’s fork hooks implementation (possibly related to Python 3.12)?
  • Or is there a known limitation/workaround for using --py-call-uwsgi-fork-hooks?

Den-Varenik avatar Sep 11 '25 18:09 Den-Varenik