@@ -5,6 +5,19 @@ use std::{
55 path:: { Path , PathBuf } ,
66} ;
77
8+ #[ cfg( target_os = "linux" ) ]
9+ use std:: os:: raw:: c_void;
10+
11+ #[ cfg( target_os = "linux" ) ]
12+ unsafe extern "C" {
13+ fn dlopen ( filename : * const i8 , flag : i32 ) -> * mut c_void ;
14+ }
15+
16+ #[ cfg( target_os = "linux" ) ]
17+ const RTLD_GLOBAL : i32 = 0x100 ;
18+ #[ cfg( target_os = "linux" ) ]
19+ const RTLD_NOW : i32 = 0x2 ;
20+
821use bytes:: BytesMut ;
922use http_handler:: { Handler , Request , RequestExt , Response , extensions:: DocumentRoot } ;
1023use pyo3:: exceptions:: PyRuntimeError ;
@@ -35,6 +48,34 @@ pub use websocket::{
3548 WebSocketConnectionScope , WebSocketReceiveMessage , WebSocketSendException , WebSocketSendMessage ,
3649} ;
3750
51+ /// Load Python library with RTLD_GLOBAL on Linux to make symbols available
52+ #[ cfg( target_os = "linux" ) ]
53+ fn ensure_python_symbols_global ( ) {
54+ unsafe {
55+ // Try common Python library names
56+ let python_libs = [
57+ "libpython3.12.so.1.0\0 " ,
58+ "libpython3.11.so.1.0\0 " ,
59+ "libpython3.10.so.1.0\0 " ,
60+ "libpython3.9.so.1.0\0 " ,
61+ "libpython3.8.so.1.0\0 " ,
62+ ] ;
63+
64+ for lib_name in & python_libs {
65+ let handle = dlopen ( lib_name. as_ptr ( ) as * const i8 , RTLD_NOW | RTLD_GLOBAL ) ;
66+ if !handle. is_null ( ) {
67+ // Successfully loaded Python library with RTLD_GLOBAL
68+ break ;
69+ }
70+ }
71+ }
72+ }
73+
74+ #[ cfg( not( target_os = "linux" ) ) ]
75+ fn ensure_python_symbols_global ( ) {
76+ // On non-Linux platforms, this is typically not needed
77+ }
78+
3879/// Core ASGI handler that loads and manages a Python ASGI application
3980pub struct Asgi {
4081 app_function : PyObject ,
@@ -47,6 +88,9 @@ impl Asgi {
4788 docroot : Option < String > ,
4889 app_target : Option < PythonHandlerTarget > ,
4990 ) -> Result < Self , HandlerError > {
91+ // Ensure Python symbols are globally available before initializing
92+ ensure_python_symbols_global ( ) ;
93+
5094 // Determine document root
5195 let docroot = PathBuf :: from ( if let Some ( docroot) = docroot {
5296 docroot
0 commit comments