Skip to content

gh-144706: docs note about combining RLock and signal handlers - #144736

Merged
ZeroIntensity merged 6 commits into
python:mainfrom
robsdedude:docs/rlock-signal-note
Feb 12, 2026
Merged

gh-144706: docs note about combining RLock and signal handlers#144736
ZeroIntensity merged 6 commits into
python:mainfrom
robsdedude:docs/rlock-signal-note

Conversation

@robsdedude

@robsdeduderobsdedude commented Feb 12, 2026

Copy link
Copy Markdown
Contributor

Document potentially surprising interplay or threading.RLock and signal as discussed in #144706 (comment).


📚 Documentation preview 📚: https://cpython-previews--144736.org.readthedocs.build/

Comment threadDoc/library/threading.rst Outdated

Because lock acquisition can be interrupted by signals, sharing reentrant
locks between :mod:`signal` handlers and the main thread can lead to
surprising behavior. Therefore, this is generally not recommended.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

surprising behavior

any other surprise that is not main thread waiting forever for itself to release RLock?

@ZeroIntensityZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the note should go at the top of the file as a general warning about concurrency and signal handlers.

Comment threadDoc/library/threading.rst Outdated
Comment threadDoc/library/threading.rst Outdated
Comment on lines +845 to +847
Because lock acquisition can be interrupted by signals, sharing reentrant
locks between :mod:`signal` handlers and the main thread can lead to
surprising behavior. Therefore, this is generally not recommended.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  1. This isn't limited to re-entrant locks.
  2. "generally not recommended" is not strong enough -- I can't think of a case where it's thread-safe to use locks in signal handlers.

@robsdeduderobsdedudeFeb 12, 2026

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. You are right, I shall fix.
  2. sharing a synchronization primitive between the signal handler and some thread other than the main thread should work just fine, shouldn't it? Anyway, with regards to the main thread only, I can't think of a case either.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This isn't limited to re-entrant locks.

what is surprising about normal locks (like returned by _thread.allocate_lock())?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

sharing a synchronization primitive between the signal handler and some thread other than the main thread should work just fine, shouldn't it?

I think you're missing the point. It might work right now, but using locks in signal handlers is not something we want to encourage or claim to support. We may want to change how we handle signals or what they affect someday.

what is surprising about normal locks (like returned by _thread.allocate_lock())?

By acquiring a lock in a signal handler, the lock is subject to deadlocks for any signal handled by the main thread. For example:

  1. Main thread acquires a lock.
  2. User sends signal.
  3. The signal handler is invoked, which tries to acquire that lock.
  4. Deadlock!

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I think you're missing the point. It might work right now, but using locks in signal handlers is not something we want to encourage or claim to support. We may want to change how we handle signals or what they affect someday.

Alright. In such case, I think this warning belongs into the signal module instead.

Comment threadDoc/library/signal.rst Outdated
Comment on lines +73 to +75
Synchronization primitives such as :class:`threading.Lock` should not be used
within signal handlers. Because blocking synchronization calls can be
interrupted by signals, such usage can lead to surprising dead locks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we need to be specific:

Suggested change
Synchronization primitives such as :class:`threading.Lock` should not be used
within signal handlers. Because blocking synchronization calls can be
interrupted by signals, such usage can lead to surprising dead locks.
Synchronization primitives such as :class:`threading.Lock` should not be used
within signal handlers. Doing so can lead to deadlocks.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Thanks for the suggestion. I think if I were to read this for the first time I wouldn't understand the issue. And understanding something makes it easier to remember, too. I'd probably be thinking something like "well yeah, using synchronization primitives in general can lead to deadlocks. So what's different here?"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's say "unexpected deadlocks" then. The situation in the issue is one of many problems that come up when using synchronization primitives in signal handlers, so it's misleading to say that interruptibility is the only concern.

@python-cla-bot

python-cla-botBot commented Feb 12, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@ZeroIntensityZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, LGTM.

@ZeroIntensityZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Feb 12, 2026
@ZeroIntensity
ZeroIntensity merged commit 945bf8c into python:mainFeb 12, 2026
37 checks passed
@github-project-automationgithub-project-automationBot moved this from Todo to Done in Docs PRsFeb 12, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @robsdedude for the PR, and @ZeroIntensity for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 12, 2026
… signal handlers (pythonGH-144736)
(cherry picked from commit 945bf8c)
Co-authored-by: Robsdedude <dev@rouvenbauer.de>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 12, 2026
… signal handlers (pythonGH-144736)
(cherry picked from commit 945bf8c)
Co-authored-by: Robsdedude <dev@rouvenbauer.de>
@bedevere-app

Copy link
Copy Markdown

GH-144767 is a backport of this pull request to the 3.14 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.14 bugs and security fixes label Feb 12, 2026
@bedevere-app

Copy link
Copy Markdown

GH-144768 is a backport of this pull request to the 3.13 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.13 bugs and security fixes label Feb 12, 2026
ZeroIntensity pushed a commit that referenced this pull request Feb 12, 2026
…n signal handlers (GH-144736) (GH-144767)
gh-144706: Warn against using synchronization primitives within signal handlers (GH-144736)
(cherry picked from commit 945bf8c)
Co-authored-by: Robsdedude <dev@rouvenbauer.de>
ZeroIntensity pushed a commit that referenced this pull request Feb 12, 2026
…n signal handlers (GH-144736) (GH-144768)
gh-144706: Warn against using synchronization primitives within signal handlers (GH-144736)
(cherry picked from commit 945bf8c)
Co-authored-by: Robsdedude <dev@rouvenbauer.de>
@robsdedude
robsdedude deleted the docs/rlock-signal-note branch February 12, 2026 23:30
thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docsDocumentation in the Doc dirskip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

@robsdedude@ZeroIntensity@xBZZZZ