Skip to content

Ensure rs-dds-adapter is killed in finally block to prevent leaks - #15237

Merged
AviaAv merged 1 commit into
realsenseai:developmentfrom
AviaAv:dds-adapter-fix
Jun 18, 2026
Merged

AviaAv merged 1 commit into
realsenseai:developmentfrom
AviaAv:dds-adapter-fix

Conversation

@AviaAv

@AviaAv AviaAv commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

When test-dds-adapter-depth aborts (e.g. the wait-for-device timeout), the cleanup was wrapped in a test.closure. On an aborted test abort() calls sys.exit() before clearing test_in_progress, so opening that closure in finally throws "test case is already running" and the kill never runs — leaking rs-dds-adapter, which then poisons later tests in the session.

Fix: kill the adapter directly in finally (plain try/except + kill_all_dds_adapters()), so it can't be blocked by leftover test state. No other behavior change.

@AviaAv
AviaAv requested a review from Nir-Az June 18, 2026 15:26
@AviaAv
AviaAv merged commit d1cc8c9 into realsenseai:development Jun 18, 2026
29 of 30 checks passed

@sysrsbuild-gh-agentic sysrsbuild-gh-agentic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Test review body

with test.closure( 'Stop rs-dds-adapter', on_fail=test.ABORT ):
# Kill directly, not via test.closure: an aborted test would make the closure throw and skip the kill.
if adapter_process:
try:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Test inline comment

@sysrsbuild-gh-agentic sysrsbuild-gh-agentic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Test 2 comments

with test.closure( 'Stop rs-dds-adapter', on_fail=test.ABORT ):
# Kill directly, not via test.closure: an aborted test would make the closure throw and skip the kill.
if adapter_process:
try:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Comment on if adapter_process line

if adapter_process:
try:
# Try graceful termination first (SIGTERM lets adapter release DDS resources on Linux)
adapter_process.send_signal( signal.SIGTERM )

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Comment on send_signal context line

@sysrsbuild-gh-agentic sysrsbuild-gh-agentic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[rs-agentic bot] Automated review of PR #15237 — unit-tests/dds/adapter/test-depth.py

The fix is correct and well-targeted. The root cause (aborting test leaving test_in_progress set, causing test.closure in finally to throw before the kill runs) is accurately diagnosed, and moving the cleanup out of test.closure into a plain if/try/finally is the right approach.

A few observations below (inline + general).


General notes (not inline):

  1. Cleanup no longer appears in test report — By removing test.closure('Stop rs-dds-adapter'), the termination step is no longer recorded in the test results. This is intentional and correct, but worth being aware of: if the graceful kill fails and kill_all_dds_adapters() is also slow, there will be no test-step failure surfaced — only a log.e() line. Acceptable trade-off given the bug fix, but document it.

  2. Missing regression test — No test is added to simulate the abort scenario (e.g. mock a wait-for-device timeout that triggers abort(), then verify rs-dds-adapter is not left running). Consider adding a test to prevent regressions of this specific leak path.

with test.closure( 'Stop rs-dds-adapter', on_fail=test.ABORT ):
# Kill directly, not via test.closure: an aborted test would make the closure throw and skip the kill.
if adapter_process:
try:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[rs-agentic bot] Enhancement — Good guard added.

This if adapter_process: check is an improvement over the original (which would raise AttributeError on None.send_signal() if the adapter never started). However, note a subtle behavioral change: in the original code, even when adapter_process was None, the inner finally: kill_all_dds_adapters() would still execute (after the AttributeError propagated through test.closure). With the new code, if adapter_process is None, the entire block is skipped — including the kill_all_dds_adapters() safety-net.

In practice this is safe (the top-of-script cleanup and the _sigterm_handler cover leftover processes), but consider moving the kill_all_dds_adapters() call outside the if adapter_process: guard for symmetry:

finally:
    if adapter_process:
        try:
            adapter_process.send_signal( signal.SIGTERM )
            adapter_process.wait( timeout=2 )
            log.d( 'rs-dds-adapter terminated gracefully' )
        except (subprocess.TimeoutExpired, Exception) as e:
            log.e( f'Error terminating rs-dds-adapter: {e}' )
    kill_all_dds_adapters()  # always run, even if adapter_process is None

if adapter_process:
try:
# Try graceful termination first (SIGTERM lets adapter release DDS resources on Linux)
adapter_process.send_signal( signal.SIGTERM )

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[rs-agentic bot] Potential Bug / Enhancement — Redundant exception type + log level mismatch.

(Note: this comment targets the except (subprocess.TimeoutExpired, Exception) as e: clause at line ~121, just below the visible diff hunk — anchored here as the nearest diff line.)

subprocess.TimeoutExpired is a subclass of Exception, so listing it explicitly before Exception is redundant — Exception already catches it. This was inherited from the original code but worth cleaning up.

More importantly, both a graceful-shutdown timeout and an unexpected exception result in log.e(...). A TimeoutExpired (adapter took >2s to die before force-kill) is often expected behavior, not an error. Consider distinguishing them:

except subprocess.TimeoutExpired:
    log.w( 'rs-dds-adapter did not terminate gracefully within 2s; force-killing' )
except Exception as e:
    log.e( f'Unexpected error terminating rs-dds-adapter: {e}' )

This gives cleaner signal in CI logs and avoids false-alarm ERROR lines on slow adapter shutdown.

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