Failed rendering leaves a stale current diagram context
Problem
If Graphviz rendering raises during Diagram.__exit__, the current diagram context is never cleared. After the exception is caught, getdiagram() still returns the failed diagram and a Node created outside any with Diagram(...) block is silently attached to it.
Reproduction
On master 0cd0d84016087eead0f7a5b747571b3f92b56726, Python 3.12/macOS and Graphviz 16.1.0:
from tempfile import TemporaryDirectory
from pathlib import Path
from graphviz import CalledProcessError
from diagrams import Diagram, Node, getdiagram
with TemporaryDirectory() as directory:
try:
with Diagram(filename=str(Path(directory) / 'broken'), show=False):
Node('<<invalid>>') # Graphviz rejects the malformed HTML label
except CalledProcessError:
pass
print(getdiagram()) # still the failed diagram
Node('outside diagram') # incorrectly succeedsExpected behavior
After leaving the context, getdiagram() should be None, and creating a node outside a diagram should raise EnvironmentError as usual. The original rendering exception should still propagate. Clearing the context in finally also covers output-file cleanup failures.
This matters in notebooks or long-lived processes that catch a rendering error and continue using diagrams in the same context.
Source: mingrammer/diagrams