The origin of the issue was the change contributed in #1686 which added the loop in semihosting_get_gdb_response.
The check for an F-packet reply was too late as gdb_main_loop may have already clobbered the packet by the time this is checked.
Collaborative inspection of debug logs from #1845 and provided by others pointed to inconsistent behaviour dealing with remote file i/o completion, which was narrowed down to the F-packet reply check being done after the packet had been processed and the packet buffer possibly clobbered.
This is corrected by checking if the received packet is an F-packet first then processing it and using the result of the check to exit the loop after the processing.
Detailed description
Collaborative inspection of debug logs from #1845 and provided by others pointed to inconsistent behaviour dealing with remote file i/o completion, which was narrowed down to the F-packet reply check being done after the packet had been processed and the packet buffer possibly clobbered.
This is corrected by checking if the received packet is an F-packet first then processing it and using the result of the check to exit the loop after the processing.
Your checklist for this pull request
Closing issues
Fixes #1845.