So, I’ve been working at integrating some more of our test code into our automated build system at work and have spent a day trying to figure out why everything was failing with the tests. They ran fine in my lab, they’d run fine individually but when I used rake to run them everything would bomb.

Eventually, I found out that all the rspec tests were being run twice. The first set of tests all passed the second set all failed. After a while I also realized the second set of tests were being run in the background. Odd, very odd.

Fast forward another hour, a lot of digging in the rspec code and I realized it’s because we fork inside the spec files. We fork and execute the server we want to test. rSpec sets up an at_exit handler to run the specs at exit if they haven’t been run. That at_exit handler would run number of forks + 1 times for each spec file.

I finally found Don’t get forked by rSpec, setup the at_exit { exit! } code in my spec files and everything is happy again.