-
Notifications
You must be signed in to change notification settings - Fork 140
feat: add monitoring thread to _multi_connect for early timeout exit #4310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This commit adds a background monitoring thread to the _multi_connect method that checks if the MAPDL process is alive during connection attempts. This allows PyMAPDL to exit early if the process dies, rather than waiting for the full timeout period. Key changes: Added monitoring thread in _multi_connect that runs in parallel with connection attempts. Thread checks process status every 0.5 seconds. Early exit when process death is detected. Proper thread cleanup with Event and join mechanisms. Only monitors local MAPDL instances. Tests added: 7 new tests covering valid connections, early exit on death, monitoring conditions, thread cleanup, process death detection, successful connection cleanup, and remote instance behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements a background monitoring thread in the _multi_connect method that actively checks if a local MAPDL process is alive during connection attempts. This enables PyMAPDL to exit early if the MAPDL process dies, reducing wait time from a full timeout (~15-30s) to approximately 1-2 seconds. The monitoring thread runs in parallel with connection attempts, checking process status every 0.5 seconds using _check_process_is_alive, and only activates for local MAPDL instances with available process and path information.
Key Changes
- Added monitoring thread to
_multi_connectinsrc/ansys/mapdl/core/mapdl_grpc.pywith proper cleanup usingthreading.Eventandjoin()mechanisms - Implemented 7 comprehensive unit tests covering various scenarios including process death detection, monitoring conditions, thread cleanup, and remote instance behavior
- Thread monitors process status every 0.5 seconds and triggers early exit when process death is detected
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/ansys/mapdl/core/mapdl_grpc.py | Adds monitoring thread to _multi_connect method with proper cleanup and early exit logic when MAPDL process dies during connection |
| tests/test_launcher.py | Adds 7 unit tests validating monitoring thread functionality, early exit behavior, thread cleanup, and proper activation conditions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/ansys/mapdl/core/mapdl_grpc.py
Outdated
|
|
||
| # Start the monitoring thread | ||
| monitor_thread = None | ||
| if self._local and self._mapdl_process: |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The monitoring thread starts when self._local and self._mapdl_process are truthy, but inside the thread function at line 634, it also checks for self._path. This creates an inconsistency where the thread starts but may not actually monitor anything if self._path is None. Consider adding and self._path to the condition at line 654 to match the monitoring condition inside the thread.
| if self._local and self._mapdl_process: | |
| if self._local and self._mapdl_process and self._path: |
| process.kill() | ||
| try: | ||
| process.wait(timeout=2) | ||
| except: |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a bare except: clause catches all exceptions including SystemExit and KeyboardInterrupt, which can make debugging difficult. Use except Exception: instead to catch only standard exceptions.
| except: | |
| except Exception: |
tests/test_launcher.py
Outdated
| # Don't raise exception - process is alive | ||
|
|
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment on line 2626 doesn't fully explain the mock's purpose. Consider expanding it to clarify that this mock simulates an alive process by not raising an exception, allowing the test to verify that the monitoring stops after successful connection.
| # Don't raise exception - process is alive | |
| # This mock simulates an alive process by not raising an exception. | |
| # This allows the test to verify that the monitoring thread stops | |
| # after a successful connection, as no process death is detected. |
| time.sleep(1.0) | ||
|
|
||
| # Count should not increase much after connection success | ||
| # (maybe 1-2 more checks before thread stops) |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number 3 lacks explanation. Consider adding a comment explaining why up to 3 additional checks are acceptable after connection success (e.g., due to race conditions between connection success and thread termination).
| # (maybe 1-2 more checks before thread stops) | |
| # (maybe 1-2 more checks before thread stops) | |
| # Allow up to 3 additional checks after connection success due to possible | |
| # race conditions between connection success and monitoring thread termination. |
- Add _path check to monitoring thread start condition (requires both process and path) - Mock _check_process_is_alive in all tests to prevent Mock.poll() issues - All 8 tests now pass successfully (2 skipped on non-local)
ddc43da to
0cfeba8
Compare
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4310 +/- ##
==========================================
- Coverage 91.27% 88.62% -2.66%
==========================================
Files 193 193
Lines 15742 15775 +33
==========================================
- Hits 14368 13980 -388
- Misses 1374 1795 +421 🚀 New features to boost your workflow:
|
Description
This PR adds a background monitoring thread to the
_multi_connectmethod inMapdlGrpcthat actively checks if the MAPDL process is alive during connection attempts. This enhancement allows PyMAPDL to exit early if the MAPDL process dies, rather than waiting for the full timeout period.Key Changes
Modified
_multi_connectmethod insrc/ansys/mapdl/core/mapdl_grpc.py:_check_process_is_aliveevery 0.5 secondsthreading.Eventandjoin()mechanismsTest Coverage:
Added 7 comprehensive unit tests in
tests/test_launcher.py:test_multi_connect_with_valid_process- Tests successful connection with alive process (requires local MAPDL)test_multi_connect_early_exit_on_process_death- Tests early exit when process dies during connection (requires local MAPDL)test_multi_connect_monitoring_conditions- Parametrized test for different monitoring start conditionstest_multi_connect_monitoring_thread_cleanup- Verifies thread is properly cleaned uptest_multi_connect_monitor_detects_process_death- Tests monitor detection capability with mockstest_multi_connect_with_successful_connection_stops_monitoring- Verifies monitor stops after successful connectiontest_multi_connect_remote_no_monitoring- Tests that monitoring doesn't start for remote instancesBenefits
Issue linked
This PR addresses the issue where PyMAPDL waits for the full timeout period even when the MAPDL process has already died during connection attempts, particularly when using launch and discovery methods.
Checklist
draftif it is not ready to be reviewed yet.feat: adding new MAPDL command)