Fixed to flush pending buffers before closing

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