Skip to content
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

Make terminal_class configurable and provide a manager to detect terminal status on linux systems #626

Closed
wants to merge 29 commits into from

Conversation

Wh1isper
Copy link
Contributor

@Wh1isper Wh1isper commented Dec 2, 2021

#625

This is a temporary solution
In Jupyterhub, each input will trigger a record, which may have performance issues

@codecov-commenter
Copy link

codecov-commenter commented Dec 2, 2021

Codecov Report

Merging #626 (82847b1) into master (e5cb6c4) will increase coverage by 0.12%.
The diff coverage is 93.27%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #626      +/-   ##
==========================================
+ Coverage   77.63%   77.75%   +0.12%     
==========================================
  Files         110      111       +1     
  Lines       10398    10509     +111     
  Branches     1408     1417       +9     
==========================================
+ Hits         8072     8171      +99     
- Misses       1929     1939      +10     
- Partials      397      399       +2     
Impacted Files Coverage Δ
jupyter_server/terminal/handlers.py 80.64% <83.33%> (+4.64%) ⬆️
jupyter_server/terminal/terminalmanager.py 89.74% <92.10%> (+2.24%) ⬆️
jupyter_server/tests/test_stateful_terminal.py 94.44% <94.44%> (ø)
jupyter_server/serverapp.py 65.38% <100.00%> (+0.03%) ⬆️
jupyter_server/terminal/__init__.py 79.16% <100.00%> (ø)
jupyter_server/services/kernels/handlers.py 60.40% <0.00%> (-2.00%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e5cb6c4...82847b1. Read the comment docs.

Copy link
Contributor

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you!

jupyter_server/terminal/handlers.py Outdated Show resolved Hide resolved
jupyter_server/terminal/handlers.py Outdated Show resolved Hide resolved
@blink1073 blink1073 added this to the 1.12 milestone Dec 2, 2021
@Wh1isper
Copy link
Contributor Author

Wh1isper commented Dec 2, 2021

well it cannot handle the following situations

$ cat untitled.py 
print(1)
while True:
    ...

python untitled.py
so I think I should add more detections

# It works well for jupyterhub-singleuser and should also work for other debian-based mirrors
# fixme: May fail if terminal is not properly separated with ':' or change user after connect
# (Any change to the user, hostname or environment may render it invalid)
self._first_stdout = message_seg[1].split(':')[0].lstrip()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This meets my needs, but may not be a good general approach
Maybe we need to raise an issue with terminado?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand it, it is terminal dependent, terminado is just reading from the pty: https://github.com/jupyter/terminado/blob/ab4ced0b77c8f198cdc27fe1d52c523635083461/terminado/websocket.py#L77

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it may be a little difficult to implement a reliable solution based on the current architecture. But we can delve into how to determine that an terminal is running

@kevin-bates
Copy link
Member

Since this is introducing state management, it seems like the culling logic should be updated to no longer cull currently 'busy' terminals. Are we confident that the 'idle' state detection is reliable? If not, then culling could be adversely affected since (if I'm following things) a terminal could get "stuck" in 'busy' and we'd probably be better off leaving culling as-is.

@Wh1isper
Copy link
Contributor Author

Wh1isper commented Dec 3, 2021

Since this is introducing state management, it seems like the culling logic should be updated to no longer cull currently 'busy' terminals. Are we confident that the 'idle' state detection is reliable? If not, then culling could be adversely affected since (if I'm following things) a terminal could get "stuck" in 'busy' and we'd probably be better off leaving culling as-is.

Yes, we'd probably be better off leaving culling as-is.
I am thinking about how to implement a reliable state determination. The current implementation requires the cooperation of the operating environment, in the environment provided by the unified is working properly.

My own idea is that this functionality is provided on-demand, and if someone needs to use it, the scenario is roughly the same as mine: detect if the user's kernel and terminal have been idle for a long time with a uniformly provided mirror and scheduling system (as jupyterhub). The point of adding terminal status detection is that last_activity_time is only updated on input and output and does not reflect the running state.

@Wh1isper
Copy link
Contributor Author

Wh1isper commented Dec 3, 2021

@kevin-bates @blink1073
I have taken the TerminalManager with state storage and made it configurable in a separate class StatefulTerminalManager

If people use singleuser-based images, it performs well
In fact it shouldn't get stuck in a busy state as long as people don't switch users, bash styles or activate the virtual environment
c.ServerApp.stateful_terminals_enabled = True to enable it

@blink1073
Copy link
Contributor

I tried locally to get it working on macOS, unsuccessfully. I think that perhaps this logic is too brittle to be in core, and this should be a third party Terminal manager, and we should allow the terminal manager to be configured similar to kernel_manager.

@Wh1isper
Copy link
Contributor Author

Wh1isper commented Dec 5, 2021

I tried locally to get it working on macOS, unsuccessfully. I think that perhaps this logic is too brittle to be in core, and this should be a third party Terminal manager, and we should allow the terminal manager to be configured similar to kernel_manager.

Good idea , I've made the terminal_manager_class configurable
c.ServerApp.terminal_manager_class = 'jupyter_server.terminal.StatefulTerminalManager' to enable StatefulTerminalManager

@Wh1isper Wh1isper changed the title patch execution_state filed into terminal Make terminal_class configurable and Provide a manager to detect terminal status on linux systems Dec 5, 2021
@Wh1isper Wh1isper changed the title Make terminal_class configurable and Provide a manager to detect terminal status on linux systems Make terminal_class configurable and provide a manager to detect terminal status on linux systems Dec 5, 2021
Copy link
Contributor

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable to me as-is. Thanks @Wh1isper!

@blink1073
Copy link
Contributor

Hi @Wh1isper, we discussed this PR in the weekly meeting. We think the right approach is to create a new repo and make terminals a server extension. This new class would then be a configurable part of that extension. I'll take the task of creating the repo and adding the initial content by the end of this month.

@Wh1isper
Copy link
Contributor Author

Hi @Wh1isper, we discussed this PR in the weekly meeting. We think the right approach is to create a new repo and make terminals a server extension. This new class would then be a configurable part of that extension. I'll take the task of creating the repo and adding the initial content by the end of this month.

Great, thank you for your attention to this issue, I fully agree with this approach and will continue to follow the progress

@Wh1isper
Copy link
Contributor Author

Turn to #640
Thanks all!

@Wh1isper Wh1isper closed this Dec 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants