Skip to content

fix: avoid globally monkeypatching Requests - #1256

Merged
Carter Tinney (cartertinney) merged 5 commits into
mainfrom
agents/edge-hsm-private-session
Sep 2, 2026
Merged

fix: avoid globally monkeypatching Requests#1256
Carter Tinney (cartertinney) merged 5 commits into
mainfrom
agents/edge-hsm-private-session

Conversation

@cartertinney

@cartertinney Carter Tinney (cartertinney) commented Sep 1, 2026

Copy link
Copy Markdown
Member

Summary

  • remove the import-time requests_unixsocket.monkeypatch() call
  • use a context-managed Unix-socket session for each Edge HSM trust-bundle and signing request
  • close every session at the end of its operation, independent of client shutdown
  • preserve existing HTTP behavior without replacing process-wide Requests helpers

Testing

  • uv run --frozen pytest tests/unit/iothub/test_edge_hsm.py tests/unit/iothub/test_sync_clients.py tests/unit/iothub/aio/test_async_inbox.py tests/unit/iothub/aio/test_async_clients.py tests/unit/iothub/test_inbox_manager.py -W error::RuntimeWarning -q (1564 passed, 6 skipped)
  • regression test reloads the Edge HSM module and verifies requests_unixsocket.monkeypatch() is not called
  • tests verify both HSM operations exit their session context
  • targeted Ruff and Black checks

Give each Edge HSM signing mechanism its own Unix-socket session so importing the SDK no longer replaces process-wide Requests helpers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cartertinney Carter Tinney (cartertinney) changed the title edge: avoid globally monkeypatching Requests fix: avoid globally monkeypatching Requests Sep 1, 2026

Copilot AI 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.

🟡 Changes recommended

The persistent session lacks deterministic cleanup, and the import-time regression is not directly tested.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Replaces global Requests monkeypatching with per-instance Unix-socket sessions for Edge HSM communication.

Changes:

  • Creates a private requests_unixsocket.Session.
  • Routes trust-bundle and signing requests through that session.
  • Updates unit-test mocks for session-based requests.
File summaries
File Description
edge_hsm.py Introduces and uses the private session.
test_edge_hsm.py Updates request mocks and tests session creation.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread azure-iot-device/azure/iot/device/iothub/edge_hsm.py Outdated
Comment thread tests/unit/iothub/test_edge_hsm.py Outdated
Close private Unix-socket sessions during sync and async client shutdown and on Edge client construction failures. Add direct regression coverage ensuring module import never invokes the global Requests monkeypatch.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use context-managed Unix-socket sessions for each trust-bundle and signing request so cleanup does not depend on the client shutdown lifecycle.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cartertinney

Copy link
Copy Markdown
Member Author

Follow-up: revised the cleanup design in be77e2d. Each Edge HSM operation now owns a context-managed Unix-socket Session, so cleanup is deterministic even if the SDK client is never shut down. The client-lifecycle ownership plumbing added in the prior review fix has been removed.

Copilot AI 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.

🟢 Approval recommended

The scoped-session implementation is correct, preserves existing request behavior, and has focused regression coverage.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@cartertinney

Copy link
Copy Markdown
Member Author

/azp run Python E2E

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@cartertinney

Copy link
Copy Markdown
Member Author

/azp run Python E2E

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Document why the import regression test must resolve classes through the reloaded module.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cartertinney
Carter Tinney (cartertinney) merged commit e3dd43e into main Sep 2, 2026
41 checks passed
@cartertinney
Carter Tinney (cartertinney) deleted the agents/edge-hsm-private-session branch September 2, 2026 19:52
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.

3 participants