handle: Call close() after write() even on write error

Otherwise the internal xmlDoc will leak.

https://bugzilla.gnome.org/show_bug.cgi?id=736825