I'd put the closing calls into an `ensure` block so they are called also when an exception is raised in the block to avoid unclosed popups.