Skip to content

Instantly share code, notes, and snippets.

@argent-smith
Created April 14, 2011 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save argent-smith/919946 to your computer and use it in GitHub Desktop.
Save argent-smith/919946 to your computer and use it in GitHub Desktop.
What happened to a file descriptor and how to cure it

Yesterday I've encountered a sudden bug: my four years old script rafused to work. After a slight rewrite I got some kind of descriptive symptom in the syslog:

Fatal: Bad file descriptor 
/usr/local/lib/ruby/gems/1.8/gems/deprecated-2.0.1/lib/deprecated.rb:199:in `write'
/usr/local/lib/ruby/gems/1.8/gems/deprecated-2.0.1/lib/deprecated.rb:199     
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'  
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require' 
/usr/local/lib/ruby/site_ruby/1.8/dbi.rb:48 
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require' 
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require' /usr/work/flow-tools/flow-pg_import:13

deprecated.rb obviously doesn't have any mention of 'write' so I've suspected an issue with some sudden output; it appeared to be true: deprecated.rb had a repeated declaration of a constant on line 199 which caused an error message on stderr which was unavailable (the script was exec'ed as a helper by another program). So I did just this:

$stdout.reopen("/dev/null", "w")
$stderr.reopen("/dev/null", "w")

And it cured the script. Rationale: you should always redirect the standard I/O streams when you really mean it to work in background.

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