Skip to content

gh-157672: Keep Python's signal handlers when Tk is initialized on macOS - #157673

Merged
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:gh-157672-tk-signals
Sep 23, 2026
Merged

serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:gh-157672-tk-signals

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

Tk on macOS replaces the SIGINT, SIGHUP and SIGTERM handlers with its own, which exits the process with status 1, so Ctrl-C terminated a tkinter program instead of raising KeyboardInterrupt, and Python-level handlers of SIGTERM and SIGHUP stopped working. Save the handlers installed by Python before Tk_Init() and restore them after it. Handlers which are SIG_DFL are not restored, which matches the fix on the Tk side (branch mac-signal-handlers): Tk's handler is only kept for signals which Python does not handle.

The test runs in a subprocess, because the process exits without the fix.

🤖 Generated with Claude Code

… on macOS

Tk on macOS replaces the SIGINT, SIGHUP and SIGTERM handlers with its
own, which exits the process, so Ctrl-C terminated a tkinter program
instead of raising KeyboardInterrupt.  Save the handlers installed by
Python before Tk_Init() and restore them after it.
@serhiy-storchaka

Copy link
Copy Markdown
Member Author

Thank you for merging the fix on upstream @culler. Here is a workaround on the Tkinter side. I think it will not harm to have both. On other hand, we build Python on macOS with specific versions of Tk, so if they contain that fix, it may be redundant.

@culler

culler commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

The patch was backported to Tk 9.0, which I guess means that the workaround will be become redundant on new Python releases. I agree that the redundancy will probably not cause any problems.

@serhiy-storchaka

Copy link
Copy Markdown
Member Author

I merged it in into core-8-6-branch as well.

But we cannot guarantee that the distributor will update Tcl/Tk to versions containing that fix.

@serhiy-storchaka
serhiy-storchaka merged commit 4532b36 into python:main Sep 23, 2026
62 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@serhiy-storchaka
serhiy-storchaka deleted the gh-157672-tk-signals branch September 23, 2026 10:09
@miss-islington-app

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.15 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 4532b36db5b9a43f63bf295aaf51df426e4c488d 3.15

@miss-islington-app

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 4532b36db5b9a43f63bf295aaf51df426e4c488d 3.14

@miss-islington-app

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 4532b36db5b9a43f63bf295aaf51df426e4c488d 3.13

@bedevere-app

bedevere-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

GH-157987 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 23, 2026
@bedevere-app

bedevere-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

GH-157988 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 Sep 23, 2026
@bedevere-app

bedevere-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Sep 23, 2026
serhiy-storchaka added a commit that referenced this pull request Sep 23, 2026
…d on macOS (GH-157673) (GH-157988)

Tk on macOS replaces the SIGINT, SIGHUP and SIGTERM handlers with its
own, which exits the process, so Ctrl-C terminated a tkinter program
instead of raising KeyboardInterrupt.  Save the handlers installed by
Python before Tk_Init() and restore them after it.

This affects Tk 8.6.11 to 8.6.18 and 9.0 to 9.0.4.  Tk itself is fixed
in 8.6.19 and 9.0.5.

(cherry picked from commit 4532b36)
serhiy-storchaka added a commit that referenced this pull request Sep 23, 2026
…d on macOS (GH-157673) (GH-157989)

Tk on macOS replaces the SIGINT, SIGHUP and SIGTERM handlers with its
own, which exits the process, so Ctrl-C terminated a tkinter program
instead of raising KeyboardInterrupt.  Save the handlers installed by
Python before Tk_Init() and restore them after it.

This affects Tk 8.6.11 to 8.6.18 and 9.0 to 9.0.4.  Tk itself is fixed
in 8.6.19 and 9.0.5.

(cherry picked from commit 4532b36)

On 3.13 there was no Tkinter_TkInit(), and tkappinit.c, which is used
when Python is built with WITH_APPINIT (as on macOS), called Tk_Init()
directly, bypassing the fix.
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