Cleanup on shutdown, fixes #866 (linux)