From 4b5c6e234d8fd3a588eab1cfbfe9a60f59db97f7 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Thu, 17 Sep 2026 12:25:09 +0200 Subject: [PATCH] indexing: the cap search always puts the direction grid back, and every FFT upload is checked A production broker on rc.170 segfaulted again in the FilterFFTResults length sort, in a binary that does contain the rc.170 non-finite guard (verified from its disassembly: the two isfinite tests are compiled in and skip the insert). With magnitude and length guaranteed finite, the only remaining way that sort sees a NaN key is a NaN direction vector - and the one place that writes the direction grid is SearchCap. SearchCap swaps in a temporary cap grid but restored it around ExecuteFFT only. The fill can run out of memory, the upload can fail, and the .at() that reads the peak back throws on exactly the corrupted result row this search has to survive - any of those left the cap grid installed on an indexer the pool hands the next image. If the axis was ever not a direction, axis.Normalize() is NaN and the leaked grid is 16384 NaN directions, which is that crash on every image afterwards. The restore now covers the whole body, and an axis that is not finite and non-zero is refused instead of normalized into NaN. The peak is also tested against the minimum length before the .at() rather than after, so an unusable peak with a corrupted index no longer throws at all. DirectionsChanged uploaded the grid with three unchecked cudaMemcpy calls, and the spot upload in ExecuteFFT had three more: a silently failed upload leaves the device holding the previous grid, so the directions the host reads the results against are not the ones the kernel used. Checked like the rest of the file. Tests: [Indexing] (9 cases, 115 assertions) and RotationIndexer pass. Co-Authored-By: Claude Opus 5 (1M context) --- image_analysis/indexing/FFTIndexer.cpp | 53 ++++++++++++++---------- image_analysis/indexing/FFTIndexerGPU.cu | 15 ++++--- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/image_analysis/indexing/FFTIndexer.cpp b/image_analysis/indexing/FFTIndexer.cpp index 08fb35f77..dfb1efa15 100644 --- a/image_analysis/indexing/FFTIndexer.cpp +++ b/image_analysis/indexing/FFTIndexer.cpp @@ -425,6 +425,13 @@ std::optional FFTIndexer::DegeneratePlaneNormal(const std::vector // and this costs exactly one extra pass. std::optional FFTIndexer::SearchCap(const std::vector &coord, size_t nspots, const Coord &axis, float half_angle_deg) { + // An axis that is not a direction normalizes to NaN, and a grid of NaN directions is not a worse + // answer but a crash: every row it produces is a NaN length, and a NaN key makes the sort in + // FilterFFTResults walk off its index array. There is nothing to search about such an axis. + const float axis_length = axis.Length(); + if (!std::isfinite(axis_length) || (axis_length <= 0.0f)) + return {}; + const Coord u = axis.Normalize(); Coord seed = std::fabs(u.x) < 0.9f ? Coord(1.0f, 0.0f, 0.0f) : Coord(0.0f, 1.0f, 0.0f); const Coord e1 = (u % seed).Normalize(); @@ -435,41 +442,45 @@ std::optional FFTIndexer::SearchCap(const std::vector &coord, size std::vector saved; saved.swap(direction_vectors); - direction_vectors.reserve(saved.size()); - for (size_t i = 0; i < saved.size(); i++) { - // Same spiral as SetupDirectionVectors, with z restricted to the cap instead of the half-sphere. - const double z = 1.0 - (1.0 - cos_half) * static_cast(i) / static_cast(saved.size() - 1); - const double r = std::sqrt(std::max(0.0, 1.0 - z * z)); - const double theta = golden_angle * static_cast(i); - direction_vectors.emplace_back(u * static_cast(z) - + e1 * static_cast(r * std::cos(theta)) - + e2 * static_cast(r * std::sin(theta))); - } - DirectionsChanged(); // The search borrows a member the object keeps using afterwards, and an indexer outlives a failed // transform - the broker's pool logs the error and hands the same indexer the next image - so the - // grid has to go back even when ExecuteFFT throws. + // grid has to go back however this function leaves. Everything between the swap and the restore is + // inside the try for that reason: the fill can run out of memory, the upload can fail, and the + // .at() below throws on exactly the corrupted result row this search has to survive. Leaving the + // cap grid installed poisons every later image on that indexer. + std::optional found; try { + direction_vectors.reserve(saved.size()); + for (size_t i = 0; i < saved.size(); i++) { + // Same spiral as SetupDirectionVectors, with z restricted to the cap instead of the half-sphere. + const double z = 1.0 - (1.0 - cos_half) * static_cast(i) / static_cast(saved.size() - 1); + const double r = std::sqrt(std::max(0.0, 1.0 - z * z)); + const double theta = golden_angle * static_cast(i); + direction_vectors.emplace_back(u * static_cast(z) + + e1 * static_cast(r * std::cos(theta)) + + e2 * static_cast(r * std::sin(theta))); + } + DirectionsChanged(); + ExecuteFFT(coord, nspots); + + int best = 0; + for (int i = 1; i < nDirections; i++) + if (result_fft[i].magnitude > result_fft[best].magnitude) + best = i; + const FFTResult peak = result_fft[best]; + if (peak.length >= min_length_A) + found = direction_vectors.at(peak.direction) * peak.length; } catch (...) { direction_vectors.swap(saved); DirectionsChanged(); throw; } - int best = 0; - for (int i = 1; i < nDirections; i++) - if (result_fft[i].magnitude > result_fft[best].magnitude) - best = i; - const FFTResult peak = result_fft[best]; - const Coord found = direction_vectors.at(peak.direction) * peak.length; - direction_vectors.swap(saved); DirectionsChanged(); - if (peak.length < min_length_A) - return {}; return found; } diff --git a/image_analysis/indexing/FFTIndexerGPU.cu b/image_analysis/indexing/FFTIndexerGPU.cu index ce0959a90..a769801f0 100644 --- a/image_analysis/indexing/FFTIndexerGPU.cu +++ b/image_analysis/indexing/FFTIndexerGPU.cu @@ -192,9 +192,12 @@ void FFTIndexerGPU::DirectionsChanged() { dir_y[i] = direction_vectors.at(i).y; dir_z[i] = direction_vectors.at(i).z; } - cudaMemcpy(d_dir_x, dir_x.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice); - cudaMemcpy(d_dir_y, dir_y.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice); - cudaMemcpy(d_dir_z, dir_z.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice); + // Checked like every other CUDA call here: a silently failed upload leaves the device holding the + // PREVIOUS grid, so the directions the host reads the results against are not the ones the kernel + // used - wrong rows reported as good ones, and nothing says so. + cuda_err(cudaMemcpy(d_dir_x, dir_x.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice)); + cuda_err(cudaMemcpy(d_dir_y, dir_y.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice)); + cuda_err(cudaMemcpy(d_dir_z, dir_z.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice)); } void FFTIndexerGPU::ExecuteFFT(const std::vector &coord, size_t nspots) { @@ -207,9 +210,9 @@ void FFTIndexerGPU::ExecuteFFT(const std::vector &coord, size_t nspots) { spot_z[i] = coord[i].z; } - cudaMemcpyAsync(d_spot_x, spot_x, nspots * sizeof(float), cudaMemcpyHostToDevice, stream); - cudaMemcpyAsync(d_spot_y, spot_y, nspots * sizeof(float), cudaMemcpyHostToDevice, stream); - cudaMemcpyAsync(d_spot_z, spot_z, nspots * sizeof(float), cudaMemcpyHostToDevice, stream); + cuda_err(cudaMemcpyAsync(d_spot_x, spot_x, nspots * sizeof(float), cudaMemcpyHostToDevice, stream)); + cuda_err(cudaMemcpyAsync(d_spot_y, spot_y, nspots * sizeof(float), cudaMemcpyHostToDevice, stream)); + cuda_err(cudaMemcpyAsync(d_spot_z, spot_z, nspots * sizeof(float), cudaMemcpyHostToDevice, stream)); // Shared-memory bins where they fit (they do at any sane sizing - 12.6 kB at the defaults), the // thread-per-direction kernel where they do not.