How can I detect when packets are looping between switches, and how can I go about troubleshooting it? Detection...
may be simultaneous with a network crash. Some vendors have proprietary mechanisms built into switches to detect the loops and kick out packets before the problem degenerates. It may be possible to detect looping as the presence of traffic (see https://searchnetworking.techtarget.com/news/903169/Traffic-and-Everything-Like-Traffic-Dealing-with-Network-Performance-Degradation) although I can't say I have actually tried this.
To prevent looping, the answer is Spanning Trees. However this isn't found on your run-of-the-mill cheapo switch.
To quote from Internet sources:
"To prevent broadcast storms and other unwanted side effects of looping, Digital Equipment Corporation created the Spanning Tree Protocol (STP) which has been standardized as the 802.1d specification by the Institute of Electrical and Electronic Engineers (IEEE). Essentially, a spanning tree uses the spanning tree algorithm (STA) which senses that the switch has more than one way to communicate with a node, determines which way is the best and blocks out the other path(s). The cool thing is that it keeps track of the other path(s) just in case the primary path is unavailable."