ard_newbie:
A workaround: Insert a watchdog reset in your code.
Yes, that works but it's lame. The USB stack on the Due has no business locking up when you try to write and the USB cord has gotten disconnected or the terminal program has closed. This is not really acceptable.
But, far from being just a complainer, I also fixed the problem. The issue is in LIBSAM. Which sucks because libsam is precompiled and everyone is using the compiled library that comes with board files. At any rate, UDD_Send which is a function in uotghs_device.c contains this little jem:
while( UOTGHS_DEVEPTISR_TXINI != (UOTGHS->UOTGHS_DEVEPTISR[ep] & UOTGHS_DEVEPTISR_TXINI )) {}
Yes, an infinite loop in the core USB sending function. NAUGHTY! If you close the terminal or pull the USB cord (obviously while also powering it from another source) this line will NEVER complete. The quick fix is to give it only a finite amount of time to wait and then give up. This works and the sketch can now try writing all it wants. But, depending on how long you set the timeout here it can waste a lot of time. Setting the timeout too long slows the program down. Setting it too short can cause writes to silently disappear.
I'm going to look to see if there is another register or status flag somewhere that I can read to see if things seem to be disconnected.