# INTERNAL: grpc: client-side streaming write failed: stream closed with error code 2

- **ID:** `grpc/client-side-streaming-write-failure`
- **Domain:** grpc
- **Category:** protocol_error
- **Error Code:** `2`
- **Verification:** ai_generated
- **Fix Rate:** 82%

## Root Cause

The gRPC client-side streaming RPC encountered a write failure because the server prematurely closed the stream due to an internal error or timeout, leaving the client unable to send further messages.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| gRPC v1.55.0 | active | — | — |
| gRPC v1.61.0 | active | — | — |
| gRPC v1.65.0 | active | — | — |

## Workarounds

1. **Implement a retry loop with backoff for the streaming RPC: `for attempt in range(3): try: call.write(msg); break; except Exception: time.sleep(0.5*2**attempt)`** (75% success)
   ```
   Implement a retry loop with backoff for the streaming RPC: `for attempt in range(3): try: call.write(msg); break; except Exception: time.sleep(0.5*2**attempt)`
   ```
2. **Check the server's stream deadline and increase it if too low: in server config, set `grpc.max_connection_age` or adjust client deadline to match.** (80% success)
   ```
   Check the server's stream deadline and increase it if too low: in server config, set `grpc.max_connection_age` or adjust client deadline to match.
   ```
3. **Add client-side logic to detect stream closure and recreate the stream: `if call.done(): call = stub.StreamingMethod()`** (70% success)
   ```
   Add client-side logic to detect stream closure and recreate the stream: `if call.done(): call = stub.StreamingMethod()`
   ```

## Dead Ends

- **** — Retrying the entire streaming RPC from scratch may work but is inefficient and does not prevent the server-side issue. (60% fail)
- **** — Reducing the client's message size does not help if the server closes the stream due to a logic error or resource limit. (50% fail)
- **** — Ignoring the error and continuing to write to the closed stream will cause a panic or crash. (90% fail)
