-   Notifications  
You must be signed in to change notification settings  - Fork 5
 
Exception handling #343
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
Exception handling #343
Conversation
 WalkthroughException handling was added to both the LAMMPS MPI execution function and the concurrent wrapper to ensure errors during command execution are caught and reported properly. Additionally, a new test was introduced to verify that the system raises exceptions when processing invalid LAMMPS input scripts. Changes
 Poem
 ✨ Finishing Touches
 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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit: 
 SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
 Other keywords and placeholders
 CodeRabbit Configuration File (  |  
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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
 Review profile: CHILL
 Plan: Pro
📒 Files selected for processing (3)
pylammpsmpi/mpi/lmpmpi.py(1 hunks)pylammpsmpi/wrapper/concurrent.py(1 hunks)tests/test_exception.py(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/test_exception.py (1)
pylammpsmpi/wrapper/extended.py (1)
LammpsLibrary(239-328)
🪛 Ruff (0.12.2)
tests/test_exception.py
37-37: Do not assert blind exception: Exception
(B017)
🔇 Additional comments (3)
pylammpsmpi/wrapper/concurrent.py (1)
71-74: Excellent exception handling improvement!The try-except block properly catches exceptions from the MPI interface and sets them on the future object, ensuring proper error propagation to the calling code. This prevents unhandled exceptions in the worker thread and maintains the asynchronous interface contract.
pylammpsmpi/mpi/lmpmpi.py (1)
485-495: Robust exception handling for LAMMPS command execution!The try-except block effectively catches exceptions during LAMMPS command execution and:
- Reports errors through the MPI interface when rank is 0
 - Breaks the execution loop to prevent further processing
 - Maintains the original behavior for successful execution
 This ensures proper error propagation in the MPI environment and prevents process crashes from unhandled exceptions.
tests/test_exception.py (1)
5-30: Well-structured test data for exception validation.The LAMMPS input script effectively creates a problematic scenario to test exception handling. The script uses valid LAMMPS syntax that should trigger an error during execution, making it suitable for testing the exception propagation improvements.
| class TestException(TestCase): | ||
| def test_overlapping_atoms(self): | ||
| with open("in.error", "w") as f: | ||
| f.writelines(lmp_str) | ||
| with self.assertRaises(Exception): | ||
| lmp = LammpsLibrary(cores=2, mode="local") | ||
| lmp.file("in.error") | 
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.
🛠️ Refactor suggestion
Refine exception handling to be more specific.
The test uses a broad Exception catch which makes it difficult to verify that the correct type of error is being raised. Consider catching more specific exceptions or validating the exception message.
Apply this diff to improve the test specificity:
- def test_overlapping_atoms(self): - with open("in.error", "w") as f: - f.writelines(lmp_str) - with self.assertRaises(Exception): - lmp = LammpsLibrary(cores=2, mode="local") - lmp.file("in.error") + def test_overlapping_atoms(self): + with open("in.error", "w") as f: + f.writelines(lmp_str) + with self.assertRaises(Exception) as context: + lmp = LammpsLibrary(cores=2, mode="local") + lmp.file("in.error") + # Verify that an actual error occurred, not just any exception + self.assertIsNotNone(context.exception)Alternatively, if you know the specific exception type that LAMMPS raises for this error condition, replace Exception with that specific type.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| class TestException(TestCase): | |
| def test_overlapping_atoms(self): | |
| with open("in.error", "w") as f: | |
| f.writelines(lmp_str) | |
| with self.assertRaises(Exception): | |
| lmp = LammpsLibrary(cores=2, mode="local") | |
| lmp.file("in.error") | |
| class TestException(TestCase): | |
| def test_overlapping_atoms(self): | |
| with open("in.error", "w") as f: | |
| f.writelines(lmp_str) | |
| with self.assertRaises(Exception) as context: | |
| lmp = LammpsLibrary(cores=2, mode="local") | |
| lmp.file("in.error") | |
| # Verify that an actual error occurred, not just any exception | |
| self.assertIsNotNone(context.exception) | 
🧰 Tools
🪛 Ruff (0.12.2)
37-37: Do not assert blind exception: Exception
(B017)
🤖 Prompt for AI Agents
In tests/test_exception.py around lines 33 to 39, the test currently catches a broad Exception which is too generic. Identify the specific exception type that LammpsLibrary raises for the error condition in this test and replace Exception with that specific exception class in the with self.assertRaises() statement. Alternatively, if the exact exception type is unknown, catch Exception but add an assertion inside the block to check that the exception message matches the expected error message to ensure the correct error is being tested.  Codecov ReportAll modified and coverable lines are covered by tests ✅ 
 Additional details and impacted files@@ Coverage Diff @@ ## main #343 +/- ## ========================================== + Coverage 81.02% 81.15% +0.13%  ========================================== Files 5 5 Lines 548 552 +4 ========================================== + Hits 444 448 +4  Misses 104 104 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
  |  
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.
Actionable comments posted: 1
♻️ Duplicate comments (1)
tests/test_exception.py (1)
45-51: The broad Exception catch issue was already flagged in previous reviews.This is the same issue that was identified in the past review comments - the test uses a broad
Exceptioncatch which makes it difficult to verify that the correct type of error is being raised.
📜 Review details
Configuration used: CodeRabbit UI
 Review profile: CHILL
 Plan: Pro
📒 Files selected for processing (2)
pylammpsmpi/wrapper/concurrent.py(1 hunks)tests/test_exception.py(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- pylammpsmpi/wrapper/concurrent.py
 
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/test_exception.py (2)
pylammpsmpi/wrapper/extended.py (1)
LammpsLibrary(239-328)pylammpsmpi/wrapper/concurrent.py (1)
file(142-157)
🪛 Ruff (0.12.2)
tests/test_exception.py
49-49: Do not assert blind exception: Exception
(B017)
🔇 Additional comments (2)
tests/test_exception.py (2)
1-3: LGTM: Import statements are appropriate.The imports are correctly chosen for the test functionality and all are used within the code.
53-56: LGTM: Proper test cleanup implementation.The tearDown method correctly cleans up both the input file and the log file that LAMMPS generates, with appropriate existence checks to prevent errors.
| lmp_str = """\ | ||
| # 3d Lennard-Jones melt | ||
|   |  ||
| units lj | ||
| atom_style atomic | ||
| atom_modify map array | ||
|   |  ||
| lattice fcc 0.8442 | ||
| region box block 0 4 0 4 0 4 | ||
| create_box 1 box | ||
| create_atoms 1 box | ||
| mass 1 1.0 | ||
|   |  ||
| velocity all create 100.44 87287 loop geom | ||
|   |  ||
| pair_style lj/cut 2.5 | ||
| pair_coeff 1 1 1.0 1.0 2.5 | ||
|   |  ||
| neighbor 0.3 bin | ||
| neigh_modify delay 0 every 20 check no | ||
|   |  ||
| compute 1 all temp | ||
| compute 2 all pressure 1 | ||
| compute ke all ke/atom | ||
| compute msd all msd | ||
| compute v all property/atom vx vy vz | ||
|   |  ||
| variable fx atom fx | ||
| variable tt equal temp | ||
| variable test string "25" | ||
| fix 1 all nve | ||
| fix 2 all ave/time 10 1 10 c_1 c_2 | ||
|   |  ||
|   |  ||
|   |  ||
| run 1000 | ||
| """ | 
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.
💡 Verification agent
🧩 Analysis chain
Verify that the LAMMPS script produces the expected exception.
The test is named test_overlapping_atoms but the LAMMPS script appears to be a standard 3D Lennard-Jones melt simulation with reasonable parameters. The FCC lattice density of 0.8442 should not typically cause overlapping atoms.
Please verify that this script actually produces the expected exception by running it manually or confirm that the test name accurately reflects the error condition being tested.
🏁 Script executed:
#!/bin/bash # Search for any documentation or comments about what specific error this test expects rg -i "overlap" --type py rg -i "error.*atom" --type pyLength of output: 233
🏁 Script executed:
#!/bin/bash # Display the test_overlapping_atoms implementation in tests/test_exception.py for context rg -n "def test_overlapping_atoms" -A 20 tests/test_exception.pyLength of output: 476
🏁 Script executed:
#!/bin/bash # Locate the LammpsLibrary class to inspect its file() method rg -n "class LammpsLibrary" -A50 .Length of output: 3636
🏁 Script executed:
#!/bin/bash # Find the definition of the `file` method in the wrapper rg -n "def file" -A10 pylammpsmpi/wrapper/extended.pyLength of output: 55
Add support for the file command in LammpsLibrary (tests are passing for the wrong reason)
The test_overlapping_atoms writes a LAMMPS input to “in.error” and then does
with self.assertRaises(Exception): lmp = LammpsLibrary(cores=2, mode="local") lmp.file("in.error")but LammpsLibrary has no file() method (and doesn’t proxy it via __getattr__), so calling lmp.file(...) raises a TypeError instead of an overlapping‐atoms error from LAMMPS. The test thus passes on a missing‐method error, not the intended collision error.
Please update as follows:
• In pylammpsmpi/wrapper/extended.py (inside class LammpsLibrary), add:
 def file(self, script_path: str) -> Any: """Run a LAMMPS input file and return the result (raises on LAMMPS errors).""" return getattr(self.lmp, "file")(script_path).result()• Ensure that "file" is included in func_list so that __getattr__ can wrap it (or explicitly proxy it as above).
• In tests/test_exception.py, either:
 – Keep lmp.file("in.error") and assert the LAMMPS “overlapping atoms” exception, or
 – Rename the test to reflect that a missing‐method error is expected (if the API is truly not intended to support file()).
This will ensure the test fails only when atoms overlap, not due to a missing wrapper method.
🤖 Prompt for AI Agents
In tests/test_exception.py lines 6 to 42, the test calls lmp.file("in.error") but LammpsLibrary lacks a file() method, causing a TypeError instead of the intended LAMMPS overlapping-atoms error. To fix this, add a file() method to LammpsLibrary in pylammpsmpi/wrapper/extended.py that calls self.lmp.file(script_path).result(), and ensure "file" is included in func_list for __getattr__ wrapping or explicitly proxied. Then update the test to expect the LAMMPS overlapping atoms exception rather than a missing method error. 
Summary by CodeRabbit
Bug Fixes
Tests