Skip to content

[Bug]: Qt-enabled Python tests segfault in getenv on Qt WebEngine's GPU thread #1113

Closed

Description

@deniial00

Before you start

  • I searched the existing issues and did not find a duplicate.
  • This is a bug report, not a usage question.

What happened?

The Qt-enabled Python CI job (test / Test (ubuntu-latest, Python 3.12), the only job that installs Qt WebEngine) segfaults with exit 139 in roughly 0.5–2% of runs; a rerun passes. The Python frame in the log moves around: Session() construction, a numpy import, _collections_abc. That frame is only where the main thread happened to be.

Core dumps show the fault is on a Qt WebEngine thread:

#3  getenv () from libc.so.6
#4  ?? () from libgallium-25.2.8-0ubuntu0.24.04.4.so
#5  driCreateNewScreen3 () from libgallium-25.2.8-0ubuntu0.24.04.4.so
#6-#9  ?? () from libEGL_mesa.so.0
#10 ?? () from libQt6WebEngineCore.so.6

WebEngine sets up EGL (Mesa) on its GPU thread, and Mesa reads the environment there. Meanwhile the main thread adds environment variables: monkeypatch.setenv("RUSTGIS_CONFIG_DIR", ...) in the saved-secrets tests, numpy's first import (OPENBLAS_MAIN_FREE), and pytest's PYTEST_CURRENT_TEST. glibc's setenv can move and free the environment array while getenv is reading it. Details and verification are in this comment. RustGIS itself is not involved; the crash affects the test suite.

Steps to reproduce

  1. Run the Python 3.12 job of .github/workflows/test-python.yml (Qt system libraries, RUSTGIS_TEST_QT=1, extras test,geopandas,qt, --features test-stores).
  2. python -m pytest -q in apps/rustgis-python; about 1 run in 50–200 crashes.

A faster repro is to add and remove environment variables in a loop while QWebEngineViews start under QT_QPA_PLATFORM=offscreen. That crashed in getenv in 17 of 30 cold runs on ubuntu-latest, and in 0 of 30 with --webEngineArgs --disable-gpu.

Expected behavior

The Python suite completes without a native crash.

How are you running RustGIS?

Python package built from source in GitHub Actions (none of the template's choices apply).

Version

0.4.0; first seen on main and PR merge commits around 2026-10-28.

Operating system, browser and graphics

Ubuntu 24.04 (ubuntu-latest), Python 3.12.15, PySide6 6.12.0 / Qt WebEngine 6.12.0.140, Mesa 25.2.8.

Sample data

None needed.

Screenshots or logs

Crashed CI runs: 40657663134, 40750952515, 40768155574, 40770996179. Core dumps and verification runs: 40778483078, 40783193853.

Activity

  1. added
    unconfirmed possible issue
    on Oct 28, 2026
  2. deniial00 commented on Oct 28, 2026

    @deniial00
    MemberAuthor

    Investigation found a definite Qt test-lifetime defect, but has not established the original native fault's exact cause.

    • The same workflow now passes on attempt 2 without source changes: https://github.com/opengeos/RustGIS/actions/runs/40770996179/attempts/2. This was a flaky native crash, not a deterministic raster assertion failure.
    • test_qt.py returned widgets without native teardown and retained QApplication only for that module. Widgets contain a bound-callback reference cycle, so Python reference-counting does not dispose of them when a test ends.
    • A focused Linux Python 3.12 smoke runner disabled cyclic GC and inspected native top-level widgets after fixture teardown. Before the change, the first widget test left one live native MapWidget; the smoke failed there. This demonstrates uncontrolled cleanup timing independently of the original segfault.
    • The correction keeps QApplication for the test session and routes every test-created widget through a yield fixture. Its finally closes widgets, schedules native destruction, and explicitly delivers DeferredDelete events on the GUI thread. No session locking changes, skipped tests, dependency pins, or separate-process workaround.
    • Afterward, the same GC-disabled lifetime smoke plus the saved-secrets tests passes (8 tests). The complete current-source suite passes on Linux ARM64/Python 3.12 and macOS ARM64/Python 3.12 (42 tests on each).

    Qt requires the application to be the first-created and last-destroyed QObject: https://doc.qt.io/qt-6/threads-qobject.html. [INFERENCE] Uncontrolled Qt cleanup caused the original later Session() crash. The lifetime bug and its correction are demonstrated; the original exit 139 has not been reproduced locally, so this issue should not yet be treated as a conclusively diagnosed native crash.

    Additional verification: cross-built current-source bindings run on Linux x86_64/Python 3.12 under Docker emulation. The GC-disabled Qt lifetime smoke plus saved-secrets tests passes (8 tests); the complete suite passes with 41 tests and one optional GeoPandas skip. The original fixture version also passes that x86 full-suite run, further limiting the causal claim. These local Linux runs are Debian-based, not the original Ubuntu runner image.

    Ownership follow-up: a stronger GC-disabled smoke deliberately retained the widget, bridge, view, and page wrappers while ending the fixture, and checked native destroyed signals. It exposed one additional gap: the unparented bridge survived widget deletion. Fixture teardown now explicitly schedules the bridge for deletion too. The smoke now observes destruction of all four native objects and no survivors; bridge message delivery was exercised before teardown. The complete macOS Python 3.12 suite passes again (42 tests). The Linux results above precede this final bridge-cleanup addition. The original SIGSEGV remains unconfirmed.

  3. deniial00 commented on Oct 28, 2026

    @deniial00
    MemberAuthor

    CI history for the Python workflow (last 200 runs, 2026-10-27 21:45Z to 2026-10-28): four first attempts segfaulted. All passed on rerun, and all failures were in Test (ubuntu-latest, Python 3.12), the only job that installs Qt WebEngine and sets RUSTGIS_TEST_QT=1.

    Run Crash site (Python frame at SIGSEGV)
    40657663134 numpy import inside test_session.py::test_geopandas_round_trip
    40750952515 <frozen _collections_abc> __new__ (pure Python), after 7 tests
    40768155574 first Session(use_saved_secrets=True), test_saved_secrets.py:53
    40770996179 second Session(), test_saved_secrets.py:54
    • The crash site varies and includes pure-Python code and a numpy import. Every crash was after the Qt tests had run. This pattern suggests earlier heap corruption that surfaces at a later allocation, not a fault in Session() itself. [INFERENCE]
    • The first crash (08:33Z) predates the saved-secrets change (Use saved secrets from the CLI and Python #1037, merged 14:06Z), so that test is not the trigger.
    • Roughly 2% of runs fail. The local reproduction attempts so far were about seven runs with the original fixtures, which is too few to distinguish "doesn't reproduce" from "didn't hit it yet".

    Next: get a baseline failure rate by running the original suite repeatedly on Ubuntu 24.04 x86_64. Then run the same loop with the fixture change. Keep core dumps or a gdb backtrace so the native frame is captured.

  4. deniial00 commented on Oct 28, 2026

    @deniial00
    MemberAuthor

    Root cause found and fix verified. This supersedes my two earlier comments: the Qt widget-lifetime issue was real, but it was not the cause, and Session() was never the crashing code.

    Cause. The segfault happens on a Qt WebEngine thread, not the Python main thread. Two core dumps from stress runs on ubuntu-latest (Python 3.12, this job's exact build) show the same faulting stack:

    #3  getenv () from libc.so.6
    #4  ?? () from libgallium-25.2.8-0ubuntu0.24.04.4.so
    #5  driCreateNewScreen3 () from libgallium-25.2.8-0ubuntu0.24.04.4.so
    #6-#9  ?? () from libEGL_mesa.so.0
    #10 ?? () from libQt6WebEngineCore.so.6
    

    WebEngine sets up EGL (Mesa) on its GPU thread, and Mesa reads the environment there. When the main thread adds an environment variable, glibc's setenv can realloc the environment array and free the old one while that getenv is still walking it. The suite has several such writers after the Qt tests start: monkeypatch.setenv("RUSTGIS_CONFIG_DIR", ...) in the saved-secrets tests, numpy's first import (os.putenv("OPENBLAS_MAIN_FREE", ...); the 08:33Z crash was in that import), and pytest's own PYTEST_CURRENT_TEST. The Python frame in each CI log is just where the main thread happened to be, which is why the crash site kept moving. It is also why the crash is rare (about 0.5–2% of runs) and only in the Python 3.12 job, the only one that installs Qt WebEngine.

    Fix. Run the Qt tests with WebEngine's GPU off: the test QApplication gets --webEngineArgs --disable-gpu. The tests never draw the page, so nothing else changes.

    Verification (fork run, ubuntu-latest, Python 3.12, release wheel with test-stores, extras test,geopandas,qt):

    Check Without fix With fix
    gdb breakpoint on driCreateNewScreen3 during the full suite 1 call on a non-main thread in each of 3 cold runs 0 calls in 3 cold runs
    Repro: churn the environment while WebEngine starts (30 cold runs) 17 SIGSEGV, all in getenv (15 under Mesa EGL setup, one with the exact CI stack) 0
    Full Qt-enabled suite, cold runs — 100 of 100 passed

    The core dumps came from an earlier stress run (400 suite runs, 2 crashes). Residual risk: two of the 17 repro crashes were in other getenv callers (glibc's resolver and WebEngine itself). The GPU-off arm had none of either in 30 runs, but setting environment variables while WebEngine threads run is not safe in general.

    The fix also keeps a smaller change that is unrelated to the crash: the Qt test fixtures now destroy each widget and its unparented message bridge at teardown, instead of leaving them to Python's cyclic garbage collector.

  5. removed
    unconfirmed possible issue
    on Oct 28, 2026
  6. changed the title [-][Bug]: Python Qt-enabled test suite segfaults while constructing a Session[/-] [+][Bug]: Qt-enabled Python tests segfault in getenv on Qt WebEngine's GPU thread[/+] on Oct 28, 2026
  7. added
    bug
    on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions