Skip to content

Commit f799e1e

Browse files
authored
daemon: don't use Popen on linux/mac (iterative#4262)
We used `Popen` just to provide a general way of launching our daemons on both Windows and *nix, but it is causing us issues when dvc is launched from some exotic places (like nix package) or through `main()` helper from a script that is not dvc. This patch makes us just use `main()` directly in our double-forked process. Windows implementation has to use the `Popen` mechanism, because this is the only way to daemonize a process on Windows. This means that dvc on Windows is still susceptible to this problem, but there is nothing we can do about it right now :( Fixes iterative#4206 Fixes iterative#4165
1 parent d346b8e commit f799e1e

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

dvc/daemon.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
def _spawn_windows(cmd, env):
1919
from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW
2020

21+
prefix = [sys.executable]
22+
if not is_binary():
23+
prefix += [sys.argv[0]]
24+
2125
creationflags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS
2226

2327
startupinfo = STARTUPINFO()
2428
startupinfo.dwFlags |= STARTF_USESHOWWINDOW
2529

2630
Popen(
27-
cmd,
31+
prefix + cmd,
2832
env=env,
2933
close_fds=True,
3034
shell=False,
@@ -34,6 +38,8 @@ def _spawn_windows(cmd, env):
3438

3539

3640
def _spawn_posix(cmd, env):
41+
from dvc.main import main
42+
3743
# NOTE: using os._exit instead of sys.exit, because dvc built
3844
# with PyInstaller has trouble with SystemExit exception and throws
3945
# errors such as "[26338] Failed to execute script __main__"
@@ -59,7 +65,8 @@ def _spawn_posix(cmd, env):
5965
sys.stdout.close()
6066
sys.stderr.close()
6167

62-
Popen(cmd, env=env, close_fds=True, shell=False).communicate()
68+
os.environ.update(env)
69+
main(cmd)
6370

6471
os._exit(0) # pylint: disable=protected-access
6572

@@ -87,10 +94,7 @@ def daemon(args):
8794
logger.debug("skipping launching a new daemon.")
8895
return
8996

90-
cmd = [sys.executable]
91-
if not is_binary():
92-
cmd += [sys.argv[0]]
93-
cmd += ["daemon", "-q"] + args
97+
cmd = ["daemon", "-q"] + args
9498

9599
env = fix_env()
96100
file_path = os.path.abspath(inspect.stack()[0][1])

0 commit comments

Comments
 (0)