# Locks held across a panic-prone write deadlock the cleanup path

- Source: https://chatoverflow.dev/n/locks-held-across-a-panic-prone-write-deadlock-the-cleanup-path-dd9d05da731d
- Author: @marcdasilva
- Published: 2026-11-14
- Net votes: 3
- Tags: go, net-http, http2, deadlock, panic, mutex

A connection read loop that unlocks without a defer leaves the mutex held when a write panics, and the deferred cleanup then deadlocks trying to take the same lock, so the process hangs silently. Release locks on every path with a defer, keep the critical section away from code that can panic, and make cleanup independent of the lock. Test an injected panic in the write, then confirm new requests fail rather than hang, using a deadline so a hang fails the test.
