Operation not permitted (Errno::EPERM) when adjusting .irbrc_history file permissions
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/crm.wegohealth.com/shared/bundle/ruby/2.3.0/gems/railties-5.1.3/lib/rails/commands/console/console_command.rb:62:in `start' from /var/www/crm.wegohealth.com/shared/bundle/ruby/2.3.0/gems/railties-5.1.3/lib/rails/commands/console/console_command.rb:17:in `start' from /var/www/crm.wegohealth.com/shared/bundle/ruby/2.3.0/gems/railties-5.1.3/lib/rails/commands/console/console_command.rb:85:in `perform' from /var/www/crm.wegohealth.com/shared/bundle/ruby/2.3.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run' from /var/www/crm.wegohealth.com/shared/bundle/ruby/2.3.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command' from /var/www/crm.wegohealth.com/shared/bundle/ruby/2.3.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch' from /var/www/crm.wegohealth.com/shared/bundle/ruby/2.3.0/gems/railties-5.1.3/lib/rails/command/base.rb:63:in `perform' from /var/www/crm.wegohealth.com/shared/bundle/ruby/2.3.0/gems/railties-5.1.3/lib/rails/command.rb:44:in `invoke' from /var/www/crm.wegohealth.com/shared/bundle/ruby/2.3.0/gems/railties-5.1.3/lib/rails/commands.rb:16:in `<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) over 1 year 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) end history_file = IRB.rc_file("_history") unless history_file # Change the permission of a file that already exists[BUG #7694] begin if File.stat(history_file).mode & 066 != 0 File.chmod(0600, history_file) end rescue Errno::ENOENT rescue raise end open(history_file, 'w', 0600 ) do |f| hist = HISTORY.to_a f.puts(hist[-num..-1] || hist) end end end
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
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.