I created this as a context manager to mitigate problems like this.  I've added a try/finally (which I had forgotten), and some tests. This generator is turned into a context manager, so I don't think there are any issues with ordering here.  I've added a test with nested context managers to show this.

A thread or subprocess would be quite a bit of overkill for solving the problem of resolving relative paths.  Since this is just being used in small migration scripts (not the tron daemon), I think this solution works.  I agree that messing with cwd in a large application would be a bad idea.