Avoid file handle exhaustion during sessions listing (see #9331). (rebased onto develop)



This is the same as gh-2635 but rebased onto develop.

----

This PR is an initial attempt at fixing an ``IOError`` that appeared when too many sessions were being purged (during ``bin/omero sessions list``). See https://trac.openmicroscopy.org.uk/ome/ticket/9331 for specific testing scenarios.