Skip to content

Commit 598af50

Browse files
authored
fix: make sure pyo3 runtime is initialized before all others (#1063)
1 parent dbb7da5 commit 598af50

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

python/cocoindex/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Cocoindex is a framework for building and running indexing pipelines.
33
"""
44

5+
from . import _engine # type: ignore
56
from . import functions, sources, targets, cli, utils
67

78
from . import targets as storages # Deprecated: Use targets instead
@@ -42,6 +43,8 @@
4243
Json,
4344
)
4445

46+
_engine.init_pyo3_runtime()
47+
4548
__all__ = [
4649
# Submodules
4750
"_engine",

src/lib_context.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ impl FlowContext {
162162
static TOKIO_RUNTIME: LazyLock<Runtime> = LazyLock::new(|| Runtime::new().unwrap());
163163
static AUTH_REGISTRY: LazyLock<Arc<AuthRegistry>> = LazyLock::new(|| Arc::new(AuthRegistry::new()));
164164

165+
pub fn get_runtime() -> &'static Runtime {
166+
&TOKIO_RUNTIME
167+
}
168+
pub fn get_auth_registry() -> &'static Arc<AuthRegistry> {
169+
&AUTH_REGISTRY
170+
}
171+
165172
type PoolKey = (String, Option<String>);
166173
type PoolValue = Arc<tokio::sync::OnceCell<PgPool>>;
167174

@@ -271,21 +278,10 @@ impl LibContext {
271278
}
272279
}
273280

274-
pub fn get_runtime() -> &'static Runtime {
275-
&TOKIO_RUNTIME
276-
}
277-
278-
pub fn get_auth_registry() -> &'static Arc<AuthRegistry> {
279-
&AUTH_REGISTRY
280-
}
281-
282281
static LIB_INIT: OnceLock<()> = OnceLock::new();
283282
pub async fn create_lib_context(settings: settings::Settings) -> Result<LibContext> {
284283
LIB_INIT.get_or_init(|| {
285284
let _ = env_logger::try_init();
286-
287-
pyo3_async_runtimes::tokio::init_with_runtime(get_runtime()).unwrap();
288-
289285
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
290286
});
291287

src/py/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ fn set_settings_fn(get_settings_fn: Py<PyAny>) -> PyResult<()> {
115115
Ok(())
116116
}
117117

118+
#[pyfunction]
119+
fn init_pyo3_runtime() {
120+
pyo3_async_runtimes::tokio::init_with_runtime(get_runtime()).unwrap();
121+
}
122+
118123
#[pyfunction]
119124
fn init(py: Python<'_>, settings: Pythonized<Option<Settings>>) -> PyResult<()> {
120125
py.allow_threads(|| -> anyhow::Result<()> {
@@ -666,6 +671,7 @@ fn seder_roundtrip<'py>(
666671
#[pymodule]
667672
#[pyo3(name = "_engine")]
668673
fn cocoindex_engine(m: &Bound<'_, PyModule>) -> PyResult<()> {
674+
m.add_function(wrap_pyfunction!(init_pyo3_runtime, m)?)?;
669675
m.add_function(wrap_pyfunction!(init, m)?)?;
670676
m.add_function(wrap_pyfunction!(set_settings_fn, m)?)?;
671677
m.add_function(wrap_pyfunction!(start_server, m)?)?;

0 commit comments

Comments
 (0)