Skip to content

Commit 937e16d

Browse files
PYTHON-2234: When mongocryptd spawn fails, the driver does not indicate what it tried to spawn (#596)
Co-authored-by: William Zhou <william.zhou@mongodb.com>
1 parent 8b444a7 commit 937e16d

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

pymongo/daemon.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
import subprocess
2424
import sys
2525
import time
26+
import warnings
27+
2628

2729
# The maximum amount of time to wait for the intermediate subprocess.
2830
_WAIT_TIMEOUT = 10
2931
_THIS_FILE = os.path.realpath(__file__)
3032

33+
3134
if sys.version_info[0] < 3:
3235
def _popen_wait(popen, timeout):
3336
"""Implement wait timeout support for Python 2."""
@@ -66,7 +69,9 @@ def _silence_resource_warning(popen):
6669
# "ResourceWarning: subprocess XXX is still running".
6770
# See https://bugs.python.org/issue38890 and
6871
# https://bugs.python.org/issue26741.
69-
popen.returncode = 0
72+
# popen is None when mongocryptd spawning fails
73+
if popen is not None:
74+
popen.returncode = 0
7075

7176

7277
if sys.platform == 'win32':
@@ -75,12 +80,17 @@ def _silence_resource_warning(popen):
7580

7681
def _spawn_daemon(args):
7782
"""Spawn a daemon process (Windows)."""
78-
with open(os.devnull, 'r+b') as devnull:
79-
popen = subprocess.Popen(
80-
args,
81-
creationflags=_DETACHED_PROCESS,
82-
stdin=devnull, stderr=devnull, stdout=devnull)
83-
_silence_resource_warning(popen)
83+
try:
84+
with open(os.devnull, 'r+b') as devnull:
85+
popen = subprocess.Popen(
86+
args,
87+
creationflags=_DETACHED_PROCESS,
88+
stdin=devnull, stderr=devnull, stdout=devnull)
89+
_silence_resource_warning(popen)
90+
except FileNotFoundError as exc:
91+
warnings.warn('Failed to start %s: is it on your $PATH?\n'
92+
'Original exception: %s' % (args[0], exc),
93+
RuntimeWarning, stacklevel=2)
8494
else:
8595
# On Unix we spawn the daemon process with a double Popen.
8696
# 1) The first Popen runs this file as a Python script using the current
@@ -95,12 +105,16 @@ def _spawn_daemon(args):
95105
# we spawn the mongocryptd daemon process.
96106
def _spawn(args):
97107
"""Spawn the process and silence stdout/stderr."""
98-
with open(os.devnull, 'r+b') as devnull:
99-
return subprocess.Popen(
100-
args,
101-
close_fds=True,
102-
stdin=devnull, stderr=devnull, stdout=devnull)
103-
108+
try:
109+
with open(os.devnull, 'r+b') as devnull:
110+
return subprocess.Popen(
111+
args,
112+
close_fds=True,
113+
stdin=devnull, stderr=devnull, stdout=devnull)
114+
except FileNotFoundError as exc:
115+
warnings.warn('Failed to start %s: is it on your $PATH?\n'
116+
'Original exception: %s' % (args[0], exc),
117+
RuntimeWarning, stacklevel=2)
104118

105119
def _spawn_daemon_double_popen(args):
106120
"""Spawn a daemon process using a double subprocess.Popen."""

0 commit comments

Comments
 (0)