Skip to content

Conversation

@robsdedude
Copy link
Contributor

@robsdedude robsdedude commented Feb 12, 2026

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


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


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

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?

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

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

Copy link
Contributor Author

@robsdedude robsdedude Feb 12, 2026

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

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

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

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
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
Copy link

python-cla-bot bot commented Feb 12, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM.

@ZeroIntensity ZeroIntensity 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:main Feb 12, 2026
37 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs Feb 12, 2026
@miss-islington-app
Copy link

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 945bf8ce1bf7ee3881752c2ecc129e35ab818477)

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

bedevere-app bot commented Feb 12, 2026

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

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Feb 12, 2026
@bedevere-app
Copy link

bedevere-app bot commented Feb 12, 2026

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

@bedevere-app bedevere-app bot 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
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