Prevent memory leak for closed connections, see #2073