Skip to content

Conversation

@DrDomenicoMarson
Copy link
Contributor

@DrDomenicoMarson DrDomenicoMarson commented Sep 11, 2025

Fixes #5112

Problem:

sort_backbone can hang indefinitely when the input AtomGroup is disconnected (e.g., some backbone atoms were excluded by selection). Currently, the function only checks n_fragments, which is insufficient to catch all cases of disconnected backbones.

Cause:

  1. The reliance on n_fragments alone does not reliably detect disconnected selections.
  2. The while loop in sort_backbone iterates until the backbone is fully traversed, but if the selection is disconnected, the loop can never complete.

Solution / Changes made:

  1. Added robust validation before the traversal, checking:

    • Each atom’s connectivity degree: 1 for caps, 2 for internal atoms
    • No atom has a degree outside {1, 2}
    • Exactly two cap atoms exist
    • Added final sanity check after traversal (ensure the sorted backbone has the same length as the input)
    • All error messages now report the problematic atoms to help with debugging.
  2. Replaced the “dangerous” unbounded while loop with a bounded iteration over the backbone length.

Tested with backbone selections missing internal atoms, that now raises a descriptive ValueError instead of hanging.

PR Checklist

  • Issue raised/referenced?
  • Tests updated/added?
  • Documentation updated/added? No change needed?
  • package/CHANGELOG file updated?
  • Is your name in package/AUTHORS? (If it is not, add it!)

Developers Certificate of Origin

I certify that I can submit this code contribution as described in the Developer Certificate of Origin, under the MDAnalysis LICENSE.


📚 Documentation preview 📚: https://mdanalysis--5113.org.readthedocs.build/en/5113/

@codecov
Copy link

codecov bot commented Sep 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.88%. Comparing base (5d48c5c) to head (800cbdf).
⚠️ Report is 30 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #5113      +/-   ##
===========================================
+ Coverage    93.86%   93.88%   +0.02%     
===========================================
  Files          179      180       +1     
  Lines        22249    22421     +172     
  Branches      3161     3185      +24     
===========================================
+ Hits         20885    21051     +166     
  Misses         902      902              
- Partials       462      468       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@orbeckst orbeckst left a comment

Choose a reason for hiding this comment

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

Thank you for your fix @DrDomenicoMarson ! Please address @tylerjereddy 's comments and my minor ones.

@orbeckst orbeckst self-assigned this Sep 30, 2025
Copy link
Member

@orbeckst orbeckst left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the other comments. Todo:

  • check formatting (black)
  • improve test coverage of patch

Comment on lines 107 to 113
if len(sorted_backbone) != len(backbone):
raise ValueError(
"Backbone traversal did not visit all atoms. "
"Expected {} atoms, got {}.".format(
len(backbone), len(sorted_backbone)
)
)
Copy link
Member

Choose a reason for hiding this comment

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

Not covered by tests – could you please add a test for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wrote this as a final sanity check, but when I tested the code, I realized that I can’t reach this part of the code without having the analysis fail earlier (which is what it should do, considering the situation). So, I decided to remove this “untestable and useless” part.

Copy link
Member

@orbeckst orbeckst left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

Will get into release 2.10.0.

@orbeckst orbeckst merged commit 2250195 into MDAnalysis:develop Oct 13, 2025
23 checks passed
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.

sort_backbone hangs when backbone selection is disconnected

3 participants