Skip to content

Commit a0aad5c

Browse files
authored
Merge pull request #81517 from rintaro/macros-wait-rdar150474701
[Macros] Mitigate plugin process 'wait' failure
2 parents 7f97e8d + 4a62ef8 commit a0aad5c

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

lib/AST/PluginRegistry.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,17 @@ LoadedExecutablePlugin::PluginProcess::~PluginProcess() {
213213
#else
214214
close(input);
215215
close(output);
216+
kill(process.Pid, SIGTERM);
216217
#endif
217218

218219
// Set `SecondsToWait` non-zero so it waits for the timeout and kill it after
219220
// that. Usually when the pipe is closed above, the plugin detects the EOF in
220221
// the stdin and exits immediately, so this usually doesn't wait for the
221222
// timeout. Note that we can't use '0' because it performs a non-blocking
222-
// wait, which make the plugin a zombie if it hasn't exited.
223-
llvm::sys::Wait(process, /*SecondsToWait=*/1);
223+
// wait, which make the plugin a zombie if it hasn't exited. We don't use
224+
// a small number like '1' because the timeout alarm(3) can fire before the
225+
// wait4(2).
226+
llvm::sys::Wait(process, /*SecondsToWait=*/10);
224227
}
225228

226229
ssize_t LoadedExecutablePlugin::PluginProcess::read(void *buf,

lib/Basic/Program.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,15 @@ swift::ExecuteWithPipe(llvm::StringRef program,
123123
posix_spawn_file_actions_t FileActions;
124124
posix_spawn_file_actions_init(&FileActions);
125125

126+
// Redirect file descriptors...
126127
posix_spawn_file_actions_adddup2(&FileActions, p1.read, STDIN_FILENO);
127-
posix_spawn_file_actions_addclose(&FileActions, p1.write);
128-
129128
posix_spawn_file_actions_adddup2(&FileActions, p2.write, STDOUT_FILENO);
129+
130+
// Close all file descriptors, not needed as we duped them to the stdio.
131+
posix_spawn_file_actions_addclose(&FileActions, p1.read);
132+
posix_spawn_file_actions_addclose(&FileActions, p1.write);
130133
posix_spawn_file_actions_addclose(&FileActions, p2.read);
134+
posix_spawn_file_actions_addclose(&FileActions, p2.write);
131135

132136
// Spawn the subtask.
133137
int error = posix_spawn(&pid, progCStr, &FileActions, nullptr,
@@ -156,13 +160,16 @@ swift::ExecuteWithPipe(llvm::StringRef program,
156160

157161
// Child process.
158162
case 0:
159-
close(p1.write);
160-
close(p2.read);
161-
162163
// Redirect file descriptors...
163164
dup2(p1.read, STDIN_FILENO);
164165
dup2(p2.write, STDOUT_FILENO);
165166

167+
// Close all file descriptors, not needed as we duped them to the stdio.
168+
close(p1.read);
169+
close(p1.write);
170+
close(p2.read);
171+
close(p2.write);
172+
166173
// Execute the program.
167174
if (envp) {
168175
execve(progCStr, const_cast<char **>(argv), const_cast<char **>(envp));
@@ -180,11 +187,12 @@ swift::ExecuteWithPipe(llvm::StringRef program,
180187

181188
// Parent process.
182189
default:
190+
close(p1.read);
191+
close(p2.write);
183192
break;
184193
}
185194
#endif
186-
close(p1.read);
187-
close(p2.write);
195+
188196
llvm::sys::ProcessInfo proc;
189197
proc.Pid = pid;
190198
proc.Process = pid;

0 commit comments

Comments
 (0)