fix: release the GIL during Python plugin teardown#466
Conversation
Signed-off-by: Will Killian <wkillian@nvidia.com>
WalkthroughThe Python plugin-clearing binding now detaches execution from the Python interpreter. Rust coverage calls the updated signature, and a parameterized observability test verifies sink delivery, dotted-field transformations, prompt clearing, and cleanup. ChangesPlugin clearing and observability
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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 `@python/tests/test_observability_plugin.py`:
- Line 16: Update the test around plugin.clear() to keep the HTTP request
blocked while teardown runs: remove the explicit flush and immediate
allow_response.set(), invoke plugin.clear() while the request remains gated, and
use a delayed Python thread or timer to release the response during the blocked
clear. Apply the same sequencing to the related test block around the other
referenced lines, ensuring the test verifies clear performs the flush and does
not depend on pre-teardown response release.
🪄 Autofix (Beta)
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: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Enterprise
Run ID: 9494d7e8-2d81-4b46-bb46-002bca747b0b
📒 Files selected for processing (3)
crates/python/src/py_plugin.rscrates/python/tests/coverage/py_plugin_coverage_tests.rspython/tests/test_observability_plugin.py
📜 Review details
⏰ Context from checks skipped due to timeout. (1)
- GitHub Check: Check / Run
🧰 Additional context used
📓 Path-based instructions (14)
**/*.rs
📄 CodeRabbit inference engine (.agents/skills/prepare-pr/SKILL.md)
**/*.rs: Any Rust change must runjust test-rust
Any Rust change must runcargo fmt --all
Any Rust change must runcargo clippy --workspace --all-targets -- -D warnings
**/*.rs: Runcargo fmt --allfor all FFI work since it is Rust work
Runjust test-rustto validate FFI changes
Runcargo clippy --workspace --all-targets -- -D warningsto enforce strict linting on FFI workWhen Rust files changed as part of Go work, also run
cargo fmt --all,just test-rust, andcargo clippy --workspace --all-targets -- -D warnings
**/*.rs: Runcargo fmt --allwhen Rust files are changed as part of Node work
Runcargo clippy --workspace --all-targets -- -D warningswhen Rust files are changed as part of Node work
Runjust test-rustwhen Rust files are changed as part of Node workWhen changing the core Rust runtime or Rust-facing API surface, format Rust code with
cargo fmt(rustfmt defaults), keepcargo clippy -- -D warningsclean, and satisfycargo deny checkperdeny.toml.
**/*.rs: If any Rust code changed, always runjust test-rust.
If any Rust code changed, also runcargo fmt --all.
If any Rust code changed, also runcargo clippy --workspace --all-targets -- -D warnings.
For Rust changes headed for review, runcargo fmt --allandcargo clippy --workspace --all-targets -- -D warningseven if relying on pre-commit.
Files:
crates/python/src/py_plugin.rscrates/python/tests/coverage/py_plugin_coverage_tests.rs
**/*.{rs,py}
📄 CodeRabbit inference engine (AGENTS.md)
Follow binding naming conventions in Rust and Python: use
snake_case.
Files:
crates/python/src/py_plugin.rspython/tests/test_observability_plugin.pycrates/python/tests/coverage/py_plugin_coverage_tests.rs
**/*.{rs,py,js,mjs,cjs,ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
**/*.{rs,py,js,mjs,cjs,ts,tsx}: UseJson = serde_json::Valuein Rust-facing runtime APIs where the existing code expects JSON payloads.
UseResult<T>withFlowErrorin core runtime paths, and keep errors explicit and binding-appropriate at the wrapper layer.
Keep async behavior on the existing tokio-based model; bindings should preserve callback and future lifetimes rather than blocking or hiding async work unexpectedly.
Files:
crates/python/src/py_plugin.rspython/tests/test_observability_plugin.pycrates/python/tests/coverage/py_plugin_coverage_tests.rs
**/*.{rs,py,go,js,ts,c,h}
📄 CodeRabbit inference engine (CONTRIBUTING.md)
Use language-appropriate naming conventions: Rust
snake_case, C FFI exports prefixednemo_relay_, GoPascalCase, Node.jscamelCase, and Pythonsnake_case.
Files:
crates/python/src/py_plugin.rspython/tests/test_observability_plugin.pycrates/python/tests/coverage/py_plugin_coverage_tests.rs
**/*.{rs,go,js,ts}
📄 CodeRabbit inference engine (CONTRIBUTING.md)
Add the SPDX license header to all Rust, Go, JavaScript, and TypeScript source files using the corresponding
//comment form.
Files:
crates/python/src/py_plugin.rscrates/python/tests/coverage/py_plugin_coverage_tests.rs
{crates/**/src/**/*.rs,python/**/*.py}
📄 CodeRabbit inference engine (.agents/skills/maintain-dynamic-plugins/SKILL.md)
Do not add tests under
src; Rust tests belong in cratetests/trees, and Python SDK tests belong underpython/tests.
Files:
crates/python/src/py_plugin.rspython/tests/test_observability_plugin.py
**/*
📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md)
**/*: Format changed files with the language-native formatter before the final lint/test pass.
If dynamic plugin behavior changed, usemaintain-dynamic-pluginsand include the native SDK, worker protocol, Python SDK, docs, packaging, and Codecov surfaces in the validation plan.
If code changes alter APIs, bindings, commands, paths, packaging behavior, observability/adaptive semantics, or documented best practices, update any dependent maintainer or consumer skills in the same branch.
During iteration, preferuv run pre-commit run --files <changed files...>.
Before review or handoff, runuv run pre-commit run --all-files.
Files:
crates/python/src/py_plugin.rspython/tests/test_observability_plugin.pycrates/python/tests/coverage/py_plugin_coverage_tests.rs
**/*.{rs,py,go,js,ts}
📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md)
If a language surface changed, always run that language's test target even when Rust core did not change.
Files:
crates/python/src/py_plugin.rspython/tests/test_observability_plugin.pycrates/python/tests/coverage/py_plugin_coverage_tests.rs
**/*.{rs,py,js,ts,tsx,go,java,kt,swift}
📄 CodeRabbit inference engine (.agents/skills/add-middleware/SKILL.md)
Add tests covering registration and duplicate names, deregistration and missing names, priority ordering, callback failure policy, scope-local inheritance and cleanup, event payload semantics, immutable mark and scope fields, and parity across affected bindings.
Files:
crates/python/src/py_plugin.rspython/tests/test_observability_plugin.pycrates/python/tests/coverage/py_plugin_coverage_tests.rs
crates/{python,ffi,node}/**/*
⚙️ CodeRabbit configuration file
crates/{python,ffi,node}/**/*: Treat binding changes as public API changes. Check for parity with the other language bindings, FFI ownership/lifetime safety,
callback error propagation, stable type conversion, and consistent async/stream semantics.
Flag changes that update one binding without corresponding tests or documentation for the same surface elsewhere.
Files:
crates/python/src/py_plugin.rscrates/python/tests/coverage/py_plugin_coverage_tests.rs
**/*.py
📄 CodeRabbit inference engine (CONTRIBUTING.md)
**/*.py: When changing the Python wrapper package, tests, or docs tooling, lint with Ruff (E,F,W,I), format with Ruff formatter (120-character lines, double quotes), and passtytype checking.
Add the SPDX license header to all Python source files using the#comment form.
Files:
python/tests/test_observability_plugin.py
**/*.{py,go,js,ts}
📄 CodeRabbit inference engine (.agents/skills/maintain-observability/SKILL.md)
Keep Python, Go, and Node.js config objects and subscriber/exporter methods aligned so all bindings expose the same logical knobs and semantics.
Files:
python/tests/test_observability_plugin.py
python/tests/**/*.py
📄 CodeRabbit inference engine (.agents/skills/test-python-binding/SKILL.md)
python/tests/**/*.py: Pytest is used to run tests.
Do not add@pytest.mark.asyncioto any test; async tests are automatically detected and run by the async runner.
Do not add a-> Nonereturn type annotation to test functions.
When mocking a class, do not define a new class; useunittest.mock.MagicMockorunittest.mock.AsyncMock, with thespecconstructor argument when necessary.
Name mocked classes with themockprefix, notfake.
Prefer pytest fixtures over helper methods.
Do not repeat fixtures; if a fixture is needed in multiple test files, place it in aconftest.pyfile.
When creating a fixture, use@pytest.fixture(name="<fixture_name>"[, scope="<scope>"])and define the fixture function asdef <fixture_name>_fixture() -> <return_type>:; only specifyscopewhen it is notfunction.
Preferpytest.mark.parametrizeover creating individual tests for different input types.
Files:
python/tests/test_observability_plugin.py
{crates/**/tests/**,python/tests/**,go/nemo_relay/**/*_test.go}
⚙️ CodeRabbit configuration file
{crates/**/tests/**,python/tests/**,go/nemo_relay/**/*_test.go}: Tests should cover the behavior promised by the changed API surface, including error paths and cross-request isolation where relevant.
Prefer assertions on lifecycle events, scope stacks, middleware ordering, and binding parity over shallow smoke tests.
Files:
python/tests/test_observability_plugin.pycrates/python/tests/coverage/py_plugin_coverage_tests.rs
🪛 Ruff (0.15.21)
python/tests/test_observability_plugin.py
[warning] 239-239: Missing return type annotation for private function do_POST
Add return type annotation: None
(ANN202)
[error] 247-247: Function argument format is shadowing a Python builtin
(A002)
[warning] 247-247: Unused method argument: format
(ARG002)
[warning] 247-247: Unused method argument: args
(ARG002)
[warning] 247-247: Dynamically typed expressions (typing.Any) are disallowed in *args
(ANN401)
🔇 Additional comments (3)
crates/python/src/py_plugin.rs (1)
958-959: LGTM!crates/python/tests/coverage/py_plugin_coverage_tests.rs (1)
580-580: LGTM!python/tests/test_observability_plugin.py (1)
9-15: LGTM!
| import pytest | ||
|
|
||
| from nemo_relay import ScopeType, plugin, scope | ||
| from nemo_relay import ScopeType, plugin, scope, subscribers |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Gate the HTTP response during plugin.clear(), not before it.
The explicit flush and allow_response.set() complete the blocked request before teardown, so this passes even if plugin.clear() retains the GIL. Let clear perform the flush and use a delayed Python thread/timer to release the response while clear is blocked.
Proposed fix
-from nemo_relay import ScopeType, plugin, scope, subscribers
+from nemo_relay import ScopeType, plugin, scope
...
- subscribers.flush()
assert request_received.wait(timeout=2)
- allow_response.set()
+ response_timer = threading.Timer(0.1, allow_response.set)
+ response_timer.start()
started_at = time.monotonic()
plugin.clear()
+ response_timer.join(timeout=2)
cleared = TrueAs per path instructions, “Tests should cover the behavior promised by the changed API surface.”
Also applies to: 290-296
🤖 Prompt for 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.
In `@python/tests/test_observability_plugin.py` at line 16, Update the test around
plugin.clear() to keep the HTTP request blocked while teardown runs: remove the
explicit flush and immediate allow_response.set(), invoke plugin.clear() while
the request remains gated, and use a delayed Python thread or timer to release
the response during the blocked clear. Apply the same sequencing to the related
test block around the other referenced lines, ensuring the test verifies clear
performs the flush and does not depend on pre-teardown response release.
Source: Path instructions
Overview
Release the GIL while synchronous Python plugin teardown waits for ATOF HTTP sink workers.
Details
clear_plugin_configurationthroughpy.detachso other Python threads can service HTTP sink responses during teardown.Where should the reviewer start?
crates/python/src/py_plugin.rs: the GIL release around the blocking teardown call.python/tests/test_observability_plugin.py: the regression scenario.Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to)
Summary by CodeRabbit
Bug Fixes
Tests