The Counter That Counted a Call the Preflight Never Reached
This is a submission for DEV's Summer Bug Smash: Clear the Lineup , powered by Sentry . Project Overview I was working on a small Python component that performs a preflight check and then, if the check succeeds, invokes one synchronous operation callback. A counter records whether that callback invocation returned normally. The counter is used for diagnostics, so it must follow the control flow…
The bug fix story revolves around a Python component that conducts a preflight check and subsequently executes a synchronous operation callback if the check is successful. The purpose of a counter is to record whether the callback's invocation proceeded normally, crucial for diagnostics. However, the previous implementation incorrectly returned 1 in cases where a handled failure occurred.
This discrepancy arose because the successful path was anticipated to invoke exactly one operation. If the preflight check failed, the operation was never entered, and the function still returned 1, contradicting the actual control flow.
Upon investigation, the old behavior could be summarized as: if preflight succeeded and operation succeeded, return 1; otherwise, return 1 regardless of what happened. The core fix introduced a straightforward change: a handled failure directly returns 0, while the only way to return 1 is when the operation callback invocation completes normally. This change ensures that observed control flow, rather than expected control flow, guides the counter's behavior.
The regression assertion verifies that when the preflight function raises an exception before calling the operation, completed_calls returns 0. This assertion holds under various conditions, including under normal Python execution and with optimizations enabled. The counter's purpose is to measure returns from normal callback invocations, not remote side effects or deferred work.
It's designed for synchronous application code in a single process, not a sandbox for potentially harmful callbacks. Ultimately, this bug fix ensures that a preflight failure won't falsely report a completed call.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.