@@ -177,13 +177,29 @@ struct Command::ForkServerProps {
177177// out-of-line here, now that ForkServerProps is complete (that's by-the-book
178178// PIMPL).
179179Command::~Command () {
180- if (is_executing ()) {
181- FUZZTEST_LOG (WARNING)
182- << " Destructing Command object for " << path () << " with "
183- << (fork_server_ ? absl::StrCat (" fork server PID " , fork_server_->pid_ )
184- : absl::StrCat (" PID " , pid_))
185- << " still running. Requesting it to stop without waiting for it..." ;
186- RequestStop ();
180+ if (!is_executing ()) return ;
181+ FUZZTEST_LOG (WARNING)
182+ << " Destructing Command object for " << path () << " with "
183+ << (fork_server_ ? absl::StrCat (" fork server PID " , fork_server_->pid_ )
184+ : absl::StrCat (" PID " , pid_))
185+ << " still running. Requesting it to stop without waiting for it..." ;
186+ RequestStop ();
187+ // If detaching the command while running, the output files may still be
188+ // written concurrently, which may interfere with new command outputs if
189+ // they share the same paths. Thus we want to clean up the output files, so
190+ // new commands will create new output files.
191+ if (!options_.stdout_file .empty ()) {
192+ if (std::error_code ec; std::filesystem::remove (options_.stdout_file , ec)) {
193+ FUZZTEST_LOG (ERROR) << " Failed to remove the stdout file "
194+ << options_.stdout_file << " : " << ec;
195+ }
196+ }
197+ if (!options_.stderr_file .empty () &&
198+ options_.stderr_file != options_.stdout_file ) {
199+ if (std::error_code ec; std::filesystem::remove (options_.stderr_file , ec)) {
200+ FUZZTEST_LOG (ERROR) << " Failed to remove the stderr file "
201+ << options_.stderr_file << " : " << ec;
202+ }
187203 }
188204}
189205
0 commit comments