pipalacademy / hamr

0 stars 0 forks source link

HTML output being redirected to stderr #20

Closed nikochiko closed 2 years ago

nikochiko commented 2 years ago

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.

Proposed solution:

Python has some special values in sys module that still retain the original standard stream even when sys.stdout or sys.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:

try:
    from wsgi import app
finally:
    sys.stdout = sys.__stdout__
    sys.stderr = sys.__stderr__