Fix resource warning (#1742)

* Fix resource warning

* Add test and update changelog

* Fix for new mypy version
This commit is contained in:
Antonin RAFFIN 2023-11-16 17:11:13 +01:00 committed by GitHub
parent b413f4c285
commit 23fbeb5975
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 26 deletions

View file

@ -3,7 +3,7 @@
Changelog Changelog
========== ==========
Release 2.2.0a11 (WIP) Release 2.2.0a12 (WIP)
-------------------------- --------------------------
**Support for options at reset, bug fixes and better error messages** **Support for options at reset, bug fixes and better error messages**
@ -32,6 +32,7 @@ Bug Fixes:
- Fixed success reward dtype in ``SimpleMultiObsEnv`` (@NixGD) - Fixed success reward dtype in ``SimpleMultiObsEnv`` (@NixGD)
- Fixed check_env for Sequence observation space (@corentinlger) - Fixed check_env for Sequence observation space (@corentinlger)
- Prevents instantiating BitFlippingEnv with conflicting observation spaces (@kylesayrs) - Prevents instantiating BitFlippingEnv with conflicting observation spaces (@kylesayrs)
- Fixed ResourceWarning when loading and saving models (files were not closed)
`SB3-Contrib`_ `SB3-Contrib`_
^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^

View file

@ -308,7 +308,7 @@ def save_to_zip_file(
:param pytorch_variables: Other PyTorch variables expected to contain name and value of the variable. :param pytorch_variables: Other PyTorch variables expected to contain name and value of the variable.
:param verbose: Verbosity level: 0 for no output, 1 for info messages, 2 for debug messages :param verbose: Verbosity level: 0 for no output, 1 for info messages, 2 for debug messages
""" """
save_path = open_path(save_path, "w", verbose=0, suffix="zip") with open_path(save_path, "w", verbose=0, suffix="zip") as save_path:
# data/params can be None, so do not # data/params can be None, so do not
# try to serialize them blindly # try to serialize them blindly
if data is not None: if data is not None:
@ -450,4 +450,6 @@ def load_from_zip_file(
except zipfile.BadZipFile as e: except zipfile.BadZipFile as e:
# load_path wasn't a zip file # load_path wasn't a zip file
raise ValueError(f"Error: the file {load_path} wasn't a zip-file") from e raise ValueError(f"Error: the file {load_path} wasn't a zip-file") from e
finally:
load_path.close()
return data, params, pytorch_variables return data, params, pytorch_variables

View file

@ -128,14 +128,14 @@ class SubprocVecEnv(VecEnv):
def step_wait(self) -> VecEnvStepReturn: def step_wait(self) -> VecEnvStepReturn:
results = [remote.recv() for remote in self.remotes] results = [remote.recv() for remote in self.remotes]
self.waiting = False self.waiting = False
obs, rews, dones, infos, self.reset_infos = zip(*results) obs, rews, dones, infos, self.reset_infos = zip(*results) # type: ignore[assignment]
return _flatten_obs(obs, self.observation_space), np.stack(rews), np.stack(dones), infos return _flatten_obs(obs, self.observation_space), np.stack(rews), np.stack(dones), infos # type: ignore[return-value]
def reset(self) -> VecEnvObs: def reset(self) -> VecEnvObs:
for env_idx, remote in enumerate(self.remotes): for env_idx, remote in enumerate(self.remotes):
remote.send(("reset", (self._seeds[env_idx], self._options[env_idx]))) remote.send(("reset", (self._seeds[env_idx], self._options[env_idx])))
results = [remote.recv() for remote in self.remotes] results = [remote.recv() for remote in self.remotes]
obs, self.reset_infos = zip(*results) obs, self.reset_infos = zip(*results) # type: ignore[assignment]
# Seeds and options are only used once # Seeds and options are only used once
self._reset_seeds() self._reset_seeds()
self._reset_options() self._reset_options()

View file

@ -1 +1 @@
2.2.0a11 2.2.0a12

View file

@ -747,3 +747,12 @@ def test_dqn_target_update_interval(tmp_path):
model = DQN.load(tmp_path / "dqn_cartpole") model = DQN.load(tmp_path / "dqn_cartpole")
os.remove(tmp_path / "dqn_cartpole.zip") os.remove(tmp_path / "dqn_cartpole.zip")
assert model.target_update_interval == 100 assert model.target_update_interval == 100
# Turn warnings into errors
@pytest.mark.filterwarnings("error")
def test_no_resource_warning(tmp_path):
# check that files are properly closed
# Create a PPO agent and save it
PPO("MlpPolicy", "CartPole-v1").save(tmp_path / "dqn_cartpole")
PPO.load(tmp_path / "dqn_cartpole")