libserial icon indicating copy to clipboard operation
libserial copied to clipboard

Release exclusive access to serial port on close

Open labradon opened this issue 1 year ago • 3 comments

After ending program execution and closing the opened serial port, the serial port could not be opened again because it is busy. After some investigation, this could be tracked down to the exclusive access to the serial port which is being set in Open(...).

To fix this, the serial port must be released from exclusive access in Close():

        // Release the serial port from exclusive access.
        if (mExclusiveAccess)
        {
            // NOLINTNEXTLINE (cppcoreguidelines-pro-type-vararg)
            if(call_with_retry(ioctl, this->mFileDescriptor, TIOCNXCL) == -1)
            {
                err_msg += ", ";
                err_msg += std::strerror(errno);
            }
        }

As an additional configuration option, I've added the exclusive flag that allows to specify whether to require exclusive access to the serial port or not.

labradon avatar Jul 25 '24 15:07 labradon

Hi @labradon , nice work. Apologies for the delay in review. I'll get a chance to run the unit tests on my hardware setup later this week.

@crayzeewulf , any concerns?

mcsauder avatar Oct 01 '24 15:10 mcsauder

@crayzeewulf , any concerns?

(Thanks, @labradon) Looks good overall @mcsauder . I would like to run some tests this week before merging too. Please give me a couple of days and I will report back with my test results.

crayzeewulf avatar Oct 01 '24 20:10 crayzeewulf

Hi @crayzeewulf . Existing unit tests continue to pass, but they were not capturing this particular case on my hardware on the master branch either. The PR seems good to me, but I'd defer to your experience here.

@labradon , can you reproduce the failure with the unit tests, or is there a unit test we can add to catch this case?

mcsauder avatar Oct 08 '24 01:10 mcsauder