FAILURE MAP
← Case archive

FA-5201 / Context managers / Open access

Nested context unwinding: Registers cleanup before acquisition has succeeded · case 01

Registers cleanup before acquisition has succeeded and changes the required Python-language result.

Verified by executionVariant 1 · 4 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

Registers cleanup before acquisition has succeeded. The executable reproducer isolates this operation from the rest of the contract.

VERIFIED REPAIR

Enter A then B and exit acquired managers in reverse order. A manager whose __enter__ raises is not exited; previously entered managers still exit. Body errors unwind both.

Unsuccessful approach: The alternative still fails because it acquires managers in the reverse declaration order.

Case contract

Enter A then B and exit acquired managers in reverse order. A manager whose __enter__ raises is not exited; previously entered managers still exit. Body errors unwind both. Inputs use the finite Python types shown in the fixtures. An error string denotes the specified caught exception, not an unhandled process failure.

Why this case matters

Runs Python standard-library language operations in a deterministic local reproducer. Literal expected answers are authored independently. Related defects share an evaluation group to avoid benchmark split leakage.

1 / The failure

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(fail, body_error):
    log=[]; acquired=[]
    try:
        for name in ['A','B']:
            log.append('enter:'+name); acquired.append(name)
            if fail==name: raise ValueError(name)
        log.append('body')
        if body_error: raise KeyError('body')
    except Exception as exc:
        log.extend('exit:'+name for name in reversed(acquired))
        log.append(type(exc).__name__)
    else: log.extend('exit:'+name for name in reversed(acquired))
    return log
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('normal nesting', solve(None, False), ['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A'])
check('first enter fails', solve('A', False), ['enter:A', 'ValueError'])
check('second enter fails', solve('B', False), ['enter:A', 'enter:B', 'exit:A', 'ValueError'])
check('body unwinds both', solve(None, True), ['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A', 'KeyError'])
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
normal nesting['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A']['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A']Passed
first enter fails['enter:A', 'exit:A', 'ValueError']['enter:A', 'ValueError']Failed
second enter fails['enter:A', 'enter:B', 'exit:B', 'exit:A', 'ValueError']['enter:A', 'enter:B', 'exit:A', 'ValueError']Failed
body unwinds both['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A', 'KeyError']['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A', 'KeyError']Passed

SHA-256 / 5cf8aa054305c190ad28b95e43c7c9a21971b2671a1552c0d25dba2c5206246f

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(fail, body_error):
    log=[]
    class Manager:
        def __init__(self,name): self.name=name
        def __enter__(self):
            log.append('enter:'+self.name)
            if fail==self.name: raise ValueError(self.name)
            return self
        def __exit__(self,typ,exc,tb): log.append('exit:'+self.name); return False
    try:
        with Manager('B'), Manager('A'):
            log.append('body')
            if body_error: raise KeyError('body')
    except Exception as exc: log.append(type(exc).__name__)
    return log
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('normal nesting', solve(None, False), ['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A'])
check('first enter fails', solve('A', False), ['enter:A', 'ValueError'])
check('second enter fails', solve('B', False), ['enter:A', 'enter:B', 'exit:A', 'ValueError'])
check('body unwinds both', solve(None, True), ['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A', 'KeyError'])
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
normal nesting['enter:B', 'enter:A', 'body', 'exit:A', 'exit:B']['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A']Failed
first enter fails['enter:B', 'enter:A', 'exit:B', 'ValueError']['enter:A', 'ValueError']Failed
second enter fails['enter:B', 'ValueError']['enter:A', 'enter:B', 'exit:A', 'ValueError']Failed
body unwinds both['enter:B', 'enter:A', 'body', 'exit:A', 'exit:B', 'KeyError']['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A', 'KeyError']Failed

SHA-256 / a7686fc9b8574f5c5388af9fcb65a9ea58497778f0f00694301d9052296c6acb

3 / The verified repair

Exit 0
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(fail, body_error):
    log=[]
    class Manager:
        def __init__(self,name): self.name=name
        def __enter__(self):
            log.append('enter:'+self.name)
            if fail==self.name: raise ValueError(self.name)
            return self
        def __exit__(self,typ,exc,tb): log.append('exit:'+self.name); return False
    try:
        with Manager('A'), Manager('B'):
            log.append('body')
            if body_error: raise KeyError('body')
    except Exception as exc: log.append(type(exc).__name__)
    return log
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('normal nesting', solve(None, False), ['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A'])
check('first enter fails', solve('A', False), ['enter:A', 'ValueError'])
check('second enter fails', solve('B', False), ['enter:A', 'enter:B', 'exit:A', 'ValueError'])
check('body unwinds both', solve(None, True), ['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A', 'KeyError'])
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
normal nesting['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A']['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A']Passed
first enter fails['enter:A', 'ValueError']['enter:A', 'ValueError']Passed
second enter fails['enter:A', 'enter:B', 'exit:A', 'ValueError']['enter:A', 'enter:B', 'exit:A', 'ValueError']Passed
body unwinds both['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A', 'KeyError']['enter:A', 'enter:B', 'body', 'exit:B', 'exit:A', 'KeyError']Passed

SHA-256 / 3cfc64d2cd163d48503d274f83ab8b0b5d8e8d7cd25949cce96e9ae6429c4bf1

Verification & scope

This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.

Observations recorded using Python 3.12.14 at 2026-09-29T14:37:47.179130+00:00.

Case digest / dfc5315ac634094f979fc438179097fd9c93abd8a129ee93d4291756b582e6da