Hi,
I am trying to add pyo3-log in my code but it is resulting in a seg fault.
Here is the rust code
mod executors;
mod io_helpers;
mod request_handler;
mod routers;
mod server;
mod shared_socket;
mod types;
mod web_socket_connection;
// use pyo3_log::{Caching, Logger};
use server::Server;
use shared_socket::SocketHeld;
// pyO3 module
use pyo3::prelude::*;
#[pymodule]
pub fn robyn(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
// the pymodule class to make the rustPyFunctions available
pyo3_log::init();
m.add_class::<Server>()?;
m.add_class::<SocketHeld>()?;
pyo3::prepare_freethreaded_python();
Ok(())
}
and here is the python code
# default imports
import os
import asyncio
from inspect import signature
import multiprocessing as mp
from robyn.events import Events
# custom imports and exports
from .robyn import SocketHeld
from .argument_parser import ArgumentParser
from .responses import static_file, jsonify
from .dev_event_handler import EventHandler
from .processpool import spawn_process
from .log_colors import Colors
from .ws import WS
from .router import Router, MiddlewareRouter, WebSocketRouter
# 3rd party imports and exports
from multiprocess import Process
from watchdog.observers import Observer
mp.allow_connection_pickling()
import logging
FORMAT = '%(levelname)s %(name)s %(asctime)-15s %(filename)s:%(lineno)d %(message)s'
logging.basicConfig(format=FORMAT)
logging.getLogger().setLevel(logging.INFO)
logging.info("Test 1")
class Robyn:
"""This is the python wrapper for the Robyn binaries."""
def start(self, url="127.0.0.1", port=5000):
"""
Starts the server
:param port int: reperesents the port number at which the server is listening
"""
if not self.dev:
processes = []
workers = self.workers
socket = SocketHeld(url, port)
for _ in range(self.processes):
copied_socket = socket.try_clone()
p = Process(
target=spawn_process,
args=(
self.directories,
self.headers,
self.router.get_routes(),
self.middleware_router.get_routes(),
self.web_socket_router.get_routes(),
self.event_handlers,
copied_socket,
workers,
),
)
p.start()
processes.append(p)
print("Press Ctrl + C to stop \n")
try:
for process in processes:
process.join()
except KeyboardInterrupt:
print(f"\n{Colors.BOLD}{Colors.OKGREEN} Terminating server!! {Colors.ENDC}")
for process in processes:
process.kill()
else:
event_handler = EventHandler(self.file_path)
event_handler.start_server_first_time()
print(
f"{Colors.OKBLUE}Dev server initialised with the directory_path : {self.directory_path}{Colors.ENDC}"
)
observer = Observer()
observer.schedule(event_handler, path=self.directory_path, recursive=True)
observer.start()
try:
while True:
pass
finally:
observer.stop()
observer.join()
I was able to identify the problem to this snippet by using comments and print statements.
mp.allow_connection_pickling()
def spawn_process(
directories, headers, routes, middlewares, web_sockets, event_handlers, socket, workers
):
"""
This function is called by the main process handler to create a server runtime.
This functions allows one runtime per process.
:param directories tuple: the list of all the directories and related data in a tuple
:param headers tuple: All the global headers in a tuple
:param routes Tuple[Route]: The routes touple, containing the description about every route.
:param middlewares Tuple[Route]: The middleware router touple, containing the description about every route.
:param web_sockets list: This is a list of all the web socket routes
:param event_handlers Dict: This is an event dict that contains the event handlers
:param socket SocketHeld: This is the main tcp socket, which is being shared across multiple processes.
:param process_name string: This is the name given to the process to identify the process
:param workers number: This is the name given to the process to identify the process
"""
# platform_name = platform.machine()
if sys.platform.startswith("win32") or sys.platform.startswith("linux-cross"):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
else:
# uv loop doesn't support windows or arm machines at the moment
# but uv loop is much faster than native asyncio
import uvloop
uvloop.install()
loop = uvloop.new_event_loop()
asyncio.set_event_loop(loop)
server = Server()
for directory in directories:
route, directory_path, index_file, show_files_listing = directory
server.add_directory(route, directory_path, index_file, show_files_listing)
for key, val in headers:
server.add_header(key, val)
for route in routes:
route_type, endpoint, handler, is_async, number_of_params = route
server.add_route(route_type, endpoint, handler, is_async, number_of_params)
for route in middlewares:
route_type, endpoint, handler, is_async, number_of_params = route
server.add_middleware_route(route_type, endpoint, handler, is_async, number_of_params)
if "startup" in event_handlers:
server.add_startup_handler(event_handlers[Events.STARTUP][0], event_handlers[Events.STARTUP][1])
if "shutdown" in event_handlers:
server.add_shutdown_handler(event_handlers[Events.SHUTDOWN][0], event_handlers[Events.SHUTDOWN][1])
for endpoint in web_sockets:
web_socket = web_sockets[endpoint]
print(web_socket.methods)
server.add_web_socket_route(
endpoint,
web_socket.methods["connect"],
web_socket.methods["close"],
web_socket.methods["message"],
)
try:
server.start(socket, workers)
loop = asyncio.get_event_loop()
loop.run_forever()
except KeyboardInterrupt:
loop.close()
I am getting the following error code:
๐ฆ Built wheel for CPython 3.10 to /var/folders/4k/tfv95dys49s5syt_t24swgvh0000gn/T/.tmpslmZ2C/robyn-0.16.3-cp310-cp310-macosx_10_7_x86_64.whl
๐ Installed robyn-0.16.3
[1] 65284 segmentation fault python3 integration_tests/base_routes.py
and I am using maturin as the development system.
Do you happen to know a fix for this?