Skip to content

MemoryError: Parser stack overflowed when running Flask 3.1 with Python 3.14 on Unikraft Catalog #257

@krpandrei05

Description

@krpandrei05

Description

I encountered a MemoryError: Parser stack overflowed - Python source too complex to parse when attempting to run the Flask 3.1 with Python 3.14 and SQLite3 example (flask3.1-python3.14-sqlite3-base) from the catalog on the QEMU x86_64 platform. The application starts successfully, but crashes when attempting to render Jinja2 templates.

Note: The Flask 3.1 with Python 3.14 example without SQLite3 (flask3.1-python3.14-base) works correctly. This issue is specific to the SQLite3 version which uses Jinja2 templates.

The issue can be reproduced by using the following exact commands:

cd examples/flask3.1-python3.14-sqlite3-base
kraft build
kraft run --rm -p 8080:8080 --plat qemu --arch x86_64 -M 1024M .

Then, in another terminal:

curl localhost:8080

The application crashes with the following error:

Exception occurred during processing of request from ('10.0.2.2', 48602)

Traceback (most recent call last):
  File "/usr/local/lib/python3.14/site-packages/jinja2/lexer.py", line 654, in wrap
    .decode("unicode-escape")
     ~~~~~~^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.14/encodings/__init__.py", line 99, in search_function
    mod = __import__('encodings.' + modname, fromlist=_import_tail,
                     level=0)
MemoryError: Parser stack overflowed - Python source too complex to parse

Root Cause Analysis

The issue appears to be related to Python 3.14's new stack protection mechanisms being incompatible with Unikraft's elfloader stack switching. When Jinja2 attempts to parse templates, Python 3.14's stack overflow detection triggers, causing the parser to fail even though there is sufficient stack space allocated.

Attempted Solutions

  1. Increased stack size: Modified Kraftfile to set CONFIG_APPELFLOADER_STACK_NBPAGES from 128 (512KB) to 1024 (4MB), but the error persists.

  2. Disabled stack checks: Added CONFIG_LIBPOSIX_ENVIRON_ENVP4: "PYTHONNOSTACKCHECK=1" to disable Python 3.14's stack overflow checks, but the error still occurs.

  3. Increased memory: Used -M 1024M flag in kraft run, but this only affects total memory, not stack size.

Observations

  • The Flask 3.1 with Python 3.14 example without SQLite3 (flask3.1-python3.14-base) works correctly and does not exhibit this issue.
  • The same Flask application with Python 3.12 works correctly with SQLite3 and Jinja2 templates (flask3.0-python3.12-sqlite3-base).
  • The issue is specific to the SQLite3 version (flask3.1-python3.14-sqlite3-base) which uses Jinja2 templates.
  • The problem manifests when Jinja2 attempts to parse templates, triggering Python 3.14's stack overflow detection on Unikraft.

Environment Information

  • Operating System (Host): Linux (WSL2)
  • Target Platform: QEMU/x86_64
  • Failing Example: examples/flask3.1-python3.14-sqlite3-base
  • Python Version: 3.14
  • Flask Version: 3.1
  • KraftKit Version: 0.12.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions