While the part that I left off happened a long time ago so I didn't have that problem.
In general gap problems are tough and need some combination of hard work and luck to overcome. I had two times when I had gap problems.
For the first I was still processing my separation from physics, had spent two years as a nearly full-time activist (a bit unplanned) and had trouble with depression and chronic pain. I networked very hard and managed to get a new position created for me after about eight months of busting my ass.
For the second I'd spent a few years trying to start up my own business together with a salesman who couldn't sell anything. I gave up in December 2016 and rolled my car on the 31st, I created a workflow system that processed job listings and applications with plans to AI enable it. I saw a listing for a company that was doing something similar and was pretty sure I'd get the job and sure enough I did. They liked the story of my workflow system, and funny enough that code has been through various revisions and become an RSS reader and image sorter.
I'm not sure what looks worse: short stints at companies or gaps. I've tried the A/B test with my resume but the result are inconclusive.
I really hate the software industry. I like writing code, I like building stuff, I like math, I like a lot of my fellow engineers, but the entire industry is pretty insufferable at this point.