Skip to content

Avoid expensive caller stack inspection - #96

Merged
Snuffy2 merged 2 commits into
mainfrom
fix-issue-94-caller-lookup
Aug 8, 2026
Merged

Avoid expensive caller stack inspection#96
Snuffy2 merged 2 commits into
mainfrom
fix-issue-94-caller-lookup

Conversation

@Snuffy2

@Snuffy2 Snuffy2 commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Summary

Avoids a full Python stack inspection for every queued API request, removing a disproportionate recurring CPU cost during normal polling.

What Changed

  • Replaced inspect.stack() caller lookup with a direct frame lookup.
  • Preserved the Unknown diagnostic fallback when the caller frame is unavailable.
  • Added regression assertions for exact caller labels and fallback behavior.

Why

The former lookup walks every stack frame and triggers costly source/module resolution despite the label being used only for diagnostics. The queue call depth is fixed, so a direct frame lookup preserves the label without that work.

Related Issues

Closes #94

Summary by CodeRabbit

  • Bug Fixes

    • Improved caller identification for queue requests.
    • Preserved the "Unknown" fallback when caller information is unavailable.
  • Tests

    • Updated coverage for successful caller detection and unavailable caller-frame scenarios.

Greptile Summary

This PR replaces full stack inspection with direct frame lookup for diagnostic caller labels while preserving the "Unknown" fallback.

  • Uses sys._getframe(3) to avoid repeated full-stack traversal.
  • Adds exact caller-label assertions and covers both supported frame-lookup failures.
  • Restores explanatory comments documenting the assertions’ fixed-depth sensitivity.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
aiopnsense/client_queue.py Replaces inspect.stack() with fixed-depth sys._getframe() lookup and updates the unavailable-frame fallback exceptions.
tests/test_client_queue.py Adds exact caller-label regression checks, preserves comments explaining frame-depth sensitivity, and parameterizes fallback coverage.

Reviews (2): Last reviewed commit: "test: cover queue caller frame fallbacks" | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The queue now resolves caller names through direct frame access instead of full stack inspection. Tests verify exact caller names and "Unknown" fallbacks when frame lookup raises ValueError.

Changes

Queue caller lookup

Layer / File(s) Summary
Direct caller frame lookup
aiopnsense/client_queue.py
Replaces inspect.stack() with sys._getframe(3).f_code.co_name and updates fallback handling for ValueError and AttributeError.
Caller lookup test coverage
tests/test_client_queue.py
Requires exact caller names for _get and _post, and mocks frame lookup failures to verify the "Unknown" fallback.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The implementation satisfies issue [#94] by using direct frame lookup and preserving the Unknown fallback with regression tests.
Out of Scope Changes check ✅ Passed All changes are limited to caller lookup optimization and related regression tests for issue [#94].
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: replacing expensive caller stack inspection with direct frame lookup.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  aiopnsense
  client_queue.py
Project Total  

This report was generated by python-coverage-comment-action

Comment thread tests/test_client_queue.py

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_client_queue.py`:
- Around line 324-341: Add AttributeError coverage for _get_caller_name
alongside the existing ValueError unavailable-frame tests, preferably by
parameterizing the shared cases with both exception types. Ensure each case
configures the mocked sys._getframe to raise the selected exception and verifies
the fallback result remains "Unknown".
- Around line 324-341: Update both _BadSys test doubles in
tests/test_client_queue.py with Google-style class docstrings, and change each
_getframe return annotation from Any to Never or NoReturn since it always raises
ValueError. Add the corresponding typing import if needed, then run the
repository-local .venv checks.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 7ca0424e-9f7f-412f-83e7-23aba078df29

📥 Commits

Reviewing files that changed from the base of the PR and between b08bb21 and e955429.

📒 Files selected for processing (2)
  • aiopnsense/client_queue.py
  • tests/test_client_queue.py

Comment thread tests/test_client_queue.py Outdated
@Snuffy2

Snuffy2 commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: a5b5bda202

ℹ️ 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".

@Snuffy2
Snuffy2 merged commit dc201d2 into main Aug 8, 2026
13 checks passed
@Snuffy2
Snuffy2 deleted the fix-issue-94-caller-lookup branch August 8, 2026 22:56
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]: _get_caller_name() calls inspect.stack() on every queued request — ~8% CPU and 5M+ calls per 2 min from stack/​sys.modules introspection

1 participant