From f06199bd874ab589310e4b20cb12bf542d018f72 Mon Sep 17 00:00:00 2001 From: Yi Hu Date: Fri, 30 Oct 2026 14:48:53 -0400 Subject: [PATCH] Fix broken GlobalWindow singleton `GlobalWindow` implements the singleton pattern via `__new__` by caching and returning `cls._instance`. However, because Python always invokes `__init__` on the instance returned by `__new__` (whenever `__new__` returns an instance of `cls`), defining `GlobalWindow.__init__` to call `super().__init__(GlobalWindow._getTimestampFromProto())` caused every `GlobalWindow()` call to re-parse the proto timestamp constant, allocate a new `Timestamp` object, and re-initialize `BoundedWindow._end`. Move the one-time `BoundedWindow.__init__` initialization into `GlobalWindow.__new__` when `cls._instance is None` and make `GlobalWindow.__init__` a no-op. --- sdks/python/apache_beam/transforms/window.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sdks/python/apache_beam/transforms/window.py b/sdks/python/apache_beam/transforms/window.py index 1a0c3007b4f1..b652c4c1f9cc 100644 --- a/sdks/python/apache_beam/transforms/window.py +++ b/sdks/python/apache_beam/transforms/window.py @@ -306,10 +306,14 @@ class GlobalWindow(BoundedWindow): def __new__(cls): if cls._instance is None: cls._instance = super(GlobalWindow, cls).__new__(cls) + super(GlobalWindow, + cls._instance).__init__(GlobalWindow._getTimestampFromProto()) return cls._instance def __init__(self) -> None: - super().__init__(GlobalWindow._getTimestampFromProto()) + # Intentionally a no-op: super().__init__ is called once in __new__ + # so repeated GlobalWindow() calls do not re-initialize the singleton. + pass def __repr__(self): return 'GlobalWindow'