fix(#710): init flserial mutexes before the reader thread can use them

fl_open started SerialThread and only then called mtx_init. Upstream's
thread locked a mutex only after its first read returned data, so the gap
rarely mattered; #710's room check locks inFifo_mutex on the thread's
first pass, which made the race reachable on every connect.

The mutexes are now initialised right after the FlSerial is allocated,
before open() can throw, so _fl_close_slot also never destroys an
uninitialised mutex after a failed open. Both copies stay identical.

Found by the pre-PR Gemini review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
feat/702-lossless-serial
diffwireauto 2 months ago
parent 845b0f1e55
commit 1d38767b72

@ -182,6 +182,11 @@ FFI_PLUGIN_EXPORT int fl_open(int flh, char *portname, int baudrate)
FlSerial *port = new FlSerial();
port->breakThread = 0;
// Before anything can fail or start the reader thread: the thread locks
// inFifo_mutex on its first pass, and _fl_close_slot destroys both even
// when open() threw.
mtx_init(&port->inFifo_mutex, mtx_plain);
mtx_init(&port->outFifo_mutex, mtx_plain);
flserial_tab[porth] = port;
@ -208,8 +213,6 @@ FFI_PLUGIN_EXPORT int fl_open(int flh, char *portname, int baudrate)
port->serialport->setParity(serial::parity_t::parity_none);
port->serialport->setStopbits(serial::stopbits_one);
thrd_create(&port->cthread, SerialThread, (void *)port);
mtx_init(&port->inFifo_mutex, mtx_plain);
mtx_init(&port->outFifo_mutex, mtx_plain);
}
catch (const serial::IOException &ioe)
{

@ -182,6 +182,11 @@ FFI_PLUGIN_EXPORT int fl_open(int flh, char *portname, int baudrate)
FlSerial *port = new FlSerial();
port->breakThread = 0;
// Before anything can fail or start the reader thread: the thread locks
// inFifo_mutex on its first pass, and _fl_close_slot destroys both even
// when open() threw.
mtx_init(&port->inFifo_mutex, mtx_plain);
mtx_init(&port->outFifo_mutex, mtx_plain);
flserial_tab[porth] = port;
@ -208,8 +213,6 @@ FFI_PLUGIN_EXPORT int fl_open(int flh, char *portname, int baudrate)
port->serialport->setParity(serial::parity_t::parity_none);
port->serialport->setStopbits(serial::stopbits_one);
thrd_create(&port->cthread, SerialThread, (void *)port);
mtx_init(&port->inFifo_mutex, mtx_plain);
mtx_init(&port->outFifo_mutex, mtx_plain);
}
catch (const serial::IOException &ioe)
{

Loading…
Cancel
Save

Powered by TurnKey Linux.