ruby / irb

interactive Ruby
BSD 2-Clause "Simplified" License
389 stars 119 forks source link

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

Open dmke opened 2 months ago

dmke commented 2 months ago

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:

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?

dmke commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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.