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

Commit 18ac4e0a authored by Himanshu Rawat's avatar Himanshu Rawat
Browse files

Avoid waiting indefinitely for BT stack cleanup. If stack cleanup takes up...

Avoid waiting indefinitely for BT stack cleanup. If stack cleanup takes up more than 1 second, proceed with BT service process exit.

Bug: 208971312
Test: Manual
Change-Id: Ia51d7067b772b56374d2a46ceb5ab6c6122ef2d4
parent d8365b3d
Loading
Loading
Loading
Loading
+19 −10
Original line number Diff line number Diff line
@@ -72,6 +72,10 @@
#include "internal_include/stack_config.h"
#include "main/shim/controller.h"

#ifndef BT_STACK_CLEANUP_WAIT_MS
#define BT_STACK_CLEANUP_WAIT_MS 1000
#endif

// Validate or respond to various conditional compilation flags

#if BLE_PRIVACY_SPT != TRUE
@@ -135,7 +139,7 @@ static bool stack_is_running;
static void event_init_stack(void* context);
static void event_start_up_stack(void* context);
static void event_shut_down_stack(void* context);
static void event_clean_up_stack(void* context);
static void event_clean_up_stack(std::promise<void> promise);

static void event_signal_stack_up(void* context);
static void event_signal_stack_down(void* context);
@@ -172,12 +176,18 @@ static void shut_down_stack_async() {
static void clean_up_stack() {
  // This is a synchronous process. Post it to the thread though, so
  // state modification only happens there.
  semaphore_t* semaphore = semaphore_new(0);
  management_thread.DoInThread(FROM_HERE,
                               base::Bind(event_clean_up_stack, semaphore));
  semaphore_wait(semaphore);
  semaphore_free(semaphore);
  std::promise<void> promise;
  auto future = promise.get_future();
  management_thread.DoInThread(
      FROM_HERE, base::BindOnce(event_clean_up_stack, std::move(promise)));

  auto status =
      future.wait_for(std::chrono::milliseconds(BT_STACK_CLEANUP_WAIT_MS));
  if (status == std::future_status::ready) {
    management_thread.ShutDown();
  } else {
    LOG_ERROR("cleanup could not be completed in time, abandon it");
  }
}

static bool get_stack_is_running() { return stack_is_running; }
@@ -414,7 +424,7 @@ static void ensure_stack_is_not_running() {
}

// Synchronous function to clean up the stack
static void event_clean_up_stack(void* context) {
static void event_clean_up_stack(std::promise<void> promise) {
  if (!stack_is_initialized) {
    LOG_INFO("%s found the stack already in a clean state", __func__);
    goto cleanup;
@@ -438,8 +448,7 @@ static void event_clean_up_stack(void* context) {
  LOG_INFO("%s finished", __func__);

cleanup:;
  semaphore_t* semaphore = (semaphore_t*)context;
  if (semaphore) semaphore_post(semaphore);
  promise.set_value();
}

static void event_signal_stack_up(UNUSED_ATTR void* context) {