Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 12170ae2 authored by David Duarte's avatar David Duarte
Browse files

PandoraServer: Replace usage of shutdownNow with shutdown

Calling shutdown let us stop accepting new incoming connections
and let the current ones finish.

This is safer for PandoraServer than shutdownNow because we needed
to call shutdownNow just after the completion of Reset as we
wanted to respond to the client before restarting the server.
But this could let the client make another call that the server
could accept before the server shutdown.
This should be prevented with shutdown as after shutdown
return to the caller the server should not accept incoming connections
anymore.

So now we stop accepting incoming connections before responding
to the client instead of after.

Bug: 244615429
Test: atest -v pts-bot --iterations 4
Change-Id: If58bfedc053168fcce2e4c6e1364344ae489e3d5
parent 2ab685b9
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -108,13 +108,12 @@ class Host(private val context: Context, private val server: Server) : HostImplB

      rebootBluetooth()

      Log.i(TAG, "Shutdown the gRPC Server")
      server.shutdown()

      // The last expression is the return value.
      Empty.getDefaultInstance()
    }
      .invokeOnCompletion {
        Log.i(TAG, "Shutdown the gRPC Server")
        server.shutdownNow()
      }
  }

  override fun softReset(request: Empty, responseObserver: StreamObserver<Empty>) {
+3 −1
Original line number Diff line number Diff line
@@ -48,7 +48,9 @@ class Main : MonitoringInstrumentation() {
    val context: Context = getApplicationContext()

    while (true) {
      Server(context).awaitTermination()
      val server = Server(context)
      server.awaitTermination()
      server.deinit()
    }
  }
}
+5 −4
Original line number Diff line number Diff line
@@ -57,15 +57,16 @@ class Server(context: Context) {
    Log.d(TAG, "Pandora Server started at $GRPC_PORT")
  }

  fun shutdownNow() {
  fun shutdown() = grpcServer.shutdown()

  fun awaitTermination() = grpcServer.awaitTermination()

  fun deinit() {
    host.deinit()
    a2dp.deinit()
    avrcp.deinit()
    gatt.deinit()
    hfp.deinit()
    security.deinit()
    grpcServer.shutdownNow()
  }

  fun awaitTermination() = grpcServer.awaitTermination()
}