modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo
Other
22.35k stars 2.55k forks source link

[BUG] Python Interop - Seg fault from Mojo using pickle library in custom Python module. #1993

Open johnsoez4 opened 3 months ago

johnsoez4 commented 3 months ago

Bug description

I wrote a custom Python module to serialize a Python dictionary, instantiated in Mojo, using the 'pickle' class from Python's standard library. Running the Python module's 'main' function shows that the code works in the Python environment. When running the Mojo module's 'main' function, the custom method converts the dictionary into a serialized string and returns it to the calling function in Mojo, then crashes after Mojo successfully prints out the returned string. Commenting out the print statement has no effect...Mojo still crashes.

When I comment out the call 'to_pickle' and assign a valid serialized string, the deserialization function 'from_pickle' works properly and Mojo does not crash. See attached below for two source code files and the output from Mojo when it crashes after the 'to_pickle' function.

Mojo 'to_pickle': var py_object_hex: String = py_module.to_pickle(py_dict.py_object) Python

def to_pickle(py_object: Any) -> str:
    return pickle.dumps(py_object).hex().__str__()

Mojo 'from_pickle': var py_object = py_module.from_pickle(py_object_hex) Python

def from_pickle(py_object_hex: str) -> Any:
       return pickle.loads(bytes.fromhex(py_object_hex))

Steps to reproduce

Source Code: https://github.com/johnsoez4/py_pickle Files:

System information

modular 0.5.2 (6b3a04fd)
mojo 24.1.1 (0ab415f7)
VS Code 1.87.2 (details pasted far below)

OS: Ubuntu 23.10
# System Details Report
---

## Report details
- **Date generated:** 2024-03-22 13:41:32

## Hardware Information:
- **Hardware Model:** Lenovo Lenovo YOGA 730-15IKB
- **Memory:** 16.0 GiB
- **Processor:** Intel® Core™ i7-8550U × 8
- **Graphics:** Intel® UHD Graphics 620 (KBL GT2)
- **Graphics 1:** NVIDIA GeForce GTX 1050
- **Disk Capacity:** 512.1 GB

## Software Information:
- **Firmware Version:** 7KCN33WW(V1.14)
- **OS Name:** Ubuntu 23.10
- **OS Build:** (null)
- **OS Type:** 64-bit
- **GNOME Version:** 45.2
- **Windowing System:** X11
- **Kernel Version:** Linux 6.5.0-26-generic

IDE: Visual Studio Code
Version: 1.87.2
Commit: 863d2581ecda6849923a2118d93a088b0745d9d6
Date: 2024-03-08T15:14:59.643Z
Electron: 27.3.2
ElectronBuildId: 26836302
Chromium: 118.0.5993.159
Node.js: 18.17.1
V8: 11.8.172.18-electron.0
OS: Linux x64 6.5.0-26-generic snap
johnsoez4 commented 3 months ago

So as of Mojo version 24.2, Python.dict() is missing which causes a different error altogether. :disappointed: https://github.com/modularml/mojo/discussions/2201