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>
This commit is contained in:
Prashant Mital 2021-04-21 15:53:00 -07:00 committed by GitHub
parent 8b444a73dd
commit 937e16d9ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,11 +23,14 @@ import os
import subprocess
import sys
import time
import warnings
# The maximum amount of time to wait for the intermediate subprocess.
_WAIT_TIMEOUT = 10
_THIS_FILE = os.path.realpath(__file__)
if sys.version_info[0] < 3:
def _popen_wait(popen, timeout):
"""Implement wait timeout support for Python 2."""
@ -66,7 +69,9 @@ def _silence_resource_warning(popen):
# "ResourceWarning: subprocess XXX is still running".
# See https://bugs.python.org/issue38890 and
# https://bugs.python.org/issue26741.
popen.returncode = 0
# popen is None when mongocryptd spawning fails
if popen is not None:
popen.returncode = 0
if sys.platform == 'win32':
@ -75,12 +80,17 @@ if sys.platform == 'win32':
def _spawn_daemon(args):
"""Spawn a daemon process (Windows)."""
with open(os.devnull, 'r+b') as devnull:
popen = subprocess.Popen(
args,
creationflags=_DETACHED_PROCESS,
stdin=devnull, stderr=devnull, stdout=devnull)
_silence_resource_warning(popen)
try:
with open(os.devnull, 'r+b') as devnull:
popen = subprocess.Popen(
args,
creationflags=_DETACHED_PROCESS,
stdin=devnull, stderr=devnull, stdout=devnull)
_silence_resource_warning(popen)
except FileNotFoundError as exc:
warnings.warn('Failed to start %s: is it on your $PATH?\n'
'Original exception: %s' % (args[0], exc),
RuntimeWarning, stacklevel=2)
else:
# On Unix we spawn the daemon process with a double Popen.
# 1) The first Popen runs this file as a Python script using the current
@ -95,12 +105,16 @@ else:
# we spawn the mongocryptd daemon process.
def _spawn(args):
"""Spawn the process and silence stdout/stderr."""
with open(os.devnull, 'r+b') as devnull:
return subprocess.Popen(
args,
close_fds=True,
stdin=devnull, stderr=devnull, stdout=devnull)
try:
with open(os.devnull, 'r+b') as devnull:
return subprocess.Popen(
args,
close_fds=True,
stdin=devnull, stderr=devnull, stdout=devnull)
except FileNotFoundError as exc:
warnings.warn('Failed to start %s: is it on your $PATH?\n'
'Original exception: %s' % (args[0], exc),
RuntimeWarning, stacklevel=2)
def _spawn_daemon_double_popen(args):
"""Spawn a daemon process using a double subprocess.Popen."""