It is not safe to clean things up here.  If an exception is thrown while iterating, it will not complete the iteration and resources will never get cleaned up.

The Sequence interface provides ways of ensuring that things get closed, look at GroupByQueryEngine.process(), there's a cleanup method that you can use there which will be guaranteed to be called.