Skip to content

Commit 04242c0

Browse files
authored
Don't leak exceptions when a subprocess fails when run by Platforms/WASI (#156030)
1 parent 83531fd commit 04242c0

2 files changed

Lines changed: 61 additions & 40 deletions

File tree

Platforms/WASI/_build.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,19 @@ def call(command, *, context=None, quiet=False, **kwargs):
129129
stderr = subprocess.STDOUT
130130
_shared.log("📝", f"Logging output to {stdout.name} (--quiet)...")
131131

132-
subprocess.check_call(command, **kwargs, stdout=stdout, stderr=stderr)
132+
try:
133+
subprocess.check_call(command, **kwargs, stdout=stdout, stderr=stderr)
134+
except subprocess.CalledProcessError as error:
135+
if quiet:
136+
_shared.log("❌", f"Exit code {error.returncode}")
137+
separator()
138+
with open(stdout.name, encoding="utf-8") as file:
139+
lines = file.readlines()
140+
# Inefficient, but the log shouldn't be dramatically large.
141+
print("".join(lines[-10:]), end="")
142+
if not lines[-1].endswith("\n"):
143+
print()
144+
sys.exit(error.returncode)
133145

134146

135147
@subdir("build_python_path", clean_ok=True)
@@ -163,8 +175,7 @@ def make_build_python(context, _working_dir):
163175
cmd = [
164176
binary,
165177
"-c",
166-
"import sys; "
167-
"print(f'{sys.version_info.major}.{sys.version_info.minor}')",
178+
"import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')",
168179
]
169180
version = subprocess.check_output(cmd, encoding="utf-8").strip()
170181

Platforms/WASI/_package.py

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"pathlib",
55
"shutil",
66
"subprocess",
7+
"sys",
78
"_shared",
89
]
910

@@ -12,6 +13,7 @@
1213
import pathlib
1314
import shutil
1415
import subprocess
16+
import sys
1517

1618
import _shared
1719

@@ -376,45 +378,53 @@ def archive(context):
376378
int(source_date_epoch), datetime.UTC
377379
).strftime(mtime_format)
378380
else:
379-
mtime = subprocess.run(
381+
try:
382+
mtime = subprocess.run(
383+
[
384+
"git",
385+
"log",
386+
"-1",
387+
"--format=tformat:%cd",
388+
f"--date=format:{mtime_format}",
389+
os.fsdecode(context.checkout),
390+
],
391+
env={"TZ": "UTC0"},
392+
capture_output=True,
393+
text=True,
394+
check=True,
395+
).stdout.strip()
396+
except subprocess.CalledProcessError as error:
397+
print(error.output)
398+
sys.exit(error.returncode)
399+
400+
try:
401+
subprocess.run(
380402
[
381-
"git",
382-
"log",
383-
"-1",
384-
"--format=tformat:%cd",
385-
f"--date=format:{mtime_format}",
386-
os.fsdecode(context.checkout),
403+
"tar",
404+
"-c",
405+
"-f",
406+
os.fsdecode(file_path),
407+
"--sort=name",
408+
"--mtime",
409+
mtime,
410+
"--clamp-mtime",
411+
"--owner=0",
412+
"--group=0",
413+
"--numeric-owner",
414+
"--pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime",
415+
"--mode=go+u,go-w",
416+
# Explicitly using `-T` because if you don't compress with threads you can't
417+
# uncompress with them and the size difference is negligible when using
418+
# single-threaded compression.
419+
"--use-compress-program",
420+
"xz -T 0",
421+
to_compress.name,
387422
],
388-
env={"TZ": "UTC0"},
423+
cwd=to_compress.parent,
389424
capture_output=True,
390425
text=True,
391426
check=True,
392-
).stdout.strip()
393-
394-
subprocess.run(
395-
[
396-
"tar",
397-
"-c",
398-
"-f",
399-
os.fsdecode(file_path),
400-
"--sort=name",
401-
"--mtime",
402-
mtime,
403-
"--clamp-mtime",
404-
"--owner=0",
405-
"--group=0",
406-
"--numeric-owner",
407-
"--pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime",
408-
"--mode=go+u,go-w",
409-
# Explicitly using `-T` because if you don't compress with threads you can't
410-
# uncompress with them and the size difference is negligible when using
411-
# single-threaded compression.
412-
"--use-compress-program",
413-
"xz -T 0",
414-
to_compress.name,
415-
],
416-
cwd=to_compress.parent,
417-
capture_output=True,
418-
text=True,
419-
check=True,
420-
)
427+
)
428+
except subprocess.CalledProcessError as error:
429+
print(error.output)
430+
sys.exit(error.returncode)

0 commit comments

Comments
 (0)