Distributive-Network / PythonMonkey

A Mozilla SpiderMonkey JavaScript engine embedded into the Python VM, using the Python engine to provide the JS host environment.
https://pythonmonkey.io
Other
830 stars 37 forks source link

exit with code -1073741819 (0xC0000005) #271

Open hjdhnx opened 6 months ago

hjdhnx commented 6 months ago

Issue type

Bug

How did you install PythonMonkey?

Installed from pip https://nightly.pythonmonkey.io/pythonmonkey/

OS platform and distribution

windows11

Python version (python --version)

3.8.8 64bit

PythonMonkey version (pip show pythonmonkey)

0.3.1.dev83+b2be8c9

Bug Description

When I use PythonMonkey in interface services, such as FastAPI, as long as I load a JavaScript module and use it as a Python variable, when I call the method of this variable again, the entire interface backend service process will be forced to exit

from pythonmonkey import eval as js_eval, require as js_require
from pathlib import Path
drpy_file = Path(os.path.join(BASE_DIR, './t4/files/drpy3_libs/drpy3.min.js')).as_posix()
DRPY = js_require(drpy_file)
@router.get(api_url + '_test', summary="t")
def vod_test():
    # http://localhost:5707/api/v1/vod_test
    api_path = get_api_path('996影视.js')
    api_text = get_file_text(api_path)
    DRPY.init(api_text) # this line will be exited
    api_text += f'\nvar DRPY = {DRPY}'
    return Response(api_text, media_type='text/javascript; charset=utf-8')

look here

image

Standalone code to reproduce the issue

drpy.js

function init(text) {
    console.log('init:', text)
    return text
}
function home(text) {
    console.log('home:', text)
    return text
}
module.exports = {
    init,
    home,
}

drpy_run.py

import pythonmonkey as pm
from pythonmonkey import eval as js_eval, require as js_require

if __name__ == '__main__':
    DRPY = js_require('./drpy.js')  # it's ok
    print(DRPY)
    print(DRPY.init('hello'))
    print(DRPY.home('world'))

main.py

import pythonmonkey as pm
from pythonmonkey import eval as js_eval, require as js_require

from typing import Union

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    # print(pm.globalThis)
    DRPY = js_require('./drpy.js')  # exit with code -1073741819 (0xC0000005)
    # print(DRPY) # # exit with code -1073741819 (0xC0000005)
    # print(DRPY.init('hello')) # # exit with code -1073741819 (0xC0000005)
    # print(DRPY.home('world')) # # exit with code -1073741819 (0xC0000005)
    return {"pm_version": pm.__version__}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(
        app='main:app',
    )

drpy_run.py tests ok,but main.py tests error when we view this http api, this bug happend http://127.0.0.1:8000 @wesgarland @philippedistributive

Relevant log output or backtrace

No response

Additional info if applicable

No response

What branch of PythonMonkey were you developing on? (If applicable)

No response

wesgarland commented 6 months ago

I suspect PythonMonkey is segfaulting here, likely due to a GC root that isn't being persisted properly. What do you think, @caleb-distributive ?

wesgarland commented 6 months ago

Access violation exceptions (0xc0000005= STATUS_ACCESS_VIOLATION) are generated by modern processors when a memory access caused by an instruction or program execution does not satisfy certain conditions defined by the processor architecture or memory management unit structures.

So this means probably that we tried to access a page which isn't mapped into the process address space.

The most likely address for that is NULL - we're probably de-referencing a NULL ptr the second time through.

wesgarland commented 6 months ago

I just noticed you are on Python 3.8, @caleb-distributive are there important things that don't work under 3.9 that could affect this user?

philippedistributive commented 6 months ago

@hjdhnx please provide the most minimal possible example with all files needed to reproduce

hjdhnx commented 6 months ago

Standalone code to reproduce the issue

see Standalone code to reproduce the issue

philippedistributive commented 6 months ago

@hjdhnx on ubuntu I get INFO: Started server process [345480] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) have you tried on linux? is this happening only on windows?

slonopot commented 1 month ago

I'm getting the same issue on Windows 11 with Python 3.12.2, pythonmonkey 0.7.1, 0.8.0, 0.8.1.dev3+5fc6ce0. Python just crashes when I try to import pythonmonkey or from pythonmonkey.lib import pmdb.

Is there anything I can do to help you debug this further?