Grid2Op: Garbage collection bug for forecast observation
Environment
- Grid2op version:
1.9.7 - System:
Springdale Open Enterprise Linux 9.2 (Parma)
Bug description
Appending an Observation object returned from forecast_obs = obs.forecast_env().reset() to a list class attribute in a function results in forecast_obs being garbage collected when we the function is exited. I believe that this is due to the fact that __del__ is being called on forecast_obs._obs_env when the function foo below is exited.
The error still persists if we remove LightSimBackend (i.e. use the default backend) or change the environment or if we copy forecast_obs before adding it to the list.
How to reproduce
Command line
Code snippet
import grid2op
from lightsim2grid import LightSimBackend
env_name = "l2rpn_wcci_2022"
env = grid2op.make(env_name, backend=LightSimBackend())
_ = env.reset()
class Manager:
obs_list = []
def foo():
obs = env.reset()
forecast_env = obs.get_forecast_env()
forecast_obs = forecast_env.reset()
Manager.obs_list.append(forecast_obs)
print(f"forecast_obs: {forecast_obs}")
print(f"[FOO] Manager.obs_list[0]._obs_env: {Manager.obs_list[0]._obs_env}")
print(f"[FOO] Manager.obs_list[0]._obs_env.backend is None: {Manager.obs_list[0]._obs_env.backend is None}")
if __name__ == '__main__':
foo()
print(f"Manager.obs_list[0]._obs_env: {Manager.obs_list[0]._obs_env}")
print(f"Manager.obs_list[0]._obs_env.backend is None: {Manager.obs_list[0]._obs_env.backend is None}")
Current output
forecast_obs: <grid2op.Space.GridObjects.ObservationWCCI2022_l2rpn_wcci_2022 object at 0x7f2f7e5f3e80>
[FOO] Manager.obs_list[0]._obs_env: <grid2op.Environment._obsEnv._ObsEnv_l2rpn_wcci_2022 object at 0x7f2f7e
5f3ee0>
[FOO] Manager.obs_list[0]._obs_env.backend is None: False
Manager.obs_list[0]._obs_env: <grid2op.Environment._obsEnv._ObsEnv_l2rpn_wcci_2022 object at 0x7f2f7e5f3ee0
>
Manager.obs_list[0]._obs_env.backend is None: True
Expected output
Manager.obs_list[0]._obs_env.backend is None: False
The backend should still remain even after we exit foo
About this issue
- Original URL
- State: closed
- Created 7 months ago
- Comments: 15
Commits related to this issue
- improving the doc to fix issue rte-france#568 [skip ci] — committed to BDonnot/Grid2Op by BDonnot 6 months ago
I see. I’ll try and modify the reward function for my purpose as I want to use reward functions that only use the observation