FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-144706: docs note about combining RLock and signal handlers by robsdedude · Pull Request #144736 · python/cpython · GitHub

Repository navigation

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

ZeroIntensity merged 6 commits into
python:mainfrom
robsdedude:docs/rlock-signal-note

Conversation

robsdedude commented Feb 12, 2026 •
edited by github-actions Bot
Loading

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 thread Doc/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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

surprising behavior

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

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread Doc/library/threading.rst Outdated
Comment thread Doc/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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
  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.

robsdedude Feb 12, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
  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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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 thread Doc/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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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 Bot commented Feb 12, 2026 •
edited
Loading

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks, LGTM.

ZeroIntensity added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes labels Feb 12, 2026
ZeroIntensity merged commit 945bf8c into python:main Feb 12, 2026
37 checks passed
github-project-automation Bot moved this from Todo to Done in Docs PRs Feb 12, 2026

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 Bot commented Feb 12, 2026

Copy link
Copy Markdown

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

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

bedevere-app Bot commented Feb 12, 2026

Copy link
Copy Markdown

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

bedevere-app Bot removed the needs backport to 3.13 only 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 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL