Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit 5e4c739

Browse files
Yifu Wangfacebook-github-bot
Yifu Wang
authored andcommitted
make allocators and sanitizers work for processes created with multiprocessing's spawn method in dev mode
Summary: **The first attempt (D30802446) overlooked that fact that the interpreter wrapper is not an executable (for `execv`) on Mac, and introduced some bugs due to the refactoring. The attempt 2 addressed the issues, and isolated the effect of the change to only processes created by multiprocess's spawn method on Linux.** #### Problem Currently, the entrypoint for in-place Python binaries (i.e. built with dev mode) executes the following steps to load system native dependencies (e.g. sanitizers and allocators): - Backup `LD_PRELOAD` set by the caller - Append system native dependencies to `LD_PRELOAD` - Inject a prologue in user code which restores `LD_PRELOAD` set by the caller - `execv` Python interpreter The steps work as intended for single process Python programs. However, when a Python program spawns child processes, the child processes will not load native dependencies, since they simply `execv`'s the vanilla Python interpreter. A few examples why this is problematic: - The ASAN runtime library is a system native dependency. Without loading it, a child process that loads user native dependencies compiled with ASAN will crash during static initialization because it can't find `_asan_init`. - `jemalloc` is also a system native dependency. Many if not most ML use cases "bans" dev mode because of these problems. It is very unfortunate considering the developer efficiency dev mode provides. In addition, a huge amount of unit tests have to run in a more expensive build mode because of these problems. For an earlier discussion, see [this post](https://fb.workplace.com/groups/fbpython/permalink/2897630276944987/). #### Solution Move the system native dependencies loading logic out of the Python binary entrypoint into an interpreter wrapper, and set the interpreter as `sys.executable` in the injected prologue: - The Python binary entrypoint now uses the interpreter wrapper, which has the same command line interface as the Python interpreter, to run the main module. - `multiprocessing`'s `spawn` method now uses the interpreter wrapper to create child processes, ensuring system native dependencies get loaded correctly. #### Alternative Considered One alternative considered is to simply not removing system native dependencies from `LD_PRELOAD`, so they are present in the spawned processes. However, this can cause some linking issues, which were perhaps the reason `LD_PRELOAD` was restored in the first place. #### References An old RFC for this change: D16210828 The counterpart for opt mode: D16350169 Reviewed By: fried fbshipit-source-id: 275a47ceeccec73703cdc5845b3caa72a5cd95b9
1 parent 499f461 commit 5e4c739

File tree

5 files changed

+297
-131
lines changed

5 files changed

+297
-131
lines changed

build.xml

+1
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@
10161016
<include name="com/facebook/buck/maven/build-file.st"/>
10171017
<include name="com/facebook/buck/python/*.py"/>
10181018
<include name="com/facebook/buck/python/run_inplace.py.in"/>
1019+
<include name="com/facebook/buck/python/run_inplace_interpreter_wrapper.py.in"/>
10191020
<include name="com/facebook/buck/python/run_inplace_lite.py.in"/>
10201021
<include name="com/facebook/buck/parser/function/BuckPyFunction.stg"/>
10211022
<include name="com/facebook/buck/shell/sh_binary_template"/>

src/com/facebook/buck/features/python/BUCK

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ java_library_with_plugins(
6969
"__test_main__.py",
7070
"compile.py",
7171
"run_inplace.py.in",
72+
"run_inplace_interpreter_wrapper.py.in",
7273
"run_inplace_lite.py.in",
7374
],
7475
tests = [

src/com/facebook/buck/features/python/PythonInPlaceBinary.java

+95-23
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818

1919
import com.facebook.buck.core.build.buildable.context.BuildableContext;
2020
import com.facebook.buck.core.build.context.BuildContext;
21+
import com.facebook.buck.core.filesystems.AbsPath;
2122
import com.facebook.buck.core.filesystems.RelPath;
2223
import com.facebook.buck.core.model.BuildTarget;
2324
import com.facebook.buck.core.model.OutputLabel;
2425
import com.facebook.buck.core.model.TargetConfiguration;
26+
import com.facebook.buck.core.model.impl.BuildTargetPaths;
2527
import com.facebook.buck.core.rulekey.AddToRuleKey;
2628
import com.facebook.buck.core.rules.BuildRule;
2729
import com.facebook.buck.core.rules.BuildRuleResolver;
2830
import com.facebook.buck.core.rules.attr.HasRuntimeDeps;
2931
import com.facebook.buck.core.rules.impl.SymlinkTree;
32+
import com.facebook.buck.core.sourcepath.ExplicitBuildTargetSourcePath;
3033
import com.facebook.buck.core.toolchain.tool.Tool;
3134
import com.facebook.buck.core.toolchain.tool.impl.CommandTool;
3235
import com.facebook.buck.cxx.toolchain.CxxPlatform;
@@ -39,6 +42,7 @@
3942
import com.facebook.buck.step.Step;
4043
import com.facebook.buck.step.fs.MkdirStep;
4144
import com.facebook.buck.step.isolatedsteps.common.WriteFileIsolatedStep;
45+
import com.facebook.buck.test.selectors.Nullable;
4246
import com.facebook.buck.util.Escaper;
4347
import com.facebook.buck.util.stream.RichStream;
4448
import com.google.common.base.Joiner;
@@ -57,6 +61,8 @@
5761
public class PythonInPlaceBinary extends PythonBinary implements HasRuntimeDeps {
5862

5963
private static final String RUN_INPLACE_RESOURCE = "run_inplace.py.in";
64+
private static final String RUN_INPLACE_INTERPRETER_WRAPPER_RESOURCE =
65+
"run_inplace_interpreter_wrapper.py.in";
6066
private static final String RUN_INPLACE_LITE_RESOURCE = "run_inplace_lite.py.in";
6167

6268
// TODO(agallagher): Task #8098647: This rule has no steps, so it
@@ -68,8 +74,10 @@ public class PythonInPlaceBinary extends PythonBinary implements HasRuntimeDeps
6874
//
6975
// We should upate the Python test rule to account for this.
7076
private final SymlinkTree linkTree;
77+
private final RelPath interpreterWrapperGenPath;
7178
@AddToRuleKey private final Tool python;
72-
@AddToRuleKey private final Supplier<String> script;
79+
@AddToRuleKey private final Supplier<String> binScript;
80+
@AddToRuleKey private final Supplier<String> interpreterWrapperScript;
7381

7482
PythonInPlaceBinary(
7583
BuildTarget buildTarget,
@@ -98,18 +106,28 @@ public class PythonInPlaceBinary extends PythonBinary implements HasRuntimeDeps
98106
legacyOutputPath);
99107
this.linkTree = linkTree;
100108
this.python = python;
101-
this.script =
102-
getScript(
109+
this.interpreterWrapperGenPath =
110+
getInterpreterWrapperGenPath(
111+
buildTarget, projectFilesystem, pexExtension, legacyOutputPath);
112+
AbsPath targetRoot =
113+
projectFilesystem
114+
.resolve(getBinPath(buildTarget, projectFilesystem, pexExtension, legacyOutputPath))
115+
.getParent();
116+
this.binScript =
117+
getBinScript(
118+
pythonPlatform,
119+
mainModule,
120+
targetRoot.relativize(linkTree.getRoot()),
121+
targetRoot.relativize(projectFilesystem.resolve(interpreterWrapperGenPath)),
122+
packageStyle);
123+
this.interpreterWrapperScript =
124+
getInterpreterWrapperScript(
103125
ruleResolver,
104126
buildTarget.getTargetConfiguration(),
105127
pythonPlatform,
106128
cxxPlatform,
107-
mainModule,
108129
components,
109-
projectFilesystem
110-
.resolve(getBinPath(buildTarget, projectFilesystem, pexExtension, legacyOutputPath))
111-
.getParent()
112-
.relativize(linkTree.getRoot()),
130+
targetRoot.relativize(linkTree.getRoot()),
113131
preloadLibraries,
114132
packageStyle);
115133
}
@@ -123,6 +141,10 @@ private static String getRunInplaceResource() {
123141
return getNamedResource(RUN_INPLACE_RESOURCE);
124142
}
125143

144+
private static String getRunInplaceInterpreterWrapperResource() {
145+
return getNamedResource(RUN_INPLACE_INTERPRETER_WRAPPER_RESOURCE);
146+
}
147+
126148
private static String getRunInplaceLiteResource() {
127149
return getNamedResource(RUN_INPLACE_LITE_RESOURCE);
128150
}
@@ -136,29 +158,64 @@ private static String getNamedResource(String resourceName) {
136158
}
137159
}
138160

139-
private static Supplier<String> getScript(
161+
private static RelPath getInterpreterWrapperGenPath(
162+
BuildTarget target,
163+
ProjectFilesystem filesystem,
164+
String extension,
165+
boolean legacyOutputPath) {
166+
if (!legacyOutputPath) {
167+
target = target.withFlavors();
168+
}
169+
return BuildTargetPaths.getGenPath(
170+
filesystem.getBuckPaths(), target, "%s#interpreter_wrapper" + extension);
171+
}
172+
173+
private static Supplier<String> getBinScript(
174+
PythonPlatform pythonPlatform,
175+
String mainModule,
176+
RelPath linkTreeRoot,
177+
RelPath interpreterWrapperPath,
178+
PackageStyle packageStyle) {
179+
return () -> {
180+
String linkTreeRootStr = Escaper.escapeAsPythonString(linkTreeRoot.toString());
181+
String interpreterWrapperPathStr =
182+
Escaper.escapeAsPythonString(interpreterWrapperPath.toString());
183+
return new ST(
184+
new STGroup(),
185+
packageStyle == PackageStyle.INPLACE
186+
? getRunInplaceResource()
187+
: getRunInplaceLiteResource())
188+
.add("PYTHON", pythonPlatform.getEnvironment().getPythonPath())
189+
.add("PYTHON_INTERPRETER_FLAGS", pythonPlatform.getInplaceBinaryInterpreterFlags())
190+
.add("MODULES_DIR", linkTreeRootStr)
191+
.add("MAIN_MODULE", Escaper.escapeAsPythonString(mainModule))
192+
.add("INTERPRETER_WRAPPER_REL_PATH", interpreterWrapperPathStr)
193+
.render();
194+
};
195+
}
196+
197+
@Nullable
198+
private static Supplier<String> getInterpreterWrapperScript(
140199
BuildRuleResolver resolver,
141200
TargetConfiguration targetConfiguration,
142201
PythonPlatform pythonPlatform,
143202
CxxPlatform cxxPlatform,
144-
String mainModule,
145203
PythonPackageComponents components,
146204
RelPath relativeLinkTreeRoot,
147205
ImmutableSet<String> preloadLibraries,
148206
PackageStyle packageStyle) {
149207
String relativeLinkTreeRootStr = Escaper.escapeAsPythonString(relativeLinkTreeRoot.toString());
150208
Linker ld = cxxPlatform.getLd().resolve(resolver, targetConfiguration);
209+
// Lite mode doesn't need an interpreter wrapper as there's no LD_PRELOADs involved.
210+
if (packageStyle != PackageStyle.INPLACE) {
211+
return null;
212+
}
151213
return () -> {
152214
ST st =
153-
new ST(
154-
new STGroup(),
155-
packageStyle == PackageStyle.INPLACE
156-
? getRunInplaceResource()
157-
: getRunInplaceLiteResource())
215+
new ST(new STGroup(), getRunInplaceInterpreterWrapperResource())
158216
.add("PYTHON", pythonPlatform.getEnvironment().getPythonPath())
159-
.add("MAIN_MODULE", Escaper.escapeAsPythonString(mainModule))
160-
.add("MODULES_DIR", relativeLinkTreeRootStr)
161-
.add("PYTHON_INTERPRETER_FLAGS", pythonPlatform.getInplaceBinaryInterpreterFlags());
217+
.add("PYTHON_INTERPRETER_FLAGS", pythonPlatform.getInplaceBinaryInterpreterFlags())
218+
.add("MODULES_DIR", relativeLinkTreeRootStr);
162219

163220
// Only add platform-specific values when the binary includes native libraries.
164221
if (components.getNativeLibraries().getComponents().isEmpty()) {
@@ -187,11 +244,26 @@ public ImmutableList<Step> getBuildSteps(
187244
BuildContext context, BuildableContext buildableContext) {
188245
RelPath binPath = context.getSourcePathResolver().getCellUnsafeRelPath(getSourcePathToOutput());
189246
buildableContext.recordArtifact(binPath.getPath());
190-
return ImmutableList.of(
191-
MkdirStep.of(
192-
BuildCellRelativePath.fromCellRelativePath(
193-
context.getBuildCellRootPath(), getProjectFilesystem(), binPath.getParent())),
194-
WriteFileIsolatedStep.of(script, binPath, /* executable */ true));
247+
ImmutableList.Builder<Step> stepsBuilder = new ImmutableList.Builder<Step>();
248+
stepsBuilder
249+
.add(
250+
MkdirStep.of(
251+
BuildCellRelativePath.fromCellRelativePath(
252+
context.getBuildCellRootPath(), getProjectFilesystem(), binPath.getParent())))
253+
.add(WriteFileIsolatedStep.of(binScript, binPath, /* executable */ true));
254+
255+
if (interpreterWrapperScript != null) {
256+
RelPath interpreterWrapperPath =
257+
context
258+
.getSourcePathResolver()
259+
.getCellUnsafeRelPath(
260+
ExplicitBuildTargetSourcePath.of(getBuildTarget(), interpreterWrapperGenPath));
261+
buildableContext.recordArtifact(interpreterWrapperPath.getPath());
262+
stepsBuilder.add(
263+
WriteFileIsolatedStep.of(
264+
interpreterWrapperScript, interpreterWrapperPath, /* executable */ true));
265+
}
266+
return stepsBuilder.build();
195267
}
196268

197269
@Override

src/com/facebook/buck/features/python/run_inplace.py.in

+15-108
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ import subprocess
77
import sys
88

99
main_module = <MAIN_MODULE>
10-
modules_dir = <MODULES_DIR>
11-
native_libs_env_var = <NATIVE_LIBS_ENV_VAR>
12-
native_libs_dir = <NATIVE_LIBS_DIR>
13-
native_libs_preload_env_var = <NATIVE_LIBS_PRELOAD_ENV_VAR>
14-
native_libs_preload = <NATIVE_LIBS_PRELOAD>
1510

1611
def try_resolve_possible_symlink(path):
1712
import ctypes
@@ -63,26 +58,6 @@ if platform.system() == "Windows":
6358
# does *not* dereference symlinks on windows until, like, 3.8 maybe.
6459
dirpath = os.path.dirname(try_resolve_possible_symlink(sys.argv[0]))
6560

66-
env_vals_to_restore = {}
67-
# Update the environment variable for the dynamic loader to the native
68-
# libraries location.
69-
if native_libs_dir is not None:
70-
old_native_libs_dir = os.environ.get(native_libs_env_var)
71-
os.environ[native_libs_env_var] = os.path.join(dirpath, native_libs_dir)
72-
env_vals_to_restore[native_libs_env_var] = old_native_libs_dir
73-
74-
# Update the environment variable for the dynamic loader to find libraries
75-
# to preload.
76-
if native_libs_preload is not None:
77-
old_native_libs_preload = os.environ.get(native_libs_preload_env_var)
78-
env_vals_to_restore[native_libs_preload_env_var] = old_native_libs_preload
79-
80-
# On macos, preloaded libs are found via paths.
81-
os.environ[native_libs_preload_env_var] = ":".join(
82-
os.path.join(dirpath, native_libs_dir, l)
83-
for l in native_libs_preload.split(":")
84-
)
85-
8661
# Allow users to decorate the main module. In normal Python invocations this
8762
# can be done by prefixing the arguments with `-m decoratingmodule`. It's not
8863
# that easy for par files. The startup script below sets up `sys.path` from
@@ -128,73 +103,18 @@ if os.environ.pop("PYTHONDEBUGWITHPDB", None):
128103
initial_commands=initial_commands,
129104
)
130105

131-
# Note: this full block of code will be included as the argument to Python,
132-
# and will be the first thing that shows up in the process arguments as displayed
133-
# by programs like ps and top.
134-
#
135-
# We include arg0 at the start of this comment just to make it more visible what program
136-
# is being run in the ps and top output.
137-
STARTUP = """\
138-
# {arg0!r}
139-
# Wrap everything in a private function to prevent globals being captured by
140-
# the `runpy._run_module_as_main` below.
141-
def __run():
142-
import sys
143-
144-
# We set the paths beforehand to have a minimal amount of imports before
145-
# nuking PWD from sys.path. Otherwise, there can be problems if someone runs
146-
# from a directory with a similarly named file, even if their code is properly
147-
# namespaced. e.g. if one has foo/bar/contextlib.py and while in foo/bar runs
148-
# `buck run foo/bar:bin`, runpy will fail as it tries to import
149-
# foo/bar/contextlib.py. You're just out of luck if you have sys.py or os.py
150-
151-
# Set `argv[0]` to the executing script.
152-
assert sys.argv[0] == '-c'
153-
sys.argv[0] = {arg0!r}
154-
155-
# Replace the working directory with location of the modules directory.
156-
assert sys.path[0] == ''
157-
sys.path[0] = {pythonpath!r}
158-
159-
import os
160-
import runpy
161-
162-
def setenv(var, val):
163-
if val is None:
164-
os.environ.pop(var, None)
165-
else:
166-
os.environ[var] = val
167-
168-
def restoreenv(d):
169-
for k, v in d.items():
170-
setenv(k, v)
171-
172-
restoreenv({env_vals!r})
173-
{module_call}
174-
175-
__run()
176-
""".format(
177-
arg0=sys.argv[0],
178-
pythonpath=os.path.join(dirpath, modules_dir),
179-
env_vals=env_vals_to_restore,
180-
main_module=main_module,
181-
this_file=__file__,
182-
module_call=module_call,
183-
)
184-
185-
args = [sys.executable, "<PYTHON_INTERPRETER_FLAGS>", "-c", STARTUP]
186-
106+
interpreter_opts = ["<PYTHON_INTERPRETER_FLAGS>"]
187107
# Default to 'd' warnings, but allow users to control this via PYTHONWARNINGS
188108
# The -E causes python to ignore all PYTHON* environment vars so we have to
189109
# pass this down using the command line.
190110
warnings = os.environ.get("PYTHONWARNINGS", "d").split(",")
191111
for item in reversed(warnings):
192-
args.insert(1, "-W{0}".format(item.strip()))
112+
interpreter_opts.insert(0, "-W{0}".format(item.strip()))
193113

194114
# Allow users to disable byte code generation by setting the standard environment var.
195115
# Same as above, because of -E we have to pass this down using the command line.
196116
if "PYTHONDONTWRITEBYTECODE" in os.environ:
197-
args.insert(1, "-B")
117+
interpreter_opts.insert(0, "-B")
198118

199119
# Python 3.7 allows benchmarking import time with this variable. Similar issues to
200120
# PYTHONDONTWRITEBYTECODE above. If using an earlier version of python... dont set this
@@ -205,30 +125,17 @@ if (
205125
and platform.python_implementation() == "CPython"
206126
and (sys.version_info[0], sys.version_info[1]) >= (3, 7)
207127
):
208-
args[1:1] = ["-X", "importtime"]
128+
interpreter_opts[0:0] = ["-X", "importtime"]
209129

210-
if platform.system() == "Windows":
211-
# exec on Windows is not true exec - there is only 'spawn' ('CreateProcess').
212-
# However, creating processes unnecessarily is painful, so we only do the spawn
213-
# path if we have to, which is on Windows. That said, this complicates signal
214-
# handling, so we need to set up some signal forwarding logic.
215-
216-
p = subprocess.Popen(args + sys.argv[1:])
217-
218-
def handler(signum, frame):
219-
# If we're getting this, we need to forward signum to subprocesses
220-
if signum == signal.SIGINT:
221-
p.send_signal(signal.CTRL_C_EVENT)
222-
elif signum == signal.SIGBREAK:
223-
p.send_signal(signal.CTRL_BREAK_EVENT)
224-
else:
225-
# shouldn't happen, we should be killed instead
226-
p.terminate()
227-
228-
signal.signal(signal.SIGINT, handler)
229-
signal.signal(signal.SIGBREAK, handler)
230-
231-
p.wait()
232-
sys.exit(p.returncode)
130+
interpreter_wrapper_path = os.path.join(dirpath, <INTERPRETER_WRAPPER_REL_PATH>)
131+
if sys.version_info >= (3, 0):
132+
import importlib.machinery
133+
loader = importlib.machinery.SourceFileLoader("interpreter_wrapper", interpreter_wrapper_path)
134+
interpreter_wrapper = loader.load_module()
233135
else:
234-
os.execv(sys.executable, args + sys.argv[1:])
136+
# Buck is sunsetting Python2 support. However this is still needed for some
137+
# unit tests.
138+
import imp
139+
interpreter_wrapper = imp.load_source("interpreter_wrapper", interpreter_wrapper_path)
140+
141+
interpreter_wrapper.exec_interpreter(dirpath, interpreter_opts, module_call, sys.argv[1:])

0 commit comments

Comments
 (0)