-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jira ticket ECS-5104: Automatically close hutch-python sessions that have been idle for 48 hours or more. #383
Conversation
A couple of quick notes before diving into code (really just explaining the PR checklist):
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall I think this works as intended, at least in my interactive testing 👍
Some tests would be good here, possibly monkeypatching the max_time to speed the timeout process up.
|
||
def __init__(self, ipython): | ||
self.curr_time = 0 | ||
self.max_idle_time = 172800 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be good to expose a way for the user to set this. That'd also help us test this feature.
self._get_time_passed() | ||
|
||
# Close the user session | ||
print("This hutch-python session has timed out. Please start a new session.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to also use the logger here. Currently this doesn't show up in our log files at all, so the session will terminate and uninformed people won't know what happened.
print("This hutch-python session has timed out. Please start a new session.") | ||
|
||
# Close this ipython session | ||
get_ipython().ask_exit() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking inspiration from the linked github thread, we apparently need to follow this up with another exit call to kill the prompt that's waiting for input, otherwise as you say in the desc this waits for user input before exiting.
ip = get_ipython()
ip.ask_exit()
ip.pt_app.app.exit()
|
||
Parameters | ||
---------- | ||
ip: ``ipython`` ``Shell`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is copied from ipython_log.py
, but the parameter name is wrong and the type hint is... odd? It should probably be the
ipython : ``IPython.terminal.interactiveshell.TerminalInteractiveShell``
from above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The weird thing about IPython is that there are dozens of shell types
self._timer(self.max_idle_time - self.idle_time) | ||
self._get_time_passed() | ||
|
||
# Close the user session |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there's some way we can warn the user at regular intervals, e.g. "this session will time out in 2 hours if there is no further input"
Summary
This PR adds a timer that automatically closes a hutch-python session that has been idle for 48 hours or more. The timer is implemented as a class called IPython Session Timer that runs in a daemon thread. Using the 'post_run_cell' hook a timestamp is created after every user interaction in the interpreter. This timestamp is compared with the maximum amount of time a session can be idle. If 48 hours have passed, the thread will close the hutch-python session.
Current Issues
I wasn't able to find a way for the ipython interpreter to exit cleanly. I was hoping to get the main thread (the ipython interpreter) to go through standard shutdown procedures and exit the program. The best solution I found is to use
get_ipython().ask_exit()
. Other methods I tried caused problems (more details in section below).After
get_ipython().ask_exit()
runs, the interpreter will display a second prompt. When the user types something at the prompt, ipython will run the code and then exit. This behavior doesn't seem ideal. This can be tested out by running hutch-python in my rhel7 folder at /cds/home/j/janeliu/git/hutch-python.Other methods to close ipython
I tried a couple of other methods to close the interpreter:
os.kill(os.getpid(), signal.SIGINT)
- this raises a KeyboardInterrupt signal and is supposed to allow the main thread to follow standard shutdown procedures, but it generates an error message "KeyboardInterrupt escaped interact()" in hutch-python and the interpreter won't exit.os._exit(1)
- this command doesn't go through standard shutdown procedures. It does force ipython to exit, but my terminal freezes up afterwards. I think it's incorrectly terminating other processes.Where Has This Been Documented?
This feature was discussed by maintainers of ipython, but it doesn't seem like it was ever implemented: ipython/ipython#9944
Pre-merge checklist
docs/pre-release-notes.sh
and created a pre-release documentation page