From 5709cdfd25a55208da073f7cb5f15824a49cf1da Mon Sep 17 00:00:00 2001 From: Chris Taylor <89115007+hailsaturn@users.noreply.github.com> Date: Tue, 3 Nov 2026 17:46:40 +1100 Subject: [PATCH 1/3] Fix stale bindings from DynamicKeyBindings when ids are reused Previously DynamicKeyBindings used (id(kb), kb._version) for its version. Since id(kb) can be reused if kb is freed, it is possible for different objects with the same number of bindings to produce an identical version and _MergedKeyBindings was serving the stale bindings. The fix instead tracks a counter that increases whenever the callback returns a different object. --- src/prompt_toolkit/key_binding/key_bindings.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/prompt_toolkit/key_binding/key_bindings.py b/src/prompt_toolkit/key_binding/key_bindings.py index 5fd56394b..8e76da079 100644 --- a/src/prompt_toolkit/key_binding/key_bindings.py +++ b/src/prompt_toolkit/key_binding/key_bindings.py @@ -630,11 +630,14 @@ def __init__(self, get_key_bindings: Callable[[], KeyBindingsBase | None]) -> No self.__version = 0 self._last_child_version = None self._dummy = KeyBindings() # Empty key bindings. + self._bindings2 = self._dummy def _update_cache(self) -> None: key_bindings = self.get_key_bindings() or self._dummy assert isinstance(key_bindings, KeyBindingsBase) - version = id(key_bindings), key_bindings._version + if key_bindings is not self._bindings2: + self.__version += 1 + version = self.__version, key_bindings._version self._bindings2 = key_bindings self._last_version = version From 442cb5d38a1b8fc66941f59e6c9e760ec4496120 Mon Sep 17 00:00:00 2001 From: Chris Taylor <89115007+hailsaturn@users.noreply.github.com> Date: Tue, 3 Nov 2026 17:55:58 +1100 Subject: [PATCH 2/3] Removed unused variable _last_child_version from DynamicKeyBindings --- src/prompt_toolkit/key_binding/key_bindings.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/prompt_toolkit/key_binding/key_bindings.py b/src/prompt_toolkit/key_binding/key_bindings.py index 8e76da079..cfb5eb13c 100644 --- a/src/prompt_toolkit/key_binding/key_bindings.py +++ b/src/prompt_toolkit/key_binding/key_bindings.py @@ -628,7 +628,6 @@ class DynamicKeyBindings(_Proxy): def __init__(self, get_key_bindings: Callable[[], KeyBindingsBase | None]) -> None: self.get_key_bindings = get_key_bindings self.__version = 0 - self._last_child_version = None self._dummy = KeyBindings() # Empty key bindings. self._bindings2 = self._dummy From a93d63aa63a7f8ded718649a18d40bb52530de24 Mon Sep 17 00:00:00 2001 From: Chris Taylor <89115007+hailsaturn@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:47:39 +1100 Subject: [PATCH 3/3] Add test for DynamicKeyBindings serving stale bindings on reused ids --- tests/test_key_binding.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/test_key_binding.py b/tests/test_key_binding.py index 479559c61..118e7ca13 100644 --- a/tests/test_key_binding.py +++ b/tests/test_key_binding.py @@ -7,7 +7,11 @@ from prompt_toolkit.application import Application from prompt_toolkit.application.current import set_app from prompt_toolkit.input.defaults import create_pipe_input -from prompt_toolkit.key_binding.key_bindings import KeyBindings +from prompt_toolkit.key_binding.key_bindings import ( + DynamicKeyBindings, + KeyBindings, + merge_key_bindings, +) from prompt_toolkit.key_binding.key_processor import KeyPress, KeyProcessor from prompt_toolkit.keys import Keys from prompt_toolkit.layout import Layout, Window @@ -202,3 +206,27 @@ def handler(event): assert events[1].previous_key_sequence[0].data == "a" assert events[1].previous_key_sequence[1].key == "a" assert events[1].previous_key_sequence[1].data == "a" + + +def test_dynamic_key_bindings_cache_with_reused_id(monkeypatch): + """ + DynamicKeyBindings must not serve stale bindings when a new KeyBindings object reuses the address of a previous one + """ + monkeypatch.setattr( + "prompt_toolkit.key_binding.key_bindings.id", lambda _: 0, raising=False + ) + + pretend_database = [0] + + def build(): + kb = KeyBindings() + value = pretend_database[0] + kb.add("a")(lambda _: value) + return kb + + merged = merge_key_bindings([DynamicKeyBindings(build)]) + lookups = [] + for value in range(3): + pretend_database[0] = value + lookups.append(merged.get_bindings_for_keys(("a",))[-1].handler(None)) + assert lookups == [0, 1, 2]