wrap all closeables in a try finally block

Shell wasn't closing closable resources, so this patch just closes resources opened.