0

I have a C++ application that periodically calls the following function in order to run a shell command and return the output:

string run_shell_command(const string& command) {
    array<char, 128> buffer;
    string response;
    FILE* pipe = popen(command.c_str(), "r");
    if (!pipe) {
        cerr << "popen failed: " << command << endl;
        return "";
    }
    while (fgets(buffer.data(), 128, pipe) != NULL) {
        response += buffer.data();
    }
    pclose(pipe);
    return response;
}

These calls to run_shell_command usually succeed when the application starts, but after a while, the popen calls start to fail. Note that once this happens, I can still run the shell commands successfully if I do so manually in a terminal. When I tried to look this problem up, some people say that being out of memory or having too many open files can cause the problem, but I don't believe that is currently the case. For example, these outputs were generated just now, while the popen calls have been failing:

~$ free -h
              total        used        free      shared  buff/cache   available
Mem:            31G         25G        220M        2.6G        5.0G        2.3G
Swap:            0B          0B          0B
~$ ulimit -Sn
15000000
~$ ulimit -Hn
15000000
~$ sudo lsof | wc -l
190121

Any ideas? Thanks!

Edit: The errno is 12 and strerror(errno) is "Cannot allocate memory"

Edit: When the application is not running, here is how the memory looks:

$ free -h
              total        used        free      shared  buff/cache   available
Mem:            31G        209M         26G        2.6G        4.2G         27G
Swap:            0B          0B          0B
galpo
  • 183
  • 1
  • 7
  • 3
    And what value is in errno? – 273K Mar 16 '22 at 04:33
  • 1
    Unrelated future bug: [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – user4581301 Mar 16 '22 at 05:11
  • It doesn't look like your posted code has this issue, but the last time I saw this behavior it was because I was erroneously calling `fclose()` instead of `pclose()`. – Jeremy Friesner Mar 16 '22 at 05:15
  • @273K Good question. I will try to print it and find out – galpo Mar 16 '22 at 09:12
  • @user4581301 Thanks for the heads up, I edited the code, hopefully fixes it – galpo Mar 16 '22 at 09:13
  • 1
    Added error checking *and printing the error* to `popen`, `fgets` and `pclose`. You can use `perror` to print the error easily, check it out. – hyde Mar 16 '22 at 09:18
  • @273K Looks like the errno is 12 and strerror(errno) is "Cannot allocate memory" – galpo Mar 16 '22 at 16:12
  • Googling "popen error 12" gives a lot of useful info, especially the first result link, although it is about python, it does not matter. – 273K Mar 16 '22 at 16:17
  • Your program seems to have memory leaks. If it is not running, how much available free memory do you have? – 273K Mar 16 '22 at 16:19
  • @273K Yeah there seem to be other discussion on this, though no straightforward solutions from what I have seen so far unfortunately. When the application is not running, this is the memory profile: $ free -h total used free shared buff/cache available Mem: 31G 209M 26G 2.6G 4.2G 27G Swap: 0B 0B 0B – galpo Mar 18 '22 at 14:19
  • @273K Actually I saw another thread mention "echo 1 > /proc/sys/vm/overcommit_memory", might try that – galpo Mar 18 '22 at 14:44

0 Answers0