DaanDeMeyer / reproc

A cross-platform (C99/C++11) process library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Environment variables logic is error prone (for windows particularly)

DaemonSnake opened this issue · comments

Hi,
The presence of the following comment environment.hpp

Note that passing an empty container to this method will start the child
process with no environment. To have the child process inherit the environment
of the parent process, call the default constructor instead.

Highlights by its presence that it is not expected behavior.
It is furthermore dangerous to have an empty env as we will highlight it later.

If a user need add an env variable, seeing that there is a environment object and that the environment is immutable (no push),
he will try to use this constructor.

This is without consequence for users of Linux or small applications.
But for Windows this mean that your application won't have any of its system Env variables that are read by windows libraries.
This means that a user trying to just add an env variable might suddenly, and just for windows, have networking issues.
for instance: A non-recoverable error occurred during a database lookup. (os error 11003)

I just was in such a situation and it took a few hours to 3 devs to find it out.

Furthermore the library doesn't offer any way to extend the env which is a pretty basic need, a much more basic one than starting a process with no env.

Therefor creating a limited env should only be a conscious choice on the part of the user and the default behavior should be extension.

Hi,

Thanks for creating the issue! In hindsight, it's indeed a lot more intuitive to extend the parent environment with extra environment variables instead of replacing it. The latest commit on master modifies the environment option a bit (which is now named env). It now takes a behavior argument which allows the user to indicate whether he wants to extend the parent environment or start from scratch with an empty environment. By default, the parent environment is extended. Extra environment variables are added via env.extra.

Usage:

reproc::options options;
options.env.behavior = reproc::env::extend;
options.env.extra = std::unordered_map<std::string, std::string> { { "MESSAGE", "EXTEND"} };

Does this fix the issue for you? Any feedback would be welcome and appreciated.

Hi,
Thanks very much for the quick respond and change. That's some reactivity right here ^^
I like the new interface it's clean 👍 !
I close the issue