Flushing pending buffer before closing

Signed-off-by: Tsuyoshi Ozawa <ozawa.tsuyoshi@gmail.com>