grpc / grpc/grpc-java

IllegalStateException: transportShutdown() must be called before transportTerminated() when Android app goes to background

Open
#12,661 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
12.1k
Forks
4k
Avg merge
2d 17h
Merged PRs (30d)
37

Description

### What version of gRPC-Java are you using?
1.79.0

### What is your environment?

- **Operating System**: Android (tested on Android 16)
- **gRPC Transport**: grpc-okhttp (via AndroidChannelBuilder)
- **JDK**: Android Runtime
- **Dependencies**:
- grpc-android: 1.79.0
- grpc-okhttp: 1.79.0
- grpc-protobuf: 1.79.0
- grpc-kotlin-stub: 1.5.0

### What did you expect to see?
When the Android app is moved to the background, gRPC connections should gracefully handle the network state changes without crashing. The transport shutdown sequence should properly call `transportShutdown()` before `transportTerminated()`.

### What did you see instead?

The app consistently crashes approximately 5 seconds after being moved to the background:

```
FATAL EXCEPTION: OkHttpClientTransport
Process: com.notahotel.android.notahotel.dev, PID: 10823
java.lang.IllegalStateException: transportShutdown() must be called before transportTerminated().
at com.google.common.base.Preconditions.checkState(Preconditions.java:513)
at io.grpc.internal.InternalSubchannel$TransportListener.transportTerminated(InternalSubchannel.java:671)
at io.grpc.okhttp.OkHttpClientTransport$ClientFrameHandler.run(OkHttpClientTransport.java:1347)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1154)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:652)
at java.lang.Thread.run(Thread.java:1563)
```

**Observations**:
- The crash occurs on **multiple OkHttpClientTransport threads simultaneously**
- The timing is consistent: approximately **5 seconds after the app goes to background**
- **This issue does NOT occur with version 1.78.0** - only appears in 1.79.0

### Steps to reproduce the bug
1. Set up an Android app using AndroidChannelBuilder with grpc-java 1.79.0:
```kotlin
val channel = AndroidChannelBuilder
.forAddress(host, port)
.useTransportSecurity()
.context(applicationContext)
.build()
```
2. Establish one or more active gRPC connections
3. Move the app to the background (press home button or switch to another app)
4. Wait approximately 5 seconds
5. Observe the crash in logcat

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.