From 26029ee232500a3a58464465319e61eb79b79c6f Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Mon, 22 Apr 2019 17:25:49 -0700 Subject: Prevent stale event from being delivered to client When an on-change sensor is activated, the most recent event is delivered to the client. A previous change attempted to ensure that any event that was generated prior to deactivating the sensor would not be delivered to a client upon activating the sensor. This was to ensure that an event that was generated in the past, and thus stale, would not be received by clients. The patch was successful in most cases, but it did not address the case of a client disabling a sensor and still maintained a reference to the SensorEventConnection through the NDK or VNDK. This patch marks the recent event as stale whenever a sensor transitions from deactivated to activated. This ensures that the recent event is always marked as stale, regardless of how the API to the Sensor Service is utilized. Bug: 116283108 Test: Verified that stale on-change event is not delivered to a client. Change-Id: Iee9a84ec57f5345fb14f0262b492ad100d05c7bb --- services/sensorservice/SensorService.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/services/sensorservice/SensorService.cpp b/services/sensorservice/SensorService.cpp index cbdd4739a3..02699906ed 100644 --- a/services/sensorservice/SensorService.cpp +++ b/services/sensorservice/SensorService.cpp @@ -1381,15 +1381,6 @@ void SensorService::cleanupConnection(SensorEventConnection* c) { ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); mActiveSensors.removeItemsAt(i, 1); mActiveVirtualSensors.erase(handle); - - // If this is the last connection, then mark the RecentEventLogger as stale. This is - // critical for on-change events since the previous event is sent to a client if the - // sensor is already active. If two clients request the sensor at the same time, one - // of the clients would receive a stale event. - auto logger = mRecentEvent.find(handle); - if (logger != mRecentEvent.end()) { - logger->second->setLastEventStale(); - } delete rec; size--; } else { @@ -1444,6 +1435,20 @@ status_t SensorService::enable(const sp& connection, if (sensor->isVirtual()) { mActiveVirtualSensors.emplace(handle); } + + // There was no SensorRecord for this sensor which means it was previously disabled. Mark + // the recent event as stale to ensure that the previous event is not sent to a client. This + // ensures on-change events that were generated during a previous sensor activation are not + // erroneously sent to newly connected clients, especially if a second client registers for + // an on-change sensor before the first client receives the updated event. Once an updated + // event is received, the recent events will be marked as current, and any new clients will + // immediately receive the most recent event. + if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) { + auto logger = mRecentEvent.find(handle); + if (logger != mRecentEvent.end()) { + logger->second->setLastEventStale(); + } + } } else { if (rec->addConnection(connection)) { // this sensor is already activated, but we are adding a connection that uses it. -- cgit v1.2.3-59-g8ed1b