Skip to content

Fix orphaned namespace children purged by DependencyManager - #1956

Open
Allen (Allen5288) wants to merge 3 commits into
Azure:devfrom
Allen5288:fix-dependency-orphan-1906
Open

Allen (Allen5288) wants to merge 3 commits into
Azure:devfrom
Allen5288:fix-dependency-orphan-1906

Conversation

@Allen5288

Copy link
Copy Markdown

Fixes #1906

Summary

DependencyManager._remove_module_cache() purged the google namespace package but left the regular subpackage google.protobuf cached, because a namespace package's __path__ is dynamic (_NamespacePath re-computes against the just-mutated sys.path) while a regular package has a static __file__. A later import google.protobuf then took the sys.modules fast path without re-binding the child onto a fresh parent, so google.protobuf.__version__ raised AttributeError and any function importing a Google Cloud client failed to load.

The purge now also evicts modules orphaned by it: any remaining module whose parent package was evicted by this purge (or was already absent) is evicted too, to a fixpoint, so it is re-imported and re-bound from the current sys.path on next use. The fix is generic — no package is special-cased. Builtin and azure_functions_worker modules are still never evicted, and deliberately aliased modules (whose __name__ differs from their sys.modules key) are left alone.

How did you test this change?

  • Standalone repro with two fake dependency trees driving the real DependencyManager.prioritize_customer_dependencies(): before the fix google.protobuf.__version__ raised AttributeError and import google.api_core failed; after the fix both succeed with version 4.25.5.
  • 4 new unit tests in workers/tests/unittests/test_utilities_dependency.py (direct purge, end-to-end prioritize, unrelated-path guard, pre-existing-orphan repair). test_utilities_dependency.py: 51 passed, 1 skipped. With only the source change stashed, 3 of the 4 new tests fail.
  • flake8 clean. E2E tests need Core Tools + host and must run in CI.

Note: workers/proxy_worker/utils/dependency.py carries the same purge logic and the same flaw; it is intentionally left out of this PR and can follow the same fix.

Pull Request Checklist

Host-Worker Contract

  • Does this PR impact the host-worker contract (e.g., gRPC messages, shared interfaces)?
  • If yes, have the changes been applied to:
  • azure_functions_worker (Python <= 3.12)
  • proxy_worker (Python 3.13-3.14)
  • r2p2 (Python >= 3.15)
  • If no, please explain why: No — the change is internal to the worker's dependency module cache handling; no gRPC messages or shared interfaces change.

Worker Execution Logic

  • Does this PR affect worker execution logic (e.g., function invocation, bindings, lifecycle)?
    If yes, please answer the following:
    Python Version Coverage
  • Does this change apply to all supported worker models?
  • If yes, have the changes been made to:
  • azure_functions_worker (Python <= 3.12)
  • proxy_worker (Python 3.13-3.14)
  • r2p2 (Python >= 3.15)
  • runtimes/v1 / runtimes/v2 (Python >= 3.13)
  • If no, please explain why: Only azure_functions_worker is changed in this PR. proxy_worker carries the same purge logic and the same flaw and is intentionally left out to follow with the same fix (see the note above).
    Programming Model Compatibility (proxy_worker and r2p2)
  • Does this change apply to both:
  • V1 programming model (runtimes/v1)?
  • V2 programming model (runtimes/v2)?
  • Explanation (if limited to one model): Not applicable — this PR does not change proxy_worker or r2p2; the fix is confined to azure_functions_worker.

Implement orphan module removal after cache purge.
Fix missing newline at end of file in test_utilities_dependency.py
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DependencyManager purges the google namespace package but keeps google.protobuf, leaving the parent unbound (function load fails)

1 participant