Skip to content

Commit 6f36f99

Browse files
committed
Fix segfault in RawTableProvider by keeping SessionContext alive
The FFI_TableProvider stores a Weak reference to the TaskContextProvider, but the Arc was created locally and dropped immediately. Use a static OnceLock to keep it alive. Also update the __datafusion_table_provider__ signature for the v52 protocol and bump datafusion dependency to >=52.0.0.
1 parent ba95abc commit 6f36f99

3 files changed

Lines changed: 482 additions & 511 deletions

File tree

python/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ version = "0.1.0"
88
description = "Build and run queries against data"
99
readme = "../README.md"
1010
license = { file = "../LICENSE" }
11-
requires-python = ">=3.9"
11+
requires-python = ">=3.10"
1212
keywords = ["datafusion", "dataframe", "rust", "query-engine"]
1313
classifier = [
1414
"Development Status :: 2 - Pre-Alpha",
@@ -24,7 +24,7 @@ classifier = [
2424
"Programming Language :: Python",
2525
"Programming Language :: Rust",
2626
]
27-
dependencies = ["datafusion>=45.0.0"]
27+
dependencies = ["datafusion>=52.0.0"]
2828

2929
[project.urls]
3030
repository = "https://github.com/datafusion-contrib/datafusion-table-providers"

python/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@ pub(crate) fn get_tokio_runtime() -> &'static tokio::runtime::Runtime {
1919
RUNTIME.get_or_init(|| tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime"))
2020
}
2121

22+
/// Returns a static reference to a default SessionContext.
23+
/// This ensures the Arc backing the Weak reference inside
24+
/// FFI_TaskContextProvider is never dropped.
25+
fn get_default_session_context() -> &'static Arc<SessionContext> {
26+
static CTX: OnceLock<Arc<SessionContext>> = OnceLock::new();
27+
CTX.get_or_init(|| Arc::new(SessionContext::default()))
28+
}
29+
2230
#[pymethods]
2331
impl RawTableProvider {
2432
fn __datafusion_table_provider__<'py>(
2533
&self,
2634
py: Python<'py>,
35+
_session: Bound<'py, PyAny>,
2736
) -> PyResult<Bound<'py, PyCapsule>> {
2837
let name = CString::new("datafusion_table_provider").unwrap();
2938

@@ -33,7 +42,7 @@ impl RawTableProvider {
3342
None
3443
};
3544

36-
let ctx = Arc::new(SessionContext::default()) as Arc<dyn TaskContextProvider>;
45+
let ctx = Arc::clone(get_default_session_context()) as Arc<dyn TaskContextProvider>;
3746
let provider = FFI_TableProvider::new(
3847
Arc::clone(&self.table),
3948
self.supports_pushdown_filters,

0 commit comments

Comments
 (0)