Bug #13907

Operation not permitted (Errno::EPERM) when adjusting .irbrc_history file permissions

Added by heaven (Alexander S.) almost 3 years ago. Updated 12 months ago.

Target version:
ruby -v:
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]


This is happening every time I close irb on the server:

$ bin/rails console
Loading staging environment (Rails 5.1.3)
2.3.0 :001 > IRB.conf[:SAVE_HISTORY]
 => 100
2.3.0 :002 > IRB.conf[:HISTORY_FILE]
 => nil
2.3.0 :003 > IRB.rc_file("_history")
 => "/usr/local/rvm/rubies/ruby-2.3.3/.irbrc_history"
2.3.0 :004 > exit
/usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb/ext/save-history.rb:91:in `chmod': Operation not permitted @ chmod_internal - /usr/local/rvm/rubies/ruby-2.3.3/.irbrc_history (Errno::EPERM)
    from /usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb/ext/save-history.rb:91:in `save_history'
    from /usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb/ext/save-history.rb:64:in `block in extended'
    from /usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb.rb:404:in `block in irb_at_exit'
    from /usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb.rb:404:in `each'
    from /usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb.rb:404:in `irb_at_exit'
    from /usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb.rb:398:in `start'
    from /var/www/ `start'
    from /var/www/ `start'
    from /var/www/ `perform'
    from /var/www/ `run'
    from /var/www/ `invoke_command'
    from /var/www/ `dispatch'
    from /var/www/ `perform'
    from /var/www/ `invoke'
    from /var/www/ `<top (required)>'
    from bin/rails:10:in `require'
    from bin/rails:10:in `<main>'

IRB is trying to adjust file permissions File.chmod(0600, history_file) which isn't possible because the file owner is root:rvm. I am not sure if this is really an IRB bug or not, but perhaps it should check the owner first.

$ ls -la /usr/local/rvm/rubies/ruby-2.3.3/
total 36
drwxrwsr-x 6 root rvm 4096 Sep 15 11:46 .
drwxrwsr-x 4 root rvm 4096 Aug 14 12:05 ..
drwxrwsr-x 2 root rvm 4096 Aug 14 12:05 bin
-rw-rw-r-- 1 root rvm 7954 Aug 14 12:05 config
drwxrwsr-x 3 root rvm 4096 Aug 14 12:05 include
-rwxrwxr-x 1 root rvm  406 Aug 14 12:05 .irbrc
-rw-rw-r-- 1 root rvm    0 Sep 15 12:28 .irbrc_history
drwxrwsr-x 4 root rvm 4096 Aug 14 12:05 lib
drwxrwsr-x 3 root rvm 4096 Aug 14 12:05 share

Updated by shevegen (Robert A. Heiler) almost 3 years ago

I am pretty sure that this is a small bug in IRB. It is a method in IRB itself,
so it should be the responsibility of IRB to handle cases like the above

The method looks like this:

def save_history
  if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
    if history_file = IRB.conf[:HISTORY_FILE]
      history_file = File.expand_path(history_file)
    history_file = IRB.rc_file("_history") unless history_file

    # Change the permission of a file that already exists[BUG #7694]
      if File.stat(history_file).mode & 066 != 0
        File.chmod(0600, history_file)
    rescue Errno::ENOENT

    open(history_file, 'w', 0600 ) do |f|
      hist = HISTORY.to_a
      f.puts(hist[-num..-1] || hist)

The problem should be at:

if File.stat(history_file).mode & 066 != 0
    File.chmod(0600, history_file)

I don't have 2.3.3. so I can not check if this still exists in
more recent versions of IRB. But I think that in general,
there should either be a check BEFORE File.chmod, to see whether
we can modify the file in question - this may be more elegant.

Or alternatively, Errno::EPERM should also be rescued. (In the
above check, with proper permissions, one can probably change it
within ruby + irb; whereas, without proper permissions, I do
not think that ruby can do something if you lack permissions to
the filesystem.)

But anyway, I agree with you - IRB should check for the proper

PS: The old bug referenced from the above method that I just posted,
part of the file save-history.rb, was from 4 years ago at:

I just note this down if others want to have a look too.

Updated by jeremyevans0 (Jeremy Evans) about 1 year ago

  • Assignee set to keiju (Keiju Ishitsuka)
  • Status changed from Open to Assigned

I'm not sure if this is a bug or not. However, considering that irb ignores permission issues on .irbrc (not intentionally I think), I suppose it makes sense to also ignore permission issues on the history file. I added a pull request in case the IRB maintainers want to make this change:


Updated by jeremyevans0 (Jeremy Evans) 12 months ago

  • Status changed from Assigned to Closed

Also available in: Atom PDF