Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiprocessing in SubprocVecEnv not thread-safe #217

Closed
AdamGleave opened this issue Mar 2, 2019 · 1 comment
Closed

Multiprocessing in SubprocVecEnv not thread-safe #217

AdamGleave opened this issue Mar 2, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@AdamGleave
Copy link
Collaborator

Describe the bug
On UNIX, SubprocVecEnv defaults to creating processes using the fork method. The Python docs warn that "safely forking a multithreaded process is problematic." This is since the child process inherits all objects from the parent, including -- for example -- TensorFlow sessions and graphs. In practice, this tends to manifest itself with difficult to diagnose deadlocks.

This is easily solved by switching the method to spawn (which is already used by default on Windows). This imposes a small fixed overhead to creating environments. I'll submit a PR that does this for discussion.

Code example
Running ci/local_tests.sh of this commit will reproduce this error (inside Docker, reproduced on my local machine and on Travis). If required I can try and create a more minimal breaking example, but I expect this will be difficult -- race conditions tend to be fickle.

The key ingredient to cause the problem is having created a session before calling SubprocVecEnv. This happens quite naturally in some contexts, e.g. population-based training, but does not occur in most code.

System Info

  • Docker; Ubuntu 16.04
  • Python 3.6.8
  • Tensorflow 1.12, CPU version
  • Stable Baselines 2.4.1, pip install
@araffin araffin added the enhancement New feature or request label Mar 6, 2019
@araffin
Copy link
Collaborator

araffin commented Mar 10, 2019

closed by #218

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants