Fixed the issues in stream close handling