Skip to content

Commit 231db65

Browse files
authored
Merge pull request #2690 from digit-google/fix-status-code-when-ninja-is-interrupted
Fix Ninja exit code when interrupted (issue #2681)
2 parents 931d990 + cc0b6ff commit 231db65

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

misc/output_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
import os
99
import platform
10+
import signal
1011
import subprocess
1112
import sys
1213
import tempfile
14+
import time
1315
import unittest
1416
from textwrap import dedent
1517
import typing as T
@@ -631,5 +633,23 @@ def test_issue_2621(self):
631633
""",
632634
)
633635

636+
def test_issue_2681(self):
637+
"""Ninja should return a status code of 130 when interrupted."""
638+
plan = r"""rule sleep
639+
command = sleep 10
640+
641+
build foo: sleep
642+
"""
643+
with BuildDir(plan) as b:
644+
for signum in (signal.SIGINT, signal.SIGHUP, signal.SIGTERM):
645+
proc = subprocess.Popen([NINJA_PATH, "foo"], cwd=b.path, env=default_env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
646+
# Sleep a bit to let Ninja start the build, otherwise the signal could be received
647+
# before it, and returncode will be -2.
648+
time.sleep(0.2)
649+
os.kill(proc.pid, signum)
650+
proc.wait()
651+
self.assertEqual(proc.returncode, 130, msg=f"For signal {signum}")
652+
653+
634654
if __name__ == '__main__':
635655
unittest.main()

src/build.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@ struct CommandRunner {
154154

155155
/// The result of waiting for a command.
156156
struct Result {
157-
Result() : edge(NULL) {}
158-
Edge* edge;
159-
ExitStatus status;
157+
Edge* edge = nullptr;
158+
ExitStatus status = ExitFailure;
160159
std::string output;
161160
bool success() const { return status == ExitSuccess; }
162161
};

src/real_command_runner.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ bool RealCommandRunner::WaitForCommand(Result* result) {
9898
Subprocess* subproc;
9999
while ((subproc = subprocs_.NextFinished()) == NULL) {
100100
bool interrupted = subprocs_.DoWork();
101-
if (interrupted)
101+
if (interrupted) {
102+
result->status = ExitInterrupted;
102103
return false;
104+
}
103105
}
104106

105107
result->status = subproc->Finish();

0 commit comments

Comments
 (0)