Skip to content

Address threaded zernike cache and groupUsed race conditions - #1361

Merged
biochem-fan merged 1 commit into
3dem:ver5.0from
fullerjamesr:ctfrefine-thead-safety
Aug 19, 2026
Merged

biochem-fan merged 1 commit into
3dem:ver5.0from
fullerjamesr:ctfrefine-thead-safety

Conversation

@fullerjamesr

Copy link
Copy Markdown
Contributor

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.

@biochem-fan
biochem-fan changed the base branch from master to ver5.0 August 19, 2026 22:43
@biochem-fan

Copy link
Copy Markdown
Member

Thank you very much! This is a great find.
I merged it into ver5.0. I will propagate it to master and newer versions.

@biochem-fan
biochem-fan merged commit 5332ad6 into 3dem:ver5.0 Aug 19, 2026
0 of 4 checks passed
biochem-fan added a commit that referenced this pull request Aug 19, 2026
safe even when writing to different elements)
@biochem-fan

Copy link
Copy Markdown
Member

Done!

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.

2 participants