Skip to content

[FIX] FeatureFinderAlgorithmPicked: count aborted seeds under a lock - #10546

Merged
timosachsenberg merged 1 commit into
developfrom
fix/ffap-abort-race
Oct 13, 2026
Merged

timosachsenberg merged 1 commit into
developfrom
fix/ffap-abort-race

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

FeatureFinderAlgorithmPicked::run_() extends seeds in a #pragma omp parallel for loop. It calls abort_() from three places in that loop when a seed fails (no isotope pattern, no extension, bad feature quality). abort_() updated the member map aborts_ with no lock, and in debug mode also abort_reasons_ and the log. When two threads insert a new abort reason at the same time, they both rebalance the map's tree and can corrupt it.

That crashed FeatureFinderAlgorithmPicked_test with a segfault on macOS CI (run on #10423, attempt 1; the re-run passed). The test section added in #10506 (min_isotope_fit, min_score and seed:min_score set to 0) rejects many seeds in parallel, which makes the collision more likely.

The fix makes the body of abort_() a named critical section, as the loop already does for its other shared state (_PLOTNR, _DEBUG, _TMPFEATUREMAP, _SEEDSINFEATURES). abort_() only counts the reason, so the lock is cheap compared with the trace extension and fitting around it. After the loop, run_() reads only the totals, and they don't depend on the order in which threads arrive.

Not changed: with debug on, other log_ writes inside the loop (the seed output on the master thread, findBestIsotopeFit_) are still not synchronised. Debug mode is not used by the tests.

Verification with helgrind

The crash did not reproduce locally: 30 runs with 16 threads before and after the fix passed. Helgrind is the evidence instead. It checks happens-before order, so it reports missing synchronisation even in runs where the threads don't actually collide.

Plain helgrind is useless on OpenMP code. libgomp synchronises with futexes that helgrind doesn't understand, so the unpatched test gives 231,657 reports from more than 1,000 contexts, most of them false positives. The runs below preload a small shim (below) that implements OpenMP critical sections with pthread mutexes and marks parallel fork/join and barriers as happens-before edges. They also suppress races whose accessing frame is inside libgomp itself. On a test program, the shim gives 0 reports for correctly locked code and reports exactly the unlocked line of a racy variant. Without the shim, helgrind also flags the line inside omp critical.

OMP_NUM_THREADS=4 LD_PRELOAD=libgomp_hg.so valgrind --tool=helgrind --suppressions=libgomp.supp FeatureFinderAlgorithmPicked_test (Linux, GCC 13.3, Debug, valgrind 3.22):

race reports other reports
develop (513f9dd) 34; each has abort_() as the accessing frame (aborts_ insert and std::_Rb_tree_insert_and_rebalance) 2
this PR (2 runs) 0 2

The 2 other reports are pthread_mutex_destroy with invalid argument in libp11-kit at program exit. They are not related to OpenMS.

Tests: FeatureFinderAlgorithmPicked_test and the TOPP_FeatureFinderCentroided tests pass (7/7). The source file this PR changes and the test are identical to develop. I verified in a build tree of another branch with the same two files.

libgomp_hg.c (LD_PRELOAD shim) and libgomp.supp
// LD_PRELOAD shim: makes libgomp synchronisation visible to helgrind.
// libgomp synchronises with futexes, which helgrind does not understand, so every
// OpenMP program reports races on correctly synchronised data. This shim
//  - implements critical sections and GOMP_atomic_* with pthread mutexes, and
//  - annotates fork/join of parallel regions and barriers as happens-before edges.
// gcc -O1 -g -shared -fPIC -o libgomp_hg.so libgomp_hg.c -ldl -lpthread
#define _GNU_SOURCE
#include <dlfcn.h>
#include <pthread.h>
#include <stdlib.h>
#include <valgrind/helgrind.h>

static pthread_mutex_t registry = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t unnamed = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t atomic_lock = PTHREAD_MUTEX_INITIALIZER;
static char barrier_token;

void GOMP_critical_start(void) { pthread_mutex_lock(&unnamed); }
void GOMP_critical_end(void) { pthread_mutex_unlock(&unnamed); }
void GOMP_atomic_start(void) { pthread_mutex_lock(&atomic_lock); }
void GOMP_atomic_end(void) { pthread_mutex_unlock(&atomic_lock); }

void GOMP_critical_name_start(void** pptr)
{
  pthread_mutex_lock(&registry);
  if (*pptr == NULL)
  {
    pthread_mutex_t* m = malloc(sizeof *m);
    pthread_mutex_init(m, NULL);
    *pptr = m;
  }
  pthread_mutex_t* m = *pptr;
  pthread_mutex_unlock(&registry);
  pthread_mutex_lock(m);
}

void GOMP_critical_name_end(void** pptr) { pthread_mutex_unlock((pthread_mutex_t*)*pptr); }

typedef void (*body_t)(void*);
struct region { body_t fn; void* data; char fork, join; };

static void region_body(void* p)
{
  struct region* r = p;
  ANNOTATE_HAPPENS_AFTER(&r->fork);
  r->fn(r->data);
  ANNOTATE_HAPPENS_BEFORE(&r->join);
}

typedef void (*parallel_fn)(body_t, void*, unsigned, unsigned);
typedef void (*parallel_loop_fn)(body_t, void*, unsigned, long, long, long, long, unsigned);
typedef void (*parallel_sections_fn)(body_t, void*, unsigned, unsigned, unsigned);
typedef void (*void_fn)(void);
#define REAL(name, type) static type real_##name; if (!real_##name) real_##name = (type)dlsym(RTLD_NEXT, #name)

void GOMP_parallel(body_t fn, void* data, unsigned n, unsigned flags)
{
  REAL(GOMP_parallel, parallel_fn);
  struct region r = {fn, data, 0, 0};
  ANNOTATE_HAPPENS_BEFORE(&r.fork);
  real_GOMP_parallel(region_body, &r, n, flags);
  ANNOTATE_HAPPENS_AFTER(&r.join);
}

void GOMP_parallel_loop_nonmonotonic_dynamic(body_t fn, void* data, unsigned n, long start, long end, long incr, long chunk, unsigned flags)
{
  REAL(GOMP_parallel_loop_nonmonotonic_dynamic, parallel_loop_fn);
  struct region r = {fn, data, 0, 0};
  ANNOTATE_HAPPENS_BEFORE(&r.fork);
  real_GOMP_parallel_loop_nonmonotonic_dynamic(region_body, &r, n, start, end, incr, chunk, flags);
  ANNOTATE_HAPPENS_AFTER(&r.join);
}

void GOMP_parallel_sections(body_t fn, void* data, unsigned n, unsigned count, unsigned flags)
{
  REAL(GOMP_parallel_sections, parallel_sections_fn);
  struct region r = {fn, data, 0, 0};
  ANNOTATE_HAPPENS_BEFORE(&r.fork);
  real_GOMP_parallel_sections(region_body, &r, n, count, flags);
  ANNOTATE_HAPPENS_AFTER(&r.join);
}

void GOMP_barrier(void)
{
  REAL(GOMP_barrier, void_fn);
  ANNOTATE_HAPPENS_BEFORE(&barrier_token);
  real_GOMP_barrier();
  ANNOTATE_HAPPENS_AFTER(&barrier_token);
}

void GOMP_loop_end(void)
{
  REAL(GOMP_loop_end, void_fn);
  ANNOTATE_HAPPENS_BEFORE(&barrier_token);
  real_GOMP_loop_end();
  ANNOTATE_HAPPENS_AFTER(&barrier_token);
}
{
   libgomp-internal-race
   Helgrind:Race
   obj:*/libgomp.so*
}

The shim covers the GOMP entry points that libOpenMS uses for parallel regions. Others (e.g. GOMP_task, ordered loops) keep helgrind's usual false positives.

Checklist

  • Make sure that you are listed in the AUTHORS file
  • Add relevant changes and new features to the CHANGELOG file
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes
  • Updated or added python bindings for changed or new classes (Tick if no updates were necessary.)

🤖 Generated with Claude Code

https://claude.ai/code/session_01B51nRFoHvZuVXD7cT8XZav


Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a possible crash during parallel feature finding by protecting abort reporting and counts from concurrent updates.

abort_() is called by all threads of the OpenMP seed extension in run_() and
updated the member maps aborts_ (and, in debug mode, abort_reasons_ and the
log) without a lock. Two threads inserting a new abort reason at the same time
could corrupt the map, which crashed FeatureFinderAlgorithmPicked_test on
macOS CI. The body of abort_() is now a named critical section, like the other
shared state of the loop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B51nRFoHvZuVXD7cT8XZav
@coderabbitai

coderabbitai Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 75bfe4a5-8628-4e7d-b9b9-2ef83cf24625
📥 Commits

Reviewing files that changed from the base of the PR and between 1381c50 and c753935.

📒 Files selected for processing (2)
  • CHANGELOG
  • src/openms/source/FEATUREFINDER/FeatureFinderAlgorithmPicked.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

📝 Walkthrough

FeatureFinderAlgorithmPicked::abort_ now protects shared abort counters and debug logging with an OpenMP critical section. The changelog records the prior unprotected access and its possible consequences.

Changes: Abort reporting synchronization

Layer / File(s) Summary
Synchronize abort updates
src/openms/source/FEATUREFINDER/FeatureFinderAlgorithmPicked.cpp, CHANGELOG
abort_ performs debug logging and updates abort counts and details inside a named OpenMP critical section. The changelog records the prior unprotected map access and possible intermittent crashes.

Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to c7539

Abort reporting is serialized during parallel seed extension; no remaining merge-blocking risk is identified.

Pre-merge checks · Passed 5
Passed Passed checks (5 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: protecting aborted-seed counting in FeatureFinderAlgorithmPicked with a lock.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (1 skipped: 1 …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

A rabbit hops past seeds at dawn
The shared abort count is now held fast
Debug notes wait their turn to write
No map is nudged by racing paws
The burrow hums in orderly quiet

Comment @coderabbitai help to get the list of available commands.

@timosachsenberg

Copy link
Copy Markdown
Contributor Author

Seems correct to me

@timosachsenberg
timosachsenberg merged commit fd88028 into develop Oct 13, 2026
33 checks passed
@timosachsenberg
timosachsenberg deleted the fix/ffap-abort-race branch October 13, 2026 21:40
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