More reliable proxy cleanup on exit.

Ported from master (see #106).