sagemathinc / cowasm

CoWasm: Collaborative WebAssembly for Servers and Browsers. Built using Zig. Supports Python with extension modules, including numpy.
https://cowasm.org
BSD 3-Clause "New" or "Revised" License
482 stars 23 forks source link

strange issue with a static variable #27

Closed williamstein closed 1 year ago

williamstein commented 1 year ago

In packages/cpython/build/wasm/Python/ceval.c there is a function

int
_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
{
    assert(is_tstate_valid(tstate));
    /* The caller must hold the GIL */
    assert(PyGILState_Check());

    static int reentrant = 0;
    if (reentrant) {
        _PyErr_SetString(tstate, PyExc_RuntimeError, "Cannot install a profile function "
                         "while another profile function is being installed");
        reentrant = 0;
        return -1;
    }
    reentrant = 1;
...

It's called the first time when you do this:

~/python-wasm/packages/cpython$ pw-d
Welcome to Node.js v18.9.0.
Type ".help" for more information.
> Python 3.11.0rc1 (main, Sep 14 2022, 08:57:39) [Clang 14.0.6 (git@github.com:ziglang/zig-bootstrap.git 9dd8ca3a7f80f1677f1ad4d5 on wasi
Type "help", "copyright", "credits" or "license" for more information.
>>> import cProfile; pr = cProfile.Profile()
>>> pr.enable()
_PyEval_SetProfile being called and reentrant = 19  // requires editing "ceval.c" to see this
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Cannot install a profile function while another profile function is being installed

For some reason the value of reentrant is initially 19, which can't happen.... unless there is some sort of memory corruption somewhere nearby. So this indicates some sort of subtle bug.