grpc / grpc/grpc-java

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

Aperta
#12,661 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Java
Stelle
12.1k
Fork
4k
Merge medio
2g 17h
PR unite (30g)
37

Descrizione

### 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

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start with InternalSubchannel.java at TransportListener.transportTerminated and OkHttpClientTransport.java at ClientFrameHandler.run, using the reported stack trace as the entry point. Reproduce the Android background transition with grpc-java 1.79.0 and compare it with 1.78.0. Done means the transport lifecycle no longer crashes and transportShutdown() precedes transportTerminated().

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
android, java, kotlin
Ambito
mobile-dev, networking
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.