# Invalid arg to sysread deep within protocol.rb

**URL:** <https://www.ruby-forum.com/t/invalid-arg-to-sysread-deep-within-protocol-rb/64363>\
**Category:** Ruby\
**Created:** [December 8, 2005, 8:08pm UTC](https://www.ruby-forum.com/t/invalid-arg-to-sysread-deep-within-protocol-rb/64363 "2005-12-08T20:08:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![williamerubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@williamerubin](https://www.ruby-forum.com/u/williamerubin)\
**Post date:** [December 8, 2005, 8:08pm UTC](https://www.ruby-forum.com/t/invalid-arg-to-sysread-deep-within-protocol-rb/64363/1 "2005-12-08T20:08:01Z")

</div>

I just made a script that fetches certain web pages, using Net::HTTP,  
over and over in a loop. Everything worked fine, for about 5000  
iterations, but then the following error was thrown (this is in Ruby  
1.8.3):

C:/Ruby/lib/ruby/1.8/net/protocol.rb:133:in `sysread' C:/Ruby/lib/ruby/1.8/net/protocol.rb:133:in`rbuf\_fill’  
C:/Ruby/lib/ruby/1.8/timeout.rb:45:in `timeout' C:/Ruby/lib/ruby/1.8/timeout.rb:62:in`timeout’  
C:/Ruby/lib/ruby/1.8/net/protocol.rb:132:in `rbuf_fill' C:/Ruby/lib/ruby/1.8/net/protocol.rb:116:in`readuntil’  
C:/Ruby/lib/ruby/1.8/net/protocol.rb:126:in `readline' C:/Ruby/lib/ruby/1.8/net/http.rb:1988:in`read\_status\_line’  
C:/Ruby/lib/ruby/1.8/net/http.rb:1977:in `read_new' C:/Ruby/lib/ruby/1.8/net/http.rb:1046:in`request’  
C:/Ruby/lib/ruby/1.8/net/http.rb:944:in `request\_get’

The appropriate part in protocol.rb is this:

```
def rbuf_fill
  timeout(@read_timeout) {
    @rbuf << @io.sysread(1024)
  }
end

```

So sysread suddenly considers “1024” to be an invalid number of bytes  
to read? After having considered it valid on each of the previous 5000  
iterations?

Any clues? Thanks.

---

<div class="post-metadata">

**Author:** ![williamerubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@williamerubin](https://www.ruby-forum.com/u/williamerubin)\
**Post date:** [December 8, 2005, 9:30pm UTC](https://www.ruby-forum.com/t/invalid-arg-to-sysread-deep-within-protocol-rb/64363/2 "2005-12-08T21:30:07Z")

</div>

On Dec 8, 2005, at 2:07 PM, William E. Rubin wrote:

> C:/Ruby/lib/ruby/1.8/net/protocol.rb:116:in `readuntil’  
> @rbuf \<\< @io.sysread(1024)  
> }  
> end
> 
> So sysread suddenly considers “1024” to be an invalid number of bytes  
> to read? After having considered it valid on each of the previous 5000  
> iterations?
> 
> Any clues? Thanks.

Maybe there aren’t 1024 bytes left? According to the docs sysread is  
supposed to raise EOFError at the end of the file, are you sure  
that’s not being raised?

---

<div class="post-metadata">

**Author:** ![williamerubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@williamerubin](https://www.ruby-forum.com/u/williamerubin)\
**Post date:** [December 8, 2005, 9:46pm UTC](https://www.ruby-forum.com/t/invalid-arg-to-sysread-deep-within-protocol-rb/64363/3 "2005-12-08T21:46:28Z")

</div>

> Maybe there aren’t 1024 bytes left? According to the docs sysread is  
> supposed to raise EOFError at the end of the file, are you sure  
> that’s not being raised?

Yes, I’m sure. Now that I look back at my original post, I notice that  
I left out a crucial piece of information:

The error raised was an “Invalid Argument” error.

---

<div class="post-metadata">

**Author:** ![williamerubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@williamerubin](https://www.ruby-forum.com/u/williamerubin)\
**Post date:** [December 8, 2005, 11:00pm UTC](https://www.ruby-forum.com/t/invalid-arg-to-sysread-deep-within-protocol-rb/64363/4 "2005-12-08T23:00:09Z")

</div>

This seems to occur every few thousand iterations (not the same number  
of iterations every time).
