wrap iterations in try-finally to make sure we're closing the cursors
fixes #599