|
13 | 13 | import pickle |
14 | 14 | import random |
15 | 15 | import re |
| 16 | +import time |
16 | 17 | from collections import defaultdict |
17 | 18 | from functools import partial |
18 | 19 | from sys import platform |
@@ -3715,26 +3716,39 @@ def test_batched_nondynamic(self, penv): |
3715 | 3716 | use_buffers=True, |
3716 | 3717 | mp_start_method=mp_ctx if penv is ParallelEnv else None, |
3717 | 3718 | ) |
3718 | | - env_buffers.set_seed(0) |
3719 | | - torch.manual_seed(0) |
3720 | | - rollout_buffers = env_buffers.rollout( |
3721 | | - 20, return_contiguous=True, break_when_any_done=False |
3722 | | - ) |
3723 | | - del env_buffers |
| 3719 | + try: |
| 3720 | + env_buffers.set_seed(0) |
| 3721 | + torch.manual_seed(0) |
| 3722 | + rollout_buffers = env_buffers.rollout( |
| 3723 | + 20, return_contiguous=True, break_when_any_done=False |
| 3724 | + ) |
| 3725 | + finally: |
| 3726 | + env_buffers.close(raise_if_closed=False) |
| 3727 | + del env_buffers |
3724 | 3728 | gc.collect() |
| 3729 | + # Add a small delay to allow multiprocessing resource_sharer threads |
| 3730 | + # to fully clean up before creating the next environment. This prevents |
| 3731 | + # a race condition where the old resource_sharer service thread is still |
| 3732 | + # active when the new environment starts, causing a deadlock. |
| 3733 | + # See: https://bugs.python.org/issue30289 |
| 3734 | + if penv is ParallelEnv: |
| 3735 | + time.sleep(0.1) |
3725 | 3736 |
|
3726 | 3737 | env_no_buffers = penv( |
3727 | 3738 | 3, |
3728 | 3739 | lambda: GymEnv(CARTPOLE_VERSIONED(), device=None), |
3729 | 3740 | use_buffers=False, |
3730 | 3741 | mp_start_method=mp_ctx if penv is ParallelEnv else None, |
3731 | 3742 | ) |
3732 | | - env_no_buffers.set_seed(0) |
3733 | | - torch.manual_seed(0) |
3734 | | - rollout_no_buffers = env_no_buffers.rollout( |
3735 | | - 20, return_contiguous=True, break_when_any_done=False |
3736 | | - ) |
3737 | | - del env_no_buffers |
| 3743 | + try: |
| 3744 | + env_no_buffers.set_seed(0) |
| 3745 | + torch.manual_seed(0) |
| 3746 | + rollout_no_buffers = env_no_buffers.rollout( |
| 3747 | + 20, return_contiguous=True, break_when_any_done=False |
| 3748 | + ) |
| 3749 | + finally: |
| 3750 | + env_no_buffers.close(raise_if_closed=False) |
| 3751 | + del env_no_buffers |
3738 | 3752 | gc.collect() |
3739 | 3753 | assert_allclose_td(rollout_buffers, rollout_no_buffers) |
3740 | 3754 |
|
|
0 commit comments