Repository navigation
Description
What happens?
Since 2.0, the check whether pandas (or numpy, pyarrow, polars) is loaded fails when the sys.modules attribute holds a collections.abc.Mapping that is not a dict:
con.from_df(df)andcon.register(name, df)raiseRuntimeError: std::bad_cast;con.sql(...).df()aborts the interpreter:libc++abi: terminating due to uncaught exception of type std::bad_cast, exit code 134.
1.4.1 handles the same setup.
We hit this in a service that runs the Temporal Python SDK.
Its workflow sandbox replaces the sys.modules attribute with a thread-local MutableMapping proxy for the whole process during every workflow activation (temporalio/worker/workflow_sandbox/_importer.py, _ThreadLocalSysModules).
A DuckDB call on another thread (a Temporal activity) that lands in that window fails, so the error is intermittent.
The cause is the strict cast in ModuleIsLoaded<T>():
// src/include/duckdb_python/pyconnection/pyconnection.hpp (v2.0.1, also on main)
auto dict = nb::cast<nb::dict>(nb::module_::import_("sys").attr("modules"));
return dict.contains(nb::str(T::Name));1.4.1 kept the attribute as a generic object (pybind11::module_::import("sys").attr("modules")), so .contains() went through the proxy's __contains__.
Other places in the client already read the interpreter's module dict, which replacing the attribute does not affect: ModuleIsImported in src/native/python_conversion.cpp, ArrowSourcesPart in src/pyconnection.cpp, and src/python_replacement_scan.cpp.
ModuleIsLoaded could do the same:
template <typename T>
static bool ModuleIsLoaded() {
return PyDict_GetItemString(PyImport_GetModuleDict(), T::Name) != nullptr;
}nb::cast<nb::mapping> would also work if you prefer to keep reading the attribute.
I'm happy to open a PR against v2.0-cyanoptera with this change and a regression test in tests/fast/test_nanobind_cutover_regressions.py.
To Reproduce
import sys
from collections.abc import MutableMapping
import duckdb
import pandas as pd
class ModulesView(MutableMapping):
"""A mapping over the real sys.modules, like the one the Temporal workflow sandbox installs."""
def __init__(self, modules):
self._modules = modules
def __getitem__(self, key):
return self._modules[key]
def __setitem__(self, key, value):
self._modules[key] = value
def __delitem__(self, key):
del self._modules[key]
def __iter__(self):
return iter(self._modules)
def __len__(self):
return len(self._modules)
df = pd.DataFrame({"x": [1, 2, 3]})
con = duckdb.connect()
print(duckdb.__version__)
real = sys.modules
sys.modules = ModulesView(real)
try:
print(con.from_df(df).fetchall())
finally:
sys.modules = realOutput on 2.0.1:
2.0.1
Traceback (most recent call last):
File "repro.py", line 37, in <module>
print(con.from_df(df).fetchall())
~~~~~~~~~~~^^^^
File ".../duckdb/_relation.py", line 4411, in _connection_from_df
return from_df(self, df)
File ".../duckdb/_relation.py", line 4321, in from_df
handle = _seam.register_source(con, df)
File ".../duckdb/_seam.py", line 164, in register_source
return con._register_source(obj, name)
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
RuntimeError: std::bad_cast
Output on 1.4.1:
1.4.1
[(1,), (2,), (3,)]
Replacing con.from_df(df).fetchall() with con.sql("SELECT 42 AS x").df() aborts the process on 2.0.1:
libc++abi: terminating due to uncaught exception of type std::bad_cast: std::bad_cast
OS:
macOS 27.2.1, arm64 (also seen on Linux containers in a deployment)
DuckDB Package Version:
2.0.1 (also fails on 2.0.0 and 2.1.0.dev2611161157; 1.4.1 works)
Python Version:
3.15.0 (also reproduced on 3.13)
Full Name:
Stanislav Serdyukov
Affiliation:
Independent
What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.
I have tested with a nightly build
Did you include all relevant data sets for reproducing the issue?
Not applicable - the reproduction does not require a data set
Did you include all code required to reproduce the issue?
- Yes, I have
Did you include all relevant configuration to reproduce the issue?
- Yes, I have
Activity