#2939·brython

Recursing to the recursion limit in a Web Worker overflows the Javascript stack instead of raising RecursionError

Author: djoumeCreated Sep 10, 2026Updated Sep 10, 2026

Recursing to Brython's own recursion limit inside a Web Worker overflows the Javascript stack instead of raising RecursionError. The same code is fine on the main thread.

Tested environment

Reproduced from upstream source at commit 424e4855f0b7ee26af350a5f0371a3ef3d75fe88, built with python3 scripts/make_dist.py and loaded in a Web Worker with importScripts("brython.js", "brython_stdlib.js"), using Chromium 151.0.7922.34. A Web Worker is required, so unlike a source-mode check this one needs the bundle. The published 3.14.1, 3.14.2 and 3.14.3 bundles behave the same. Expected behavior was compared with CPython 3.14.4 on Linux.

Steps to reproduce — in a worker, __BRYTHON__.runPythonSource(src) with:

python
import sys
print("recursionlimit:", sys.getrecursionlimit())


def flatten(nested):
    if not isinstance(nested, list):
        return [nested]
    result = []
    for item in nested:
        result = result + flatten(item)
    return result


deep = 1
for _ in range(1100):
    deep = [deep]

try:
    flatten(deep)
    print("no RecursionError")
except RecursionError:
    print("RecursionError caught")

Expected (CPython 3.14)

recursionlimit: 1000
RecursionError caught

Actual, in a worker

recursionlimit: 900
RangeError: Maximum call stack size exceeded

On the main thread, the same build prints recursionlimit: 900 and then RecursionError caught.

Cause:

The RecursionError is raised at the right depth — sys.getrecursionlimit() is 900 on this revision and the error fires at exactly 900 frames, measured on the main thread. The overflow happens afterwards, in the handler, while the traceback is being built.

The reason this is not a one-function fix is worth stating plainly. The first frame to blow is $B.make_tb, which recurses once per Python frame. Making $B.make_tb iterative locally does remove that overflow — and the next call that needs stack, $B.exception, overflows in its turn. Each function made iterative moves the failure to the next one, because by the time the limit is reached there is no Javascript stack budget left for the handler at all. The limit itself is the problem, not any particular function along the path, and in a worker the process then dies without reporting anything: no RecursionError, no error event carrying a Python traceback, just a worker that stops.

How much headroom is missing

Same worker, same code, varying sys.setrecursionlimit() before recursing:

limit worker main thread
900 (the default) RangeError ok
700 RangeError ok
650 TypeError: Cannot read properties of undefined (reading 'length') ok
600 ok ok
500 ok ok

So a worker needs the limit at roughly two thirds of the default before RecursionError can be raised and handled. At 650 the failure is not even a clean overflow: the traceback is built from a partially unwound stack and dies differently.

Main thread versus worker, by version

Same page, same browser, same code:

main thread Web Worker
3.14.0 ok ok
3.14.1 ok RangeError
3.14.2 ok RangeError
3.14.3 ok RangeError
master 424e485 ok RangeError

A worker's Javascript stack is smaller than the main thread's, so what changed in 3.14.1 is the margin between the 900-frame limit and the engine's real capacity: 3.14.0 fits inside it, everything after does not.

A worker is the natural place to run Brython, so that a user's loop cannot freeze the page, and there the worker dies without posting a result: the host sees silence rather than an error.

No patch is proposed, deliberately. Chasing the overflow function by function is whack-a-mole for the reason above, and the two real directions — spending less Javascript stack per Python frame, or making the effective limit sensitive to the stack actually available so the handler has room — are design decisions for the project rather than local repairs. Lowering the default outright is the small change, and it does work: a worker survives at 600. But 600 is one number from one browser and one thread type, and hard-coding it would take away main-thread recursion depth that works today.

AI assistance

This report and its reproduction were prepared with AI assistance. The reproduction was rerun against the upstream revision identified above.