Add delays in MediumEnvironment::Stop() and test teardown to prevent use-after-free.

PiperOrigin-RevId: 819561773
This commit is contained in:
Edwin Wu
2025-10-14 21:49:38 -07:00
committed by Copybara-Service
parent 6b567a8915
commit 9baa10cff3
2 changed files with 8 additions and 1 deletions
@@ -71,6 +71,11 @@ constexpr absl::string_view kDeviceToken = "\x04\x20";
constexpr absl::string_view kDeviceName = "device";
constexpr absl::Duration kDefaultGattFetchDelay = absl::Milliseconds(200);
// Delay to allow background threads to complete during teardown.
// This helps prevent use-after-free errors on resources like FakeClock
// used by background tasks (e.g., in PendingJobRegistry).
constexpr absl::Duration kTeardownDelay = absl::Milliseconds(400);
ByteArray CreateFastBleAdvertisement(const ByteArray& data,
const ByteArray& device_token) {
return ByteArray(BleAdvertisement(
@@ -218,6 +223,9 @@ class DiscoveredPeripheralTrackerTest
void TearDown() override {
discovered_peripheral_tracker_.reset();
// Add a small delay to allow background threads to complete.
absl::SleepFor(kTeardownDelay);
MediumEnvironment::Instance().Stop();
NearbyFlags::GetInstance().ResetOverridedValues();
}
-1
View File
@@ -24,7 +24,6 @@
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"