close stream outside of synchronized block to avoid deadlock