Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ThreadWorker): don't unload modules without __loader__
Browse files Browse the repository at this point in the history
DuckDB (and likely other packages) manually add their submodules (e.g. duckdb.duckdb.functional) during side-effects when the "parent" (e.g. duckdb.duckdb) is loaded. Since extension modules can't be reloaded, it is impossible to re-run these side-effects to recreate these modules. This commit preserves modules without the __loader__ attribute (which is added by Python's normal import machinery), sacrificing isolation for compatibility.
MHajoha committed Jan 13, 2025
1 parent b8b9180 commit 26d5281
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions questionpy_server/worker/impl/thread.py
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ def run(self) -> None:
# is only for testing anyway, it'll do for now.
original_path = sys.path.copy()
original_module_names = set(sys.modules.keys())
original_path_importer_cache = sys.path_importer_cache.copy()

connection = WorkerToServerConnection(self._pipe.right, self._pipe.right)
manager = WorkerManager(connection)
@@ -50,7 +51,15 @@ def run(self) -> None:
self._loop.call_soon_threadsafe(self._end_event.set)

sys.path = original_path
sys.path_importer_cache = original_path_importer_cache
for module_name in sys.modules.keys() - original_module_names:
if getattr(sys.modules[module_name], "__loader__", None) is None:
log.warning(
"Not unloading '%s', as it doesn't have a '__loader__' attribute and was probably added "
"manually.",
sys.modules[module_name].__name__,
)
continue
# Having reset the path, this forces questionpy and any package modules to be reloaded upon next import.
del sys.modules[module_name]

0 comments on commit 26d5281

Please sign in to comment.