I'm using platformio's ide and serial monitor, but I've also tried connecting to the board straight through putty and screen and got the same behavior.
I think I understand what you're saying.
When I connect through putty or screen the board resets, but it might continue running the last few lines before it resets? Which says to me that connecting to the board must send some kind of ACK SYN sequence that triggers the FOOBAR loop.