This is an automated email from the ASF dual-hosted git repository.

zclll pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 06d114b4ce1 [Fix](pyudf) Fix import conflicts for modules with the 
same top-level name (#62167)
06d114b4ce1 is described below

commit 06d114b4ce1b9c2896290a406ecdf9398c6dcdd2
Author: linrrarity <[email protected]>
AuthorDate: Wed Apr 8 20:54:34 2026 +0800

    [Fix](pyudf) Fix import conflicts for modules with the same top-level name 
(#62167)
    
    Related PR: https://github.com/apache/doris/pull/61280
    
    Problem Summary:
    
    When two imported modules share the same top-level package name,
    python_server imports them package by package, and clean up
    `sys.modules` after import, which causes incorrect lock key assignment
    and leads to thread contention or interference between imported states.
    For example, the symbols `pkg.mod.func1` and `pkg.mod.func2` may
    conflict when importing `pkg` or `pkg.mod`
    
    ```text
    2026-04-07 16:35:30.108 ERROR [suite-thread-1] (ScriptContext.groovy:122) - 
Run test_pythonudtf_data_types_module in 
/mnt/disk7/linzhenqi/dv/doris/regression-test/suites/pythonudtf_p0/test_pythonudtf_data_types_module.groovy
 failed
    java.sql.SQLException: errCode = 2, detailMessage = 
(127.0.0.1)[RUNTIME_ERROR]Flight stream finish failed with message: 
'pyudtf_module'. Detail: Python exception: Traceback (most recent call last):
      File "pyarrow/_flight.pyx", line 2315, in pyarrow._flight._do_exchange
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 2499, in do_exchange
        self._handle_exchange_udtf(python_udf_meta, reader, writer)
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 2316, in _handle_exchange_udtf
        adaptive_udtf = loader.load()
                        ^^^^^^^^^^^^^
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 877, in load
        func = self.load_udf_from_module(location, package_name, module_name, 
func_name)
               
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 1135, in load_udf_from_module
        return self._load_package_udf(
               ^^^^^^^^^^^^^^^^^^^^^^^
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 1088, in _load_package_udf
        udf_module = self._get_or_import_module(location, full_module_name)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 978, in _get_or_import_module
        module = importlib.import_module(full_module_name)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File 
"/mnt/disk7/linzhenqi/miniconda3/envs/py312/lib/python3.12/importlib/__init__.py",
 line 90, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1310, in 
_find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 488, in 
_call_with_frames_removed
      File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1331, in 
_find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 946, in _load_unlocked
    KeyError: 'pyudtf_module'
    
    ...
    
    2026-04-07 16:35:30.169 ERROR [suite-thread-6] (ScriptContext.groovy:122) - 
Run test_pythonudtf_exceptions_module in 
/mnt/disk7/linzhenqi/dv/doris/regression-test/suites/pythonudtf_p0/test_pythonudtf_exceptions_module.groovy
 failed
    java.sql.SQLException: errCode = 2, detailMessage = 
(127.0.0.1)[RUNTIME_ERROR]Flight stream finish failed with message: 
'pyudtf_module'. Detail: Python exception: Traceback (most recent call last):
      File "pyarrow/_flight.pyx", line 2315, in pyarrow._flight._do_exchange
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 2499, in do_exchange
        self._handle_exchange_udtf(python_udf_meta, reader, writer)
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 2316, in _handle_exchange_udtf
        adaptive_udtf = loader.load()
                        ^^^^^^^^^^^^^
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 877, in load
        func = self.load_udf_from_module(location, package_name, module_name, 
func_name)
               
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 1135, in load_udf_from_module
        return self._load_package_udf(
               ^^^^^^^^^^^^^^^^^^^^^^^
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 1088, in _load_package_udf
        udf_module = self._get_or_import_module(location, full_module_name)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File 
"/mnt/disk7/linzhenqi/dv/doris/output/be/plugins/python_udf/python_server.py", 
line 978, in _get_or_import_module
        module = importlib.import_module(full_module_name)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File 
"/mnt/disk7/linzhenqi/miniconda3/envs/py312/lib/python3.12/importlib/__init__.py",
 line 90, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1337, in 
_find_and_load_unlocked
    KeyError: 'pyudtf_module'
    ```
---
 be/src/udf/python/python_server.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/be/src/udf/python/python_server.py 
b/be/src/udf/python/python_server.py
index b259a80d89f..d16fc352178 100644
--- a/be/src/udf/python/python_server.py
+++ b/be/src/udf/python/python_server.py
@@ -835,13 +835,17 @@ class ModuleUDFLoader(UDFLoader):
     @classmethod
     def _get_import_lock(cls, module_name: str) -> threading.Lock:
         """
-        Get or create a reentrant lock for the given module name.
+        Get or create an import lock for the given module namespace.
 
         Uses double-checked locking pattern for optimal performance:
         - Fast path: return existing lock without acquiring global lock
         - Slow path: create new lock under global lock protection
         """
-        cache_key = module_name
+        # Lock by top-level package to avoid concurrent imports mutating shared
+        # parent entries in sys.modules. If we lock by full module name 
instead,
+        # pkg.mod.func1 and pkg.mod.func2 can import in parallel and race while
+        # initializing pkg/pkg.mod, causing flaky import failures (for example 
KeyError).
+        cache_key = module_name.split(".", 1)[0]
 
         # Fast path: check without lock (read-only, safe for most cases)
         if cache_key in cls._import_locks:


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to