Fix external camera HAL crashes

External USB camera hal service crashes when disconnecting the camera.
ExternalCameraDeviceSession's desctructor is called and deletes mLock,
but mBufferRequestThread is still running and waiting on the same
mLock, which causes the "pthread_mutex_lock called on a destroyed
mutex" error.

Fix the issue by stopping the thread in destructor. Also refactors the
functions and remove unused conditions.

Bug: 289741662
Test: Test with USB camera
Change-Id: I06b1ff6e192a4bca16822785d65d68a6aae53414
This commit is contained in:
Tang Lee
2023-08-01 18:23:07 +08:00
parent c350401b21
commit 65382f6a50
2 changed files with 14 additions and 14 deletions

View File

@@ -224,10 +224,6 @@ void ExternalCameraDeviceSession::initOutputThread() {
}
void ExternalCameraDeviceSession::closeOutputThread() {
closeOutputThreadImpl();
}
void ExternalCameraDeviceSession::closeOutputThreadImpl() {
if (mOutputThread != nullptr) {
mOutputThread->flush();
mOutputThread->requestExitAndWait();
@@ -235,6 +231,13 @@ void ExternalCameraDeviceSession::closeOutputThreadImpl() {
}
}
void ExternalCameraDeviceSession::closeBufferRequestThread() {
if (mBufferRequestThread != nullptr) {
mBufferRequestThread->requestExitAndWait();
mBufferRequestThread.reset();
}
}
Status ExternalCameraDeviceSession::initStatus() const {
Mutex::Autolock _l(mLock);
Status status = Status::OK;
@@ -248,7 +251,7 @@ Status ExternalCameraDeviceSession::initStatus() const {
ExternalCameraDeviceSession::~ExternalCameraDeviceSession() {
if (!isClosed()) {
ALOGE("ExternalCameraDeviceSession deleted before close!");
close(/*callerIsDtor*/ true);
closeImpl();
}
}
@@ -1411,19 +1414,16 @@ Status ExternalCameraDeviceSession::importBufferLocked(int32_t streamId, uint64_
}
ScopedAStatus ExternalCameraDeviceSession::close() {
close(false);
closeImpl();
return fromStatus(Status::OK);
}
void ExternalCameraDeviceSession::close(bool callerIsDtor) {
void ExternalCameraDeviceSession::closeImpl() {
Mutex::Autolock _il(mInterfaceLock);
bool closed = isClosed();
if (!closed) {
if (callerIsDtor) {
closeOutputThreadImpl();
} else {
closeOutputThread();
}
closeOutputThread();
closeBufferRequestThread();
Mutex::Autolock _l(mLock);
// free all buffers

View File

@@ -240,9 +240,9 @@ class ExternalCameraDeviceSession : public BnCameraDeviceSession, public OutputT
// To init/close different version of output thread
void initOutputThread();
void closeOutputThread();
void closeOutputThreadImpl();
void closeBufferRequestThread();
void close(bool callerIsDtor);
void closeImpl();
Status initStatus() const;
status_t initDefaultRequests();