-
Notifications
You must be signed in to change notification settings - Fork 1
Ensure Win32 resize hooks removed during shutdown #3587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MelkorBalrog
wants to merge
1
commit into
main
Choose a base branch
from
codex/fix-tab-pop-out-crash-and-resize-issues-teag06
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
95 changes: 95 additions & 0 deletions
95
tests/detachment/window/test_closable_notebook_detach_resize.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Author: Miguel Marina <karel.capek.robotics@gmail.com> | ||
| # SPDX-License-Identifier: GPL-3.0-or-later | ||
| # | ||
| # Copyright (C) 2025 Capek System Safety & Robotic Solutions | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| """Regression tests for detaching tabs into floating windows.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import tkinter as tk | ||
| from tkinter import ttk | ||
|
|
||
| import pytest | ||
|
|
||
| from gui.utils.closable_notebook import ClosableNotebook | ||
|
|
||
|
|
||
| @pytest.mark.detachment | ||
| @pytest.mark.window_resizer | ||
| class TestClosableNotebookDetachment: | ||
| """Ensure detached tabs register with the floating window resizer.""" | ||
|
|
||
| def test_detach_tab_registers_resizer( | ||
| self, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| try: | ||
| root = tk.Tk() | ||
| except tk.TclError: | ||
| pytest.skip("Tk not available") | ||
|
|
||
| notebook = ClosableNotebook(root) | ||
| frame = tk.Frame(notebook) | ||
| notebook.add(frame, text="Diagram") | ||
|
|
||
| class StubWindow: | ||
| def __init__(self, *_args, **_kwargs) -> None: | ||
| self.win = tk.Toplevel(root) | ||
| self.nb = ttk.Notebook(self.win) | ||
| self.added: list[tuple[tk.Widget, str]] = [] | ||
|
|
||
| def add_moved_widget(self, widget: tk.Widget, text: str) -> None: | ||
| self.added.append((widget, text)) | ||
|
|
||
| def _ensure_toolbox(self, widget: tk.Widget) -> None: # noqa: ANN001 - stub API | ||
| self.added.append((widget, "toolbox")) | ||
|
|
||
| def _activate_hooks(self, widget: tk.Widget) -> None: # noqa: ANN001 - stub API | ||
| self.added.append((widget, "hooks")) | ||
|
|
||
| stub_windows: list[StubWindow] = [] | ||
| monkeypatch.setattr( | ||
| "gui.utils.closable_notebook.DetachedWindow", | ||
| lambda *args, **kwargs: stub_windows.append(StubWindow(*args, **kwargs)) | ||
| or stub_windows[-1], | ||
| ) | ||
|
|
||
| class StubManager: | ||
| def __init__(self) -> None: | ||
| self.calls: list[tuple[tk.Widget, str, tk.Widget]] = [] | ||
|
|
||
| def detach_tab( | ||
| self, source: tk.Widget, tab_id: str, target: tk.Widget | ||
| ) -> tk.Widget: | ||
| self.calls.append((source, tab_id, target)) | ||
| source.forget(tab_id) | ||
| target.add(frame, text="Diagram") | ||
| return frame | ||
|
|
||
| manager = StubManager() | ||
| monkeypatch.setattr( | ||
| "gui.utils.closable_notebook.WidgetTransferManager", lambda: manager | ||
| ) | ||
|
|
||
| notebook._detach_tab(str(frame), x=10, y=10) | ||
|
|
||
| assert stub_windows, "DetachedWindow should have been instantiated" | ||
| window = stub_windows[0] | ||
| assert window.added[0] == (frame, "Diagram") | ||
| assert manager.calls and manager.calls[0][0] is notebook | ||
|
|
||
| window.win.destroy() | ||
| root.destroy() |
54 changes: 54 additions & 0 deletions
54
tests/detachment/window/test_window_resizer_finalization.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Author: Miguel Marina <karel.capek.robotics@gmail.com> | ||
| # SPDX-License-Identifier: GPL-3.0-or-later | ||
| # | ||
| # Copyright (C) 2025 Capek System Safety & Robotic Solutions | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| """Finalization safety for the window resize controller.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import gui.utils.window_resizer as window_resizer | ||
|
|
||
|
|
||
| class _StubWindow: | ||
| def __init__(self) -> None: | ||
| self.unbind_calls: list[tuple[tuple[str, str], dict[str, str]]] = [] | ||
|
|
||
| def wm_resizable(self, *_args, **_kwargs) -> None: # pragma: no cover - noop | ||
| return None | ||
|
|
||
| def bind(self, *args, **_kwargs) -> str: # pragma: no cover - basic stub | ||
| return f"bind-{args[0]}" | ||
|
|
||
| def unbind(self, *args, **kwargs) -> None: # pragma: no cover - tracking only | ||
| self.unbind_calls.append((args, kwargs)) | ||
|
|
||
| def winfo_id(self) -> None: # pragma: no cover - disable Win32 hook install | ||
| return None | ||
|
|
||
|
|
||
| def test_shutdown_skips_finalizing(monkeypatch) -> None: | ||
| """Ensure shutdown avoids Tk/Win32 work during interpreter finalization.""" | ||
|
|
||
| win = _StubWindow() | ||
| monkeypatch.setattr(window_resizer, "_python_is_finalizing", lambda: False) | ||
| controller = window_resizer.WindowResizeController(win) | ||
| monkeypatch.setattr(window_resizer, "_python_is_finalizing", lambda: True) | ||
|
|
||
| controller.shutdown() | ||
|
|
||
| assert win.unbind_calls == [] | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
sys.is_finalizing()is true,_Win32WindowProcHook.__del__now drops the hook from_HOOKSand returns without callinguninstall, so the patched window procedure remains installed while the CFUNCTYPE callback is being torn down. During interpreter shutdownWindowResizeController.__del__also exits immediately (window_resizer.py:333-334), so hooks created by controllers are collected via this branch and never reach the new atexit cleanup (the hook has already been removed from_HOOKS). On Windows any late WM messages in that window tear-down window will then call a freed callback, risking crashes precisely during interpreter exit—the scenario this change was meant to prevent.Useful? React with 👍 / 👎.