# Bad file descriptor error

**URL:** <https://www.ruby-forum.com/t/bad-file-descriptor-error/165306>\
**Category:** Ruby\
**Created:** [April 11, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/bad-file-descriptor-error/165306 "2009-04-11T21:49:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![diskava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@diskava](https://www.ruby-forum.com/u/diskava)\
**Post date:** [April 11, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/bad-file-descriptor-error/165306/1 "2009-04-11T21:49:16Z")

</div>

Hello everyone,  
I’m new to ruby.I’m trying to write an application that would  
synchronise with google calendar.Below is a script I’m trying to use for  
authentication with google.When I try to run it I’m getting a bad file  
descriptor error.I have also provided the error message just after the  
script.Any help will be appriciated.Thank you in advance.

def authentication(email,passwd,service)  
require ‘net/https’  
http = Net::HTTP.new(‘[www.google.com](http://www.google.com)’, 443)  
http.use\_ssl = true  
path = ‘/accounts/ClientLogin’

# Now we are passing in our actual authentication data.

```
data = 'accountType=HOSTED_OR_GOOGLE&Email=email &Passwd=passwd

```

&service=service’

# Set up a hash for the headers

headers = { ‘Content-Type’ =\> ‘application/x-www-form-urlencoded’}

# Post the request and print out the response to retrieve our

authentication token  
resp, data = http.post(path, data, headers)

# Strip out our actual token (Auth) and store it

cl\_string = data[/Auth=(.\*)/, 1]

# Build our headers hash and add the authorization token

headers[“Authorization”] = “GoogleLogin auth=#{cl\_string}”  
end

#After calling this function like this:  
authentication(“removed\_email\_address@domain.invalid”,“passwd”,“cl”)

#I get the follow error message:

Errno::EBADF: Bad file descriptor  
from C:/Ruby/lib/ruby/1.8/net/http.rb:564:in `write' from C:/Ruby/lib/ruby/1.8/net/http.rb:564:in `warn’  
from C:/Ruby/lib/ruby/1.8/net/http.rb:564:in `connect' from C:/Ruby/lib/ruby/1.8/net/http.rb:553:in `do\_start’  
from C:/Ruby/lib/ruby/1.8/net/http.rb:542:in `start' from C:/Ruby/lib/ruby/1.8/net/http.rb:1035:in `request’  
from C:/Ruby/lib/ruby/1.8/net/http.rb:845:in `post' from (irb):15:in `authentication’  
from (irb):23  
from :0  
irb(main):024:0\>

---

<div class="post-metadata">

**Author:** ![diskava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@diskava](https://www.ruby-forum.com/u/diskava)\
**Post date:** [April 12, 2009, 4:58am UTC](https://www.ruby-forum.com/t/bad-file-descriptor-error/165306/2 "2009-04-12T04:58:08Z")

</div>

> Errno::EBADF: Bad file descriptor  
> from C:/Ruby/lib/ruby/1.8/net/http.rb:564:in `write' from C:/Ruby/lib/ruby/1.8/net/http.rb:564:in `warn’  
> from C:/Ruby/lib/ruby/1.8/net/http.rb:564:in `connect' from C:/Ruby/lib/ruby/1.8/net/http.rb:553:in `do\_start’  
> from C:/Ruby/lib/ruby/1.8/net/http.rb:542:in `start' from C:/Ruby/lib/ruby/1.8/net/http.rb:1035:in `request’  
> from C:/Ruby/lib/ruby/1.8/net/http.rb:845:in `post' from (irb):15:in `authentication’  
> from (irb):23  
> from :0  
> irb(main):024:0\>

It may mean that google has summarily hung up on you.  
-=r

---

<div class="post-metadata">

**Author:** ![diskava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@diskava](https://www.ruby-forum.com/u/diskava)\
**Post date:** [April 12, 2009, 5:16am UTC](https://www.ruby-forum.com/t/bad-file-descriptor-error/165306/3 "2009-04-12T05:16:30Z")

</div>

Roger P. wrote:

> > Errno::EBADF: Bad file descriptor  
> > from C:/Ruby/lib/ruby/1.8/net/http.rb:564:in `write' from C:/Ruby/lib/ruby/1.8/net/http.rb:564:in `warn’  
> > from C:/Ruby/lib/ruby/1.8/net/http.rb:564:in `connect' from C:/Ruby/lib/ruby/1.8/net/http.rb:553:in `do\_start’  
> > from C:/Ruby/lib/ruby/1.8/net/http.rb:542:in `start' from C:/Ruby/lib/ruby/1.8/net/http.rb:1035:in `request’  
> > from C:/Ruby/lib/ruby/1.8/net/http.rb:845:in `post' from (irb):15:in `authentication’  
> > from (irb):23  
> > from :0  
> > irb(main):024:0\>
> 
> It may mean that google has summarily hung up on you.  
> -=r

Thank you Roger.Any suggestions on how I can fix this problem?Do I have  
to changing some parameters or something in those lines?

---

<div class="post-metadata">

**Author:** ![diskava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@diskava](https://www.ruby-forum.com/u/diskava)\
**Post date:** [April 12, 2009, 5:32am UTC](https://www.ruby-forum.com/t/bad-file-descriptor-error/165306/4 "2009-04-12T05:32:53Z")

</div>

> > It may mean that google has summarily hung up on you.  
> > -=r
> 
> Thank you Roger.Any suggestions on how I can fix this problem?Do I have  
> to changing some parameters or something in those lines?

very carefully check the parameters to make sure they’re right is the  
only thing I can imagine.

---

<div class="post-metadata">

**Author:** ![diskava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@diskava](https://www.ruby-forum.com/u/diskava)\
**Post date:** [April 12, 2009, 6:08am UTC](https://www.ruby-forum.com/t/bad-file-descriptor-error/165306/5 "2009-04-12T06:08:04Z")

</div>

Roger P. wrote:

> > > It may mean that google has summarily hung up on you.  
> > > -=r
> > 
> > Thank you Roger.Any suggestions on how I can fix this problem?Do I have  
> > to changing some parameters or something in those lines?
> 
> very carefully check the parameters to make sure they’re right is the  
> only thing I can imagine.

I definately will.

---

<div class="post-metadata">

**Author:** ![diskava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@diskava](https://www.ruby-forum.com/u/diskava)\
**Post date:** [April 12, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/bad-file-descriptor-error/165306/6 "2009-04-12T17:55:44Z")

</div>

On 12.04.2009 04:58, Roger P. wrote:

> > from :0  
> > irb(main):024:0\>
> 
> It may mean that google has summarily hung up on you.

I believe you would see a different error. EBADF occurs, if the file  
descriptor (i.e. the numeric value) is invalid. You can provoke it by  
doing something like this

ruby -e ‘File.open(10, “w”) {|f| f.puts “test”}’

Whereas it does not happen if you do

ruby -e ‘File.open(1, “w”) {|f| f.puts “test”}’

One way to create this is to open a file, remember the number returned  
from #fileno, close the file and then open another file with the  
remembered number (file descriptor).

Kind regards

robert
