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

[feature request] Automatically set process title to currently running command #991

Open
dmke opened this issue Aug 21, 2024 · 3 comments
Open

Comments

@dmke
Copy link

dmke commented Aug 21, 2024

Description

Colleagues of mine regularly SSH into our servers, start an IRB session to look something up, and forget to logout. When they store database query results in local variables, they obviously occupy memory and can't be garbage collected.

It has happened on more than one occasion that I need to manually free resources because half a dozen IRB sessions have multiple GB of memory allocated. I would like to avoid currently active sessions though (i.e. where a user is actively collecting information).

As far as I can tell, I have no information what an IRB process is currently doing. I can't rely on CPU usage, as swapping may induce some load (on Ubuntu 24.04, I have noticed an otherwise idle IRB process to use up to 1% of CPU in top).

It would be useful if IRB would update the process title (Process.setproctitle) to something meaningful, for example:

  • "irb - running ...", when a command is currently running (maybe show the first few characters of the command?)
  • "irb - idle since 13:55", when no command is running

Maybe this is achievable through a piece of code in .irbrc, but I haven't figured out how to do that.

Result of irb_info

irb(main):001> irb_info
Ruby version: 3.2.3
IRB version: irb 1.14.0 (2024-07-06)
InputMethod: RelineInputMethod with Reline 0.5.9
Completion: Autocomplete, RegexpCompletor
.irbrc paths: /home/[redacted]/.irbrc
RUBY_PLATFORM: x86_64-linux-gnu
LANG env: en_US.UTF-8
East Asian Ambiguous Width: 1

This is Ruby/IRB from Ubuntu 24.04 packages.

Terminal Emulator

Tilix on the workstation. This should not matter

Setting Files

Are you using ~/.irbrc and ~/.inputrc?

  • not really, ~/.irbrc includes only commented # IRB.conf[:USE_MULTILINE] = false
@dmke
Copy link
Author

dmke commented Aug 21, 2024

In lib/init.rb, I have stumbled upon IRB.set_measure_callback. The following configuration seems to do the trick:

# ~/.irbrc
IRB.conf[:MEASURE] = true

IRB.set_measure_callback do |context, code, line_no, &block|
  trunc = code.strip.then { _1[...16] + (_1.size > 16 ? "..." : "") }

  now = Time.now.strftime "%H:%S"
  Process.setproctitle "irb:#{line_no} - running `#{trunc}` since #{now}"

  block.()

  now = Time.now.strftime "%H:%S"
  Process.setproctitle "irb:#{line_no} - idle since #{now}"
end

I'm not sure what side effects to expect with IRB.conf[:MEASURE], so I'm leaving this issue open for further discussion.

@tompng
Copy link
Member

tompng commented Aug 21, 2024

I think there is a negative impact of overriding proc title.
If we do it, meaningfull proc name like ruby script_name.rb my_awsome_rails_app_console will be overrided.

I think this should be done in .irbrc or in application code, not by default.
However, there are no pre/post evaluation hooks. Perhaps we can add more hooks.

@dmke
Copy link
Author

dmke commented Aug 22, 2024

I agree on your point regarding other uses of scripts leveraging the process title.

Come to think of it, there's also a potential security problem, when the code block argument is something like pw = "supersecret\n", even if evaluating that may only take a few CPU cycles before the process title is changed back to "idle".

However, having a callback to run things around a command evaluation would be very valuable for this use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants