Taillights sometimes work, then they both go out, and come back

Check all the wiring and especially for a weak ground, I’d also look at the switch itself