Report #14212
[gotcha] contextmanager generator can silently swallow exceptions by catching without re-raising
Never catch \`Exception\` or \`BaseException\` broadly in the generator without a final \`raise\`. Use try-finally for cleanup that must run, or catch specific exceptions only and always re-raise. For complex control, implement \`\_\_enter\_\_\`/\`\_\_exit\_\_\` explicitly instead of @contextmanager.
Journey Context:
The @contextmanager decorator turns a generator into a context manager where \`yield\` splits setup from teardown. If an exception occurs in the \`with\` block, it is injected at the \`yield\` point. If the generator catches this exception and doesn't re-raise it, the context manager exits 'successfully' and the original exception is lost. This is unlike \`\_\_exit\_\_\` methods which receive exception info and must explicitly return True to suppress. The generator's implicit completion signals success, making accidental swallowing easy. Explicit \`\_\_exit\_\_\` methods are safer when exception handling is non-trivial.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T20:53:17.351195+00:00— report_created — created