monitor.py: Still write manifest if close errors

Before, on a headless server I was losing manifests with:

NullFunctionError: Attempt to call an undefined function glIsFramebuffer, check for bool(glIsFramebuffer) before calling (when closing Ant-v0)
This commit is contained in:
Greg Brockman
2016-04-27 19:35:33 -07:00
parent cf54e97686
commit dbde599c17
2 changed files with 40 additions and 4 deletions

View File

@@ -155,14 +155,14 @@ class Monitor(object):
# during video recording.
try:
self.env.render(close=True)
except Exception:
type, value, traceback = sys.exc_info()
except Exception as e:
if self.env.spec:
key = self.env.spec.id
else:
key = self.env
# This likely indicates unsupported kwargs
six.reraise(type, '{} (when closing {})'.format(value, key), traceback)
# We don't want to avoid writing the manifest simply
# because we couldn't close the renderer.
logger.warn('Could not close renderer for %s: %s'.format(key, e))
# Give it a very distiguished name, since we need to pick it
# up from the filesystem later.

View File

@@ -0,0 +1,36 @@
import contextlib
import glob
import os
import shutil
import tempfile
import gym
from gym.monitoring import monitor
class FakeEnv(gym.Env):
def _render(self, close=True):
raise RuntimeError('Raising')
@contextlib.contextmanager
def tempdir():
temp = tempfile.mkdtemp()
yield temp
shutil.rmtree(temp)
def test_monitor_filename():
with tempdir() as temp:
env = gym.make('Acrobot-v0')
env.monitor.start(temp)
env.monitor.close()
manifests = glob.glob(os.path.join(temp, '*.manifest.*'))
assert len(manifests) == 1
def test_close_monitor():
with tempdir() as temp:
env = FakeEnv()
env.monitor.start(temp)
env.monitor.close()
manifests = monitor.detect_training_manifests(temp)
assert len(manifests) == 1