#2910·taipy

`Gui.__exit__` Unconditionally Returns `True`, Illegitimately Swallowing All Exceptions in `with state:` and `with gui:`

Author: codeCraft-RitikCreated Sep 22, 2026Updated Sep 22, 2026

Bug : Gui.__exit__ Unconditionally Returns True, Illegitimately Swallowing All Exceptions in with state: and with gui:

1. File & Lines

2. Description & Real-World Impact

Taipy's official guide instructs developers to use with state: to batch variable modifications and send a single bulk update over WebSockets:

python
with state:
    state.var_a = 1
    state.var_b = 2

In Gui.__exit__:

python
def __exit__(self, exc_type, exc_value, traceback):
    try:
        self.__send_messages()
    except Exception as e:  # pragma: no cover
        _warn("Exception raised while sending messages", e)
    if exc_value:  # pragma: no cover
        _warn(f"An {exc_type or 'Exception'} was raised", exc_value)
    return True

Under Python PEP 343 (The "with" Statement):

"If __exit__ returns a true value, the exception is suppressed. If it returns false or None, the exception is re-raised."

Because Gui.__exit__ always returns True, any and all exceptions occurring inside a with state: or with gui: block are completely swallowed!

  • If a critical database query, calculation, or network call inside with state: fails with RuntimeError, KeyError, ValueError, or ZeroDivisionError, Python does not raise the error.
  • Enclosing try...except blocks will never receive the error.
  • Code following the with block continues executing under the false assumption that all operations succeeded, leading to silent state corruption in production dashboards.

3. Vulnerable Source Code

python
# taipy/gui/gui.py lines 1554-1562
def __exit__(self, exc_type, exc_value, traceback):
    try:
        self.__send_messages()
    except Exception as e:  # pragma: no cover
        _warn("Exception raised while sending messages", e)
    if exc_value:  # pragma: no cover
        _warn(f"An {exc_type or 'Exception'} was raised", exc_value)
    return True  # <--- CRITICAL BUG: Suppresses any exception in 'with state:'!

4. Minimal Reproduction Script

python
class MockGui:
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_value, tb):
        # Mirroring taipy.gui.gui.Gui.__exit__
        if exc_value:
            print(f"[Warning Logged]: {exc_value}")
        return True # Swallows exception!

gui = MockGui()

print("1. Starting batch update...")
try:
    with gui:
        print("2. Inside with state block...")
        raise ValueError("Critical financial calculation failed!")
        print("3. Never reached")
except ValueError:
    print("Caught error in outer try/except!")

print("4. Execution continued despite failure! Outer try/except was bypassed!")
# The exception was completely swallowed, leaving the app in an inconsistent state.

5. Proposed Fix (Git Patch)

diff
diff --git a/taipy/gui/gui.py b/taipy/gui/gui.py
index b7c8d9e..f0a1b2c 100644
--- a/taipy/gui/gui.py
+++ b/taipy/gui/gui.py
@@ -1558,7 +1558,8 @@ class Gui:
             _warn("Exception raised while sending messages", e)
         if exc_value:  # pragma: no cover
             _warn(f"An {exc_type or 'Exception'} was raised", exc_value)
-        return True
+            return False
+        return False
 
     def __hold_messages(self):
         grouping_message = self.__get_message_grouping()


Bonus Architectural Findings (Honorable Mentions)

Beyond the 4 primary defects, the audit revealed 2 additional high-value issues in Taipy Core & GUI:

Bonus A: Loss of Custom Encoders on JSONDataNode Deserialization & No-Op Setters

  • Files: taipy/core/data/_data_converter.py:204, 212 and taipy/core/data/json.py:118-130
  • Root Cause: When JSONDataNode is deserialized from the repository, _DataNodeConverter explicitly writes None for keys encoder and decoder. Because self._properties.get(key, _DefaultJSONEncoder) is used in JSONDataNode.__init__, if the key is present with value None, it evaluates to None rather than the default _DefaultJSONEncoder. Any Enum values fail to serialize on write. Furthermore, dn.encoder = NewEncoder setter only updates self.properties but does not update the cached self._encoder, making dynamic encoder reassignment a silent no-op.

Bonus B: Subsecond Timeout Ineffectiveness in Job Orchestrator

  • File: taipy/core/_orchestrator/_orchestrator.py:208-212
  • Root Cause: In _wait_until_job_finished:
    python
    def __check_if_timeout(st, to):
        if to is None:
            return True
        return (datetime.now() - st).seconds < to
    timedelta.seconds only returns integer seconds (0 to 86399) and truncates microseconds. For subsecond timeouts (e.g. timeout=0.5), (datetime.now() - st).seconds remains 0 for the entire first second (0 < 0.5 == True). The timeout is incapable of triggering earlier than 1.0 second. The standard fix is to use (datetime.now() - st).total_seconds() < to.