Skip to content

Conversation

theStack
Copy link
Contributor

The newly introduced cpp-subprocess library provides a good number of options for the Popen class:

void set_option(executable&& exe);
void set_option(cwd&& cwdir);
void set_option(bufsize&& bsiz);
void set_option(environment&& env);
void set_option(defer_spawn&& defer);
void set_option(shell&& sh);
void set_option(input&& inp);
void set_option(output&& out);
void set_option(error&& err);
void set_option(close_fds&& cfds);
void set_option(preexec_func&& prefunc);
void set_option(session_leader&& sleader);

Some of them are either not fully implemented (shell, missing an implementation on Windows), implemented in an ugly way (e.g. using "Impoverished, meager, needy, truly needy version of type erasure" for preexec_func according to the author's own words) or simply unlikely to be ever needed for our external signer use-case (defer_spawn). Instead of maintaining incomplete and/or unneeded code, I'd suggest to get rid of it and only keep support for options if there is a strong reason for it.

@DrahtBot
Copy link
Contributor

DrahtBot commented Apr 13, 2024

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK hebasto, achow101
Concept ACK TheCharlatan, laanwj

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #29910 (refactor: Rename subprocess.hpp to follow our header name conventions by hebasto)
  • #29868 (Reintroduce external signer support for Windows by hebasto)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@TheCharlatan
Copy link
Contributor

Nice, Concept ACK.

@hebasto
Copy link
Member

hebasto commented Apr 13, 2024

Concept ACK.

@hebasto
Copy link
Member

hebasto commented Apr 13, 2024

remove unused cpp-subprocess options

Are all remaining options used?

@laanwj
Copy link
Member

laanwj commented Apr 14, 2024

Concept ACK, but could it be that we could need some of these?
Eg close_fds sounds like what's described in #22417

@theStack
Copy link
Contributor Author

remove unused cpp-subprocess options

Are all remaining options used?

From the 12 available cpp-subprocess options we currently only use 4 (namely executable, input, output and error):

auto c = sp::Popen(str_command, sp::input{sp::PIPE}, sp::output{sp::PIPE}, sp::error{sp::PIPE});

I.e. the other 8 are unused. This PR currently removes 3 of the 8 unused ones -- the remaining 5 (cwd, bufsize, environment, close_fds, session_leader) seemed to be common enough to be useful at some point in the future, but my choice is more based on gut feeling than anything else, as I don't know what future use-cases could be. Concrete suggestions are very welcome. For example, setting environment variables seems like it could be useful, but the code is quite complex for Windows.

Concept ACK, but could it be that we could need some of these?
Eg close_fds sounds like what's described in #22417

Good question. Note that close_fds is currently not removed.

@laanwj
Copy link
Member

laanwj commented Apr 15, 2024

For example, setting environment variables seems like it could be useful, but the code is quite complex for Windows.

Agree that that seems useful. I vaguely remember we even had a use-case for that once, but don't remember it clearly, we did give it up because it was too hard to do in windows.

Good question. Note that close_fds is currently not removed.

Oh, whoops, sorry, didn't notice that.

FWIW I think it's a valid choice to remove what we're not using and re-introduce it when we do, the code is out there there's little point in keeping unused code in the repository.

@hebasto
Copy link
Member

hebasto commented Apr 16, 2024

FWIW I think it's a valid choice to remove what we're not using and re-introduce it when we do, the code is out there there's little point in keeping unused code in the repository.

I agree.

@theStack theStack force-pushed the 202404-remove-unused-cpp-subprocess-options branch from a5368c1 to 0f84733 Compare April 16, 2024 12:28
@theStack
Copy link
Contributor Author

Thanks a lot for you feedback @laanwj, @hebasto, I've now removed support for all options that we don't use. Especially for the environment option, lots of Windows-specific code can be removed.

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach ACK 0f84733.

image 🚀

I have reviewed all commits except the last one (it's still in progress).

The CI run of the combined branch [ this PR + #29868 ] is available here: https://github.com/hebasto/bitcoin/actions/runs/8740663547/job/23984855961

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 0f84733.

I'm happy to re-ACK if the #29865 (comment) will be addressed.

@luke-jr
Copy link
Member

luke-jr commented Apr 21, 2024

This seems likely to have a high maintenance cost. What's the benefit?

@theStack theStack force-pushed the 202404-remove-unused-cpp-subprocess-options branch from 0f84733 to 899be1a Compare April 23, 2024 13:02
Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK 899be1a.

@theStack
Copy link
Contributor Author

theStack commented Apr 23, 2024

Force-pushed with the last commit changed to tackle #29865 (comment).

Diff to the previous version
diff --git a/src/util/subprocess.hpp b/src/util/subprocess.hpp
index c7f9a685d1..a85c0b0bff 100644            
--- a/src/util/subprocess.hpp                                                                            
+++ b/src/util/subprocess.hpp         
@@ -1210,8 +1210,6 @@ inline void Popen::kill(int sig_num)
 inline void Popen::execute_process() noexcept(false)     
 {   
 #ifdef __USING_WINDOWS__                                                                                
-  void* environment_string_table_ptr = nullptr;
-                                              
   if (exe_name_.length()) {                                                                             
     this->vargs_.insert(this->vargs_.begin(), this->exe_name_);
     this->populate_c_argv();
@@ -1258,7 +1256,7 @@ inline void Popen::execute_process() noexcept(false)
                             NULL,         // primary thread security attributes
                             TRUE,         // handles are inherited
                             creation_flags,    // creation flags
-                            environment_string_table_ptr,  // use provided environment
+                            NULL,         // use environment of the calling process
                             NULL,         // use parent's current directory
                             &siStartInfo, // STARTUPINFOW pointer
                             &piProcInfo); // receives PROCESS_INFORMATION

@luke-jr:

This seems likely to have a high maintenance cost. What's the benefit?

Do you ask what's the benefit of using cpp-subprocess in general (instead of boost::process) or the benefit of removing code that we don't use? If it's the latter and you're worried that this makes backporting upstream fixes harder, I'd argue that there is hardly any activity upstream, the most recent fixes have been done by @hebasto.

@theStack theStack force-pushed the 202404-remove-unused-cpp-subprocess-options branch from 899be1a to 13adbf7 Compare April 23, 2024 13:10
Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK 13adbf7.

@hebasto
Copy link
Member

hebasto commented Apr 23, 2024

This seems likely to have a high maintenance cost.

As discussed in #28981 and during the IRC meeting, we are going to treat the new code as our own:

... this code is going to be maintained as our own. Next PRs should:

  • Remove linter exclusions and fix all issues.
  • Delete all unused code.
  • Rewrite in C++20.
  • hpp -> cpp ?

14:16 <fanquake> Obviously having a static header in our repo is an improvement, similar to nanobench/tinyformat

@achow101
Copy link
Member

ACK 13adbf7

@achow101 achow101 merged commit 2cecbbb into bitcoin:master Apr 23, 2024
@theStack theStack deleted the 202404-remove-unused-cpp-subprocess-options branch April 23, 2024 17:05
@hebasto
Copy link
Member

hebasto commented Apr 23, 2024

#29910 is the next one :)

@bitcoin bitcoin locked and limited conversation to collaborators Apr 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants