Skip to content

[DOC] pyOpenMS: document thread-safety / GIL-release contract (#9792 R-9.5) - #9857

Merged
timosachsenberg merged 4 commits into
OpenMS:developfrom
aliviahossain:docs-thread-safety-r9-5
Aug 11, 2026
Merged

timosachsenberg merged 4 commits into
OpenMS:developfrom
aliviahossain:docs-thread-safety-r9-5

Conversation

@aliviahossain

@aliviahossain aliviahossain commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

@timosachsenberg

Description

Added THREAD_SAFETY.md covering what the pyOpenMS binding layer actually guarantees around concurrency, since GIL release is often mistaken for thread safety.

Covers:

  • The safe concurrency boundary for callers (no concurrent calls on the same object, no concurrent access to anything reachable through a GIL-releasing call, including writable NumPy/memoryview/Arrow views)
  • Callback reentry behavior for streaming readers
  • A full inventory of the 62 gil_scoped_release sites, grouped by binding file, verified against source rather than paraphrased from the issue
  • A callout on TransitionListEvidenceFilter.filter, which additionally parallelizes internally via a threads argument, so its inputs are read by multiple worker threads during the call, not just exposed to other Python threads

No behavior change, docs only. Linked from README.md so it's discoverable outside this PR.

Part of #9792 (R-9.5).

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.)

Please review this PR, and merge if it's suitable. Thanks !

Summary by CodeRabbit

  • Documentation
    • Added links to the wrapping guide and thread-safety guidance for binding development.
    • Documented thread-safety expectations for concurrent access, shared data, worker isolation, and streaming callbacks.
    • Clarified that releasing Python’s GIL does not guarantee safe concurrent access to OpenMS objects.
    • Added an inventory of GIL-release points and documented internally parallelized filtering behavior.
    • Added guidance for API-specific thread-safety documentation and avoiding OpenMP oversubscription in Python workers.

@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3457352d-29de-4da6-b2da-799d59d7c9ee

📥 Commits

Reviewing files that changed from the base of the PR and between 6b0f5b1 and 4cf0277.

📒 Files selected for processing (1)
  • src/pyOpenMS/THREAD_SAFETY.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/pyOpenMS/THREAD_SAFETY.md

📝 Walkthrough

Walkthrough

The PR adds pyOpenMS development links and a thread-safety contract. The contract covers concurrent access, shared storage, worker isolation, callbacks, GIL-release sites, filtering parallelism, OpenMP usage, and maintenance requirements.

Changes

pyOpenMS Documentation

Layer / File(s) Summary
Thread-safety guidance and navigation
src/pyOpenMS/THREAD_SAFETY.md, src/pyOpenMS/README.md
The README links to binding guidance and the thread-safety contract. The contract defines concurrency rules, documents 62 GIL-release sites, records the TransitionListEvidenceFilter.filter exception, specifies maintenance requirements, and adds OpenMP guidance.

Possibly related PRs

  • OpenMS/OpenMS#9794: Adds related pyOpenMS iterator-lifetime wrapping guidance and bindings.

Suggested reviewers: jpfeuffer

Poem

A rabbit checks each thread with care,
And links the binding guide with flair.
The GIL may pause or flow,
Shared objects need rules to know.
Workers hop in lanes apart,
Clear contracts guard each start.

🚥 Pre-merge checks | ✅ 5
✅ 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 identifies the documentation change for the pyOpenMS thread-safety and GIL-release contract.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 unit tests (beta)
  • Create PR with unit tests

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

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/pyOpenMS/THREAD_SAFETY.md`:
- Line 31: Update the concurrency note for TransitionListEvidenceFilter.filter
to state that worker threads concurrently read the active SWATH maps and derived
candidates/index data, while transition_exp is only read during candidate
construction before the threaded scan. Do not describe transition_exp as being
accessed by worker threads.
- Line 16: Update the streaming-reader callback statement in THREAD_SAFETY.md to
remove the universal GIL reacquisition guarantee, or scope it only to
streaming-reader APIs that explicitly invoke the gil_scoped_acquire path; retain
the guidance about synchronizing access to reader input/output objects.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0a1d956c-0961-4e83-ad73-6885f91540b5

📥 Commits

Reviewing files that changed from the base of the PR and between 6d09f84 and 30442f1.

📒 Files selected for processing (2)
  • src/pyOpenMS/README.md
  • src/pyOpenMS/THREAD_SAFETY.md

Comment thread src/pyOpenMS/THREAD_SAFETY.md Resolved
Comment thread src/pyOpenMS/THREAD_SAFETY.md Resolved
@aliviahossain

Copy link
Copy Markdown
Contributor Author

@jpfeuffer Please review this PR when you have time. Thanks !

@jpfeuffer

Copy link
Copy Markdown
Contributor

I guess it's fine. It's quite defensive. I think there are many read-only or partitioning style applications where it is safe to access a static MSExperiment (for example) via multiple threads.

One problem that is not really explained by your docs is the potential oversubscription of threads when calling OpenMS function that are internally parallelized by OpenMP. One should explain how to set the parallelism level of OpenMS to avoid that.

@aliviahossain

Copy link
Copy Markdown
Contributor Author

@jpfeuffer Pushed another commit, solving the concerns you raised. Please review that. Thanks !

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/pyOpenMS/THREAD_SAFETY.md`:
- Around line 13-14: Update the thread-safety guidance around the two bullets so
the read-only concurrency exception applies only to fully initialized types or
APIs that explicitly document concurrent read access, while retaining the
prohibition when any thread writes. Qualify the GIL-releasing-call rule to
prohibit concurrent access when the call may mutate the object or expose
writable aliased storage, and remove the broader implication of a general OpenMS
core thread-safety guarantee.
- Around line 35-40: Update the “Avoiding OpenMP oversubscription” guidance
around OpenMSBuildInfo.setOpenMPNumThreads so it describes the cap as
task-scoped rather than a single worker-global setting. State that, after
OMP_NUM_THREADS is unset, each Python worker should call setOpenMPNumThreads
before entering OpenMS-parallel work, and clarify that setting it only in the
parent process does not propagate independently to workers.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2c0e5ccd-5874-4172-b904-07abaf1fef96

📥 Commits

Reviewing files that changed from the base of the PR and between 6bf6142 and 6b0f5b1.

📒 Files selected for processing (1)
  • src/pyOpenMS/THREAD_SAFETY.md

Comment thread src/pyOpenMS/THREAD_SAFETY.md Resolved
Comment thread src/pyOpenMS/THREAD_SAFETY.md Resolved
@aliviahossain
aliviahossain force-pushed the docs-thread-safety-r9-5 branch from 1309ce3 to 4cf0277 Compare August 10, 2026 15:49
@jpfeuffer

Copy link
Copy Markdown
Contributor

yes better, thank you

@aliviahossain

Copy link
Copy Markdown
Contributor Author

@timosachsenberg This PR has been approved. You may merge it now.

@timosachsenberg
timosachsenberg merged commit 33593da into OpenMS:develop Aug 11, 2026
33 checks passed
timosachsenberg added a commit that referenced this pull request Aug 12, 2026
Routine changelog audit of PRs merged since the last CHANGELOG update
(#9902, merged 2026-08-11 06:20 UTC). Two PRs merged in that window:
#9857 and #9887.

- #9887 (ccache strategy/statistics in CI) is workflow-only with no
  code changes; its own description notes no CHANGELOG entry applies.
- #9857 added src/pyOpenMS/THREAD_SAFETY.md, a new pyOpenMS
  thread-safety/GIL-release contract document (part of #9792). Added
  a bullet under PyOpenMS: documenting it, consistent with the other
  #9792-related entries already in that section.

Co-authored-by: Claude <[email protected]>
@aliviahossain
aliviahossain deleted the docs-thread-safety-r9-5 branch October 28, 2026 11:51
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.

3 participants