Repository navigation
Address threaded zernike cache and groupUsed race conditions - #1361
Merged
Merged
Conversation
Member
|
Thank you very much! This is a great find. |
biochem-fan
added a commit
that referenced
this pull request
Aug 19, 2026
safe even when writing to different elements)
Member
|
Done! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tracking down sporadic crashes when running CTF refinement, I think I located a 2 thread-safety/race conditions. These crashes are quite rare, but occur often enough to motivate me to look into it.
I think the most common cause of what I was seeing is the R_coeffs cache vector in Zernike.cpp . If multiple threads try to populate or grow it at once (like when the cache is totally cold), I think the subsequent assignment back to R_coeffs is not thread safe and bad things can ensue. Possibly also one thread could be reading while another is still assigning back. CTF refinement indeed calls this in multithreaded sections via (for example) AberrationEstimator::parametricFit -> TiltHelper::fitEvenZernike -> computeEvenZernike -> Zernike::Z -> Zernike::R
You could avoid a lock like what I've implemented here by instead making every application precompute the R_coeffs table upfront (it would have to know its maximum
n) before any multithreaded work, but the code in Zernike.cpp is used in several places and I was nervous to make that change. I didn't find that this change affected performance noticeably, but all I've tested is single particle CTF refinement because that's where I could reproduce crashes, as well as a handful of single particle Refine3D jobs as a quick check.even more rare, I think, was an issue where modifications to a
std::vector<bool>specifically are not guaranteed thread safe (I'm pretty sure?), so it's more robust as char and doesn't risk corrupting the assignment of whether optics group are occupied/used.