Skip to content

Commit

Permalink
fix(ThreadWorker): don't unload modules without __loader__ (backport)
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.
  • Loading branch information
MHajoha committed Jan 14, 2025
1 parent 3ae1fcc commit 4cbf495
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions questionpy_server/worker/impl/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def run(self) -> None:

sys.path = original_path
for module_name in sys.modules.keys() - original_module_names:
if getattr(sys.modules[module_name], "__loader__", None) is None:
log.debug(
"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]

Expand Down

0 comments on commit 4cbf495

Please sign in to comment.