Increase the request timeout when using jupyter-server-proxy
When using jupyter-server-proxy to run an app inside JupyterLab you may experience timeouts or 500 Internal Server Errors if a request to your app takes longer than 20 seconds to process. This is because jupyter-server-proxy uses the default timeout of 20s for HTTP client connections as provided by the Tornado package. There isn't a direct way to change this in jupyter-server-proxy but you can patch the Tornado package using the following method:
First, open a terminal in your Jupyter Lab session and run the command
find / -xdev -path "*/tornado/httpclient.py" 2>/dev/null
This will print one of more lines like this:
The actual path may differ from the example above on your system and there may be more than one path output. Make a note of each of the paths and then add the following line to the end of your environment's Dockerfile instructions, one per path you found above, and rebuild the environment:
RUN sed -i 's:^\(\s*\)request_timeout=20.0,:\1request_timeout=600.0,:' /path/to/tornado/httpclient.py && rm -rf /path/to/tornado/__pycache__
So, for example, if you find two copies of
/usr/local/lib/python3.6/site-packages/tornado/httpclient.py you would put this in your Dockerfile:
RUN sed -i 's:^\(\s*\)request_timeout=20.0,:\1request_timeout=600.0,:' /usr/local/anaconda/lib/python3.6/site-packages/tornado/httpclient.py && rm -rf /usr/local/anaconda/lib/python3.6/site-packages/tornado/__pycache__ RUN sed -i 's:^\(\s*\)request_timeout=20.0,:\1request_timeout=600.0,:' /usr/local/lib/python3.6/site-packages/tornado/httpclient.py && rm -rf /usr/local/lib/python3.6/site-packages/tornado/__pycache__
It is recommend that you make a copy of the environment you are using first and work on that copy. The change will affect all users and all code that uses the Tornado httpclient package in that environment. Essentially it is increasing the default timeout for requests to 600 seconds which may not be desirable in all situations. Once you have finished developing your app in Jupyter Lab you can switch back to the original environment without the patched copy of Tornado.
If you require a longer timeout then you can change the part of the line that reads
request_timeout=600.0 to a larger value like
request_timeout=1200.0 for example.