Defining -DUSB_DUAL_SERIAL in teensy 4.1 build causes upload and monitor to fail

Using the USB_DUAL_SERIAL build flag in teensy41 builds on a Ubuntu 22.04 Linux system causes the Upload and Monitor task to fail.

Uploading .pio/build/ssd1306/firmware.hex
==================== [SUCCESS] Took 2.33 seconds ====================
Error: [Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: '/dev/ttyACM0'

 *  The terminal process "platformio 'run', '--target', 'upload', '--target', 'monitor', '--environment', 'ssd1306', '--upload-port', '/dev/ttyACM0', '--monitor-port', '/dev/ttyACM0'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

I believe this may be related to the teensy often enumerating the second USB CDC channel (02) before the first one (00). I have come across this before and tried to fix it by overriding the monitor port in platformio.ini to use /dev/serial/by-id/xxxxxxxxx but I did not get consistent behaviour.

I think platformio picks up the first serial device that is enumerated by udev and tries to use that as the monitor device without checking that it is the correct CDC channel, or it does not wait long enough for the device nodes and symlinks to settle down.

Any ideas on how to fix this?

Where do find the code that does this in the PlatformIO source?

Thanks

Roger

It works when “Upload” and “Monitor” tasks are executed separately?