Skip to content

Commit

Permalink
Fix __del__ in VectorEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
tristandeleu committed Jun 2, 2019
1 parent d3bd3bb commit 359bc59
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 10 additions & 2 deletions gym/vector/async_vector_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ class AsyncVectorEnv(VectorEnv):
context : str, optional
Context for multiprocessing. If `None`, then the default context is used.
Only available in Python 3.
"""
def __init__(self, env_fns, observation_space=None, action_space=None,
shared_memory=True, copy=True, context=None):
ctx = mp.get_context(context)
try:
ctx = mp.get_context(context)
except AttributeError:
logger.warn('Context switching for `multiprocessing` is not '
'available in Python 2. Using the default context.')
ctx = mp
self.env_fns = env_fns
self.shared_memory = shared_memory
self.copy = copy
Expand Down Expand Up @@ -299,7 +305,9 @@ def _raise_if_errors(self):
raise exctype(value)

def __del__(self):
self.close(terminate=True)
if hasattr(self, 'closed'):
if not self.closed:
self.close(terminate=True)


def _worker(index, env_fn, pipe, parent_pipe, shared_memory, error_queue):
Expand Down
4 changes: 3 additions & 1 deletion gym/vector/vector_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ def close(self, **kwargs):
self.closed = True

def __del__(self):
self.close()
if hasattr(self, 'closed'):
if not self.closed:
self.close()

0 comments on commit 359bc59

Please sign in to comment.