Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(settings): only load MILVUS_ prefixed keys from dotenv to avoid env pollution - #3686

Open
saitejabandaru-in wants to merge 1 commit into
milvus-io:masterfrom
saitejabandaru-in:fix/settings-dotenv-no-pollution
Open

fix(settings): only load MILVUS_ prefixed keys from dotenv to avoid env pollution#3686
saitejabandaru-in wants to merge 1 commit into
milvus-io:masterfrom
saitejabandaru-in:fix/settings-dotenv-no-pollution

Conversation

@saitejabandaru-in

Copy link
Copy Markdown

Resolves #3666.

Description

Currently, pymilvus calls load_dotenv() unconditionally in global namespace when importing pymilvus/settings.py. This causes side effects by loading all key-value pairs from .env (such as app secrets or credentials for other systems) into the process-wide os.environ environment variables, polluting the host application space.

To solve this, this change replaces the unconditional global load_dotenv() with a structured check using dotenv_values(find_dotenv(usecwd=True)) that filters and only injects keys prefixed with MILVUS_ (e.g. MILVUS_URI, MILVUS_CONN_ALIAS) into os.environ if they are not already set.

Changes

  • Updated pymilvus/settings.py to use find_dotenv(usecwd=True) + dotenv_values() and only inject keys beginning with MILVUS_.
  • Added a new unit test suite in tests/unit/test_settings.py checking that MILVUS_ keys are loaded, while non-Milvus keys (like MY_APP_SECRET) remain untouched in the host environment.

@sre-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: saitejabandaru-in
To complete the pull request process, please assign xuanyang-cn after the PR has been reviewed.
You can assign the PR to them by writing /assign @xuanyang-cn in a comment when ready.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sre-ci-robot

Copy link
Copy Markdown

Welcome @saitejabandaru-in! It looks like this is your first PR to milvus-io/pymilvus 🎉

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3e2dd5ff81

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread pymilvus/settings.py Outdated
Comment on lines +6 to +8
dotenv_path = find_dotenv(usecwd=True)
if dotenv_path:
for k, v in dotenv_values(dotenv_path).items():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Respect dotenv's disable flag

In deployments using python-dotenv 1.2+ (allowed by this package's <2.0.0 dependency), setting PYTHON_DOTENV_DISABLED=1 made the previous load_dotenv() call a no-op for third-party packages. This manual find_dotenv/dotenv_values path bypasses that guard, so importing PyMilvus can still read .env and inject MILVUS_ variables in environments that explicitly disabled dotenv loading. Please gate this block on the same disable flag before reading the file.

Useful? React with 👍 / 👎.

Comment thread pymilvus/settings.py Outdated
Comment on lines +9 to +10
if k.startswith("MILVUS_") and k not in os.environ:
os.environ[k] = v

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Skip valueless dotenv entries

When a .env contains a valueless Milvus placeholder such as MILVUS_URI (a syntax python-dotenv parses as None), this assignment raises TypeError: str expected, not NoneType during import pymilvus. The old load_dotenv() path skipped None values, so this change makes the package unimportable for those .env files; check v is not None before assigning to os.environ.

Useful? React with 👍 / 👎.

@saitejabandaru-in
saitejabandaru-in force-pushed the fix/settings-dotenv-no-pollution branch from 3e2dd5f to 900189a Compare July 14, 2026 11:12
@mergify mergify Bot added dco-passed and removed needs-dco labels Jul 14, 2026
@saitejabandaru-in

Copy link
Copy Markdown
Author

Hi, I have updated the PR to address Codex feedback:

  1. Gated the dotenv loading check on PYTHON_DOTENV_DISABLED != "1".
  2. Added a check v is not None to prevent errors from valueless environment variable declarations in .env.
  3. Signed-off the commits to satisfy the DCO requirements.

@saitejabandaru-in

Copy link
Copy Markdown
Author

/assign @XuanYang-cn

@saitejabandaru-in

Copy link
Copy Markdown
Author

I have checked the codebase and both fixes have been applied:

  1. Respecting .
  2. Skipping valueless dotenv entries ().
    Thanks for the review!

@saitejabandaru-in

Copy link
Copy Markdown
Author

Both fixes have been verified/applied:

  1. Respecting PYTHON_DOTENV_DISABLED.
  2. Skipping valueless dotenv entries with v is not None.

Thanks for the review!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: load_dotenv() in pymilvus/settings.py causes global environment pollution at import time

3 participants