Skip to content

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

@pike-hachi

Description

@pike-hachi

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:
val channel = AndroidChannelBuilder
    .forAddress(host, port)
    .useTransportSecurity()
    .context(applicationContext)
    .build()
  1. Establish one or more active gRPC connections
  2. Move the app to the background (press home button or switch to another app)
  3. Wait approximately 5 seconds
  4. Observe the crash in logcat

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions