Release api lock on connection close or finish

fix #4372