From f3950da009ef0ec06b3f55f5dc2223e3f80a9415 Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Tue, 20 Aug 2013 09:54:31 -0500 Subject: [PATCH] Unlock mutex in raw input reader error cases - BIT-1060 --- src/input/readers/Raw.cc | 32 ++++++++++++++++++++++---------- src/input/readers/Raw.h | 2 ++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/input/readers/Raw.cc b/src/input/readers/Raw.cc index fecf9a7ddc..e7a5af3e79 100644 --- a/src/input/readers/Raw.cc +++ b/src/input/readers/Raw.cc @@ -90,6 +90,24 @@ void Raw::ClosePipeEnd(int i) pipes[i] = -1; } +bool Raw::LockForkMutex() + { + int res = pthread_mutex_lock(&fork_mutex); + if ( res == 0 ) + return true; + Error(Fmt("cannot lock fork mutex: %d", res)); + return false; + } + +bool Raw::UnlockForkMutex() + { + int res = pthread_mutex_unlock(&fork_mutex); + if ( res == 0 ) + return true; + Error(Fmt("cannot unlock fork mutex: %d", res)); + return false; + } + bool Raw::Execute() { // TODO: AFAICT, pipe/fork/exec should be thread-safe, but actually having @@ -99,15 +117,12 @@ bool Raw::Execute() // individually or sequentially, that issue never crops up... ("never" // meaning I haven't seen in it in hundreds of tests using 50+ threads // where before I'd see the issue w/ just 2 threads ~33% of the time). - int lock_rval = pthread_mutex_lock(&fork_mutex); - if ( lock_rval != 0 ) - { - Error(Fmt("cannot lock fork mutex: %d", lock_rval)); + if ( ! LockForkMutex() ) return false; - } if ( pipe(pipes) != 0 || pipe(pipes+2) || pipe(pipes+4) ) { + UnlockForkMutex(); Error(Fmt("Could not open pipe: %d", errno)); return false; } @@ -115,6 +130,7 @@ bool Raw::Execute() childpid = fork(); if ( childpid < 0 ) { + UnlockForkMutex(); Error(Fmt("Could not create child process: %d", errno)); return false; } @@ -144,12 +160,8 @@ bool Raw::Execute() else { // we are the parent - lock_rval = pthread_mutex_unlock(&fork_mutex); - if ( lock_rval != 0 ) - { - Error(Fmt("cannot unlock fork mutex: %d", lock_rval)); + if ( ! UnlockForkMutex() ) return false; - } ClosePipeEnd(stdout_out); if ( Info().mode == MODE_STREAM ) diff --git a/src/input/readers/Raw.h b/src/input/readers/Raw.h index 8c05b54576..bd5c11acfd 100644 --- a/src/input/readers/Raw.h +++ b/src/input/readers/Raw.h @@ -32,6 +32,8 @@ protected: private: void ClosePipeEnd(int i); + bool LockForkMutex(); + bool UnlockForkMutex(); bool OpenInput(); bool CloseInput();