Here's how the upload process works on the Yun Rev. 2:
The Arduino IDE opens a 1200 baud connection to the Yun.
There is some background code running on the Yun's ATmega32U4 that sees the 1200 baud connection as a signal to reset the microcontroller.
After the reset, the bootloader runs. The bootloader presents its own CDC serial port and the computer's OS assigns it a COM port. The bootloader waits for an upload to start. If no upload starts then it times out and and runs the sketch.
After sending the reset signal, the Arduino IDE watches for a new port to appear. It assumes the new port is the Yun's bootloader.
The Arduino IDE starts the upload.
After the upload finishes, the bootloader exits and the sketch runs.
In both the working and non-working output, you can see the reset works because the sketch's COM port (COM6) disappears.
In the non-working output, no new COM port ever appears. It just eventually times out and runs the sketch, which makes COM6 appear again. The IDE tries to upload to COM6, but that can't work because the bootloader is not running on the board.
In the working sketch, you can see COM7 appears, which is the COM port of the bootloader and the IDE then uploads to that port.
So the question is why is the bootloader's COM port failing to enumerate sometimes. I can see two possible explanations:
- The bootloader is not working correctly. It must be working to some extent because it is able to time out and exit to the sketch.
- The bootloader is working correctly but the computer is failing to enumerate the COM port.