# RuntimeError: Cannot write to closing transport

- **ID:** `python/aiohttp-cannot-write-after-response-started`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

aiohttp server tried to write to a StreamResponse after the client disconnected or the response was already finalized.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.8+ | active | — | — |
| 3.9+ | active | — | — |
| 3.10+ | active | — | — |
| 3.11+ | active | — | — |

## Workarounds

1. **** (90% success)
   ```
   if request.transport is None or request.transport.is_closing():
    return
await resp.write(chunk)
   ```
2. **** (88% success)
   ```
   resp = web.StreamResponse()
await resp.prepare(request)
try:
    async for chunk in source:
        await resp.write(chunk)
except (ConnectionResetError, RuntimeError):
    pass
finally:
    await resp.write_eof()
   ```

## Dead Ends

- **** — Silently drops data and may hide application logic bugs. (70% fail)
- **** — The transport is already closing; timeout is unrelated. (85% fail)
