Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault during fast write-read on linux #4

Open
adixmasz opened this issue Aug 31, 2018 · 0 comments
Open

Segmentation fault during fast write-read on linux #4

adixmasz opened this issue Aug 31, 2018 · 0 comments

Comments

@adixmasz
Copy link

Hi,
in read function you don't use mutex. So there is a race condition. Code start from line 117 in file HidApi.cpp:

       bool isTimeout = true;
        while( (std::clock()-start) < end )
        {
            if( this->readAvailable() > 0 )
            {
                ret = this->readFifoBuffer.front();
                this->readFifoBuffer.pop();
                isTimeout = false;
                break;
            }
        }
    }

Segmentation fault can be rise in line:

                this->readFifoBuffer.pop();

when device thread is during change te readFifoBuffer.

Resolve:
I've make "pthread_mutex_t runningMutex;" public in class HidDeviceReaderThread.
Then I've change read function to this:

       bool isTimeout = true;
        while( (std::clock()-start) < end )
        {
            #ifdef OS_LINUX
            pthread_mutex_lock( &(this->backgroundReader->runningMutex));
            #endif

            if( this->readAvailable() > 0 )
            {
                ret = this->readFifoBuffer.front();
                this->readFifoBuffer.pop();
                isTimeout = false;

                #ifdef OS_LINUX
                pthread_mutex_unlock( &(this->backgroundReader->runningMutex));
                #endif
                break;
            }
            else
            {
                #ifdef OS_LINUX
                pthread_mutex_unlock( &(this->backgroundReader->runningMutex));
                #endif
            }
        }
    }

Now it works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant