Ensure we don't leak erlang terms on failure, closes #2303