Issue
I am trying to execute the following C++ script inside the Docker container, using python's docker library
#include <iostream>
int main() {
int a = 1;
int b = 0;
std::cout << a / b;
return 0;
}
Here are commands that are in fact executed:
Compilation, no problems here
container.exec_run('g++ user-scripts-dir/cpp_script.cpp -o compiled')
Execution:
container.exec_run('./compiled', demux=True)
print(result)
demux flag indicates that stdout and stderr are returned separately.
So, here is my problem. This C++ code has Run-Time error (division by zero) and I want to recieve this error message in stderr of container.exec_run
command, but I only get this: ExecResult(exit_code=136, output=(None, None))
However, If I run execution of compiled file command inside the container manually, I will get message that says: Floating point exception
. My question is: how to get correct message in stderr?
Solution
Your program isn't writing out anything; you are successfully collecting everything that the process writes to its standard output and error.
You can demonstrate this without Docker using shell redirection to write the output to files
$ g++ -o x x.cpp
$ ./x >x.out 2>x.err
[1] 81925 floating point exception ./x > x.out 2> x.err
$ ls -l
total 80
-rwxr-xr-x 1 dmaze staff 33168 Dec 26 19:46 x
-rw-r--r-- 1 dmaze staff 104 Dec 26 19:46 x.cpp
-rw-r--r-- 1 dmaze staff 0 Dec 26 19:46 x.err
-rw-r--r-- 1 dmaze staff 0 Dec 26 19:46 x.out
Notice that, even redirecting both stdout and stderr, the "floating point exception" message has appeared in the terminal window, and the files that got written to disk are both zero length.
Your other hint here is the exit_code=136
. Exit codes above 127 are "often" an abnormal exit, with the actual exit code being 128 plus a signal number. signal(7) documents the "usual" signal numbers, showing SIGFPE as signal number 8; 8 + 128 yields your exit code of 136.
If you want something to appear on the program's stderr, you yourself need to capture the signal and handle it, probably using sigaction(2). Note that some signals can't be captured at all (SIGKILL) and some are ignored by default (SIGCHLD).
Answered By - David Maze Answer Checked By - Clifford M. (WPSolving Volunteer)