Skip to content

Commit 2131c08

Browse files
committed
properly close pipe in parent
1 parent 5597aa0 commit 2131c08

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

include/boost/process/v2/stdio.hpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,18 @@ struct process_io_binding
173173
bool fd_needs_closing{false};
174174
error_code ec;
175175

176-
~process_io_binding()
176+
void close()
177177
{
178178
if (fd_needs_closing)
179+
{
179180
::close(fd);
181+
fd = target;
182+
fd_needs_closing = false;
183+
}
184+
}
185+
~process_io_binding()
186+
{
187+
close();
180188
}
181189

182190
process_io_binding() = default;
@@ -193,8 +201,7 @@ struct process_io_binding
193201

194202
process_io_binding & operator=(process_io_binding && other) noexcept
195203
{
196-
if (fd_needs_closing)
197-
::close(fd);
204+
close();
198205

199206
fd = other.fd;
200207
fd_needs_closing = other.fd_needs_closing;
@@ -358,6 +365,22 @@ struct process_stdio
358365

359366
return error_code {};
360367
};
368+
369+
// weather fork succeeded or not, we should close the pipe properly in the parent
370+
void close_pipes()
371+
{
372+
in.close();
373+
out.close();
374+
err.close();
375+
};
376+
void on_success(posix::default_launcher & /*launcher*/, const filesystem::path &, const char * const *)
377+
{
378+
return close_pipes();
379+
};
380+
void on_error(posix::default_launcher & /*launcher*/, const filesystem::path &, const char * const *)
381+
{
382+
return close_pipes();
383+
};
361384
#endif
362385

363386
};

0 commit comments

Comments
 (0)