# ERROR at teardown of test_create_user
  File ".../conftest.py", line 42, in db
    conn.close()
psycopg2.OperationalError: server closed the connection unexpectedly

- **ID:** `python/pytest-fixture-finalizer-error-masked`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A fixture's teardown code (after yield) raises because the resource it cleans up (DB connection, temp server) was already closed by the test or by an earlier fixture. pytest reports this as a teardown ERROR, which can mask the original test result.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## Workarounds

1. **** (92% success)
   ```
   @pytest.fixture
def db():
    conn = connect()
    try:
        yield conn
    finally:
        if not conn.closed:
            conn.close()
   ```
2. **** (88% success)
   ```
   def test_query(db):
    with db.cursor() as cur:
        cur.execute('SELECT 1')
    # do not call db.close() here; the fixture owns it
   ```

## Dead Ends

- **** — Hides teardown failures but leaves leaked connections and masks real resource bugs. (70% fail)
- **** — Changes lifecycle and can cause cross-test contamination; the teardown error may still occur. (60% fail)
