On my dev node, because rajdhani sets sys.stdout to sys.stderr as a fix to this issue (https://github.com/pipalacademy/rajdhani/issues/14), that also sets sys.stdout of the runner and so the output is printed into stderr logs.
Because memory is shared between the CGI runner (our global hamr.cgi) and user's modules (wsgi.py and others imported in), there may be side effects of just importing the user's wsgi module, such as setting sys.stdout or sys.stderr to other streams. This shouldn't affect the runner and it should still print the output to stdout only, the logs to stderr only.
On my dev node, because rajdhani sets
sys.stdout
tosys.stderr
as a fix to this issue (https://github.com/pipalacademy/rajdhani/issues/14), that also setssys.stdout
of the runner and so the output is printed into stderr logs.Because memory is shared between the CGI runner (our global
hamr.cgi
) and user's modules (wsgi.py
and others imported in), there may be side effects of just importing the user'swsgi
module, such as settingsys.stdout
orsys.stderr
to other streams. This shouldn't affect the runner and it should still print the output tostdout
only, the logs tostderr
only.Proposed solution:
Python has some special values in sys module that still retain the original standard stream even when
sys.stdout
orsys.stderr
are changed: https://docs.python.org/3/library/sys.html#sys.__stdout__We can import wsgi.app inside a try finally block like this, so that we retain the original values: