SHDP-408 Make sure writer is closed if exception is thrown