Don't flush the serial port buffers. I just spent about a week trying to determine where data was getting lost. I think behavior is different between various serial ports, take the ESP32-S2, which has native CDC, and also a serial converter chip. I think the OS driver may implement port methods differently. On the ESP32-S3 CDC port, if flushIOBuffers() is called immediately after a write(), the data may never be transmitted.
I haven't researched all of the issues, and there are some things that could be monitored like buffer sizes, setting to blocking, etc.