# NoMethodError in protocol.rb

**URL:** <https://www.ruby-forum.com/t/nomethoderror-in-protocol-rb/78624>\
**Category:** Ruby\
**Created:** [December 7, 2006, 11:14am UTC](https://www.ruby-forum.com/t/nomethoderror-in-protocol-rb/78624 "2006-12-07T11:14:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [December 7, 2006, 11:14am UTC](https://www.ruby-forum.com/t/nomethoderror-in-protocol-rb/78624/1 "2006-12-07T11:14:10Z")

</div>

Does this look familiar to anyone?

NoMethodError: undefined method `+' for nil:NilClass at /usr/local/lib/ruby/1.8/net/protocol.rb:176:in`write0’

I’m getting it sporadically during some fairly heavy XMLRPC traffic. It  
doesn’t make very much sense to me, because write0 looks like this:

def write0(str)  
@debug\_output \<\< str.dump if @debug\_output  
len = @io.write(str)  
@written\_bytes += len # \<= line 176  
len  
end

The error must mean that @written\_bytes is nil. It’s only called from  
within a block passed to writing, which looks like this:

def writing  
@written\_bytes = 0  
@debug\_output \<\< '\<- ’ if @debug\_output  
yield  
@debug\_output \<\< “\n” if @debug\_output  
bytes = @written\_bytes  
@written\_bytes = nil  
bytes  
end

Is this a known thread-safety issue with a known solution, or would a  
patch be in order? I’ve been testing this on 1.8.4, because that’s what  
we’ve got in production. Does anyone know if it’s been addressed in  
1.8.5?

Thanks,

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [December 7, 2006, 11:32am UTC](https://www.ruby-forum.com/t/nomethoderror-in-protocol-rb/78624/2 "2006-12-07T11:32:16Z")

</div>

Alex Y. wrote:

> len = @io.write(str)  
> yield  
> You aren’t showing us the block to which this yield refers. Does that  
> block  
> modify @written\_bytes?

> @debug\_output \<\< “\n” if @debug\_output  
> bytes = @written\_bytes  
> @written\_bytes = nil  
> bytes

Okay, here you are modifying the @written\_bytes class variable,  
assigning it  
nil, and this is likely to be the problem. Can I ask why you are using  
an  
instance variable here rather than a method-local one? Can’t this need  
be  
met by a local variable?

> end
> 
> Is this a known thread-safety issue with a known solution, or would a  
> patch be in order? I’ve been testing this on 1.8.4, because that’s what  
> we’ve got in production. Does anyone know if it’s been addressed in  
> 1.8.5?

AFAICS, it’s not yet obvious to me that it is a language issue, as  
opposed  
to a coding issue.

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [December 7, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-protocol-rb/78624/3 "2006-12-07T21:32:56Z")

</div>

On Dec 7, 2006, at 02:13 , Alex Y. wrote:

> @debug\_output \<\< str.dump if @debug\_output  
> @debug\_output \<\< '\<- ’ if @debug\_output  
> yield  
> @debug\_output \<\< “\n” if @debug\_output  
> bytes = @written\_bytes  
> @written\_bytes = nil

Yes, this is going to be a problem in a multithreaded environment.

> bytes  
> end
> 
> Is this a known thread-safety issue with a known solution, or would  
> a patch be in order? I’ve been testing this on 1.8.4, because  
> that’s what we’ve got in production.

I’ve not heard of it before.

> Does anyone know if it’s been addressed in 1.8.5?

Doubtful.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [December 7, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-protocol-rb/78624/4 "2006-12-07T22:45:12Z")

</div>

On Dec 7, 2006, at 02:30 , Paul L. wrote:

> > def write0(str)  
> > def writing  
> > @written\_bytes = 0  
> > @debug\_output \<\< '\<- ’ if @debug\_output  
> > yield
> 
> You aren’t showing us the block to which this yield refers. Does  
> that block  
> modify @written\_bytes?

Irrelevant. #write0 is the code you are looking for.

> > @debug\_output \<\< “\n” if @debug\_output  
> > bytes = @written\_bytes  
> > @written\_bytes = nil  
> > bytes
> 
> Okay, here you are modifying the @written\_bytes class variable

There are no class variables here, only instance variables.

> , assigning it nil, and this is likely to be the problem. Can I ask  
> why you are using an instance variable here rather than a method-  
> local one? Can’t this need be met by a local variable?

This is from protocol.rb. See #write0 above for why an instance  
variable in necessary.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [December 8, 2006, 8:14am UTC](https://www.ruby-forum.com/t/nomethoderror-in-protocol-rb/78624/5 "2006-12-08T08:14:00Z")

</div>

Eric H. wrote:

> On Dec 7, 2006, at 02:13 , Alex Y. wrote:
> 
> > Does this look familiar to anyone?
> > 
> > NoMethodError: undefined method `+' for nil:NilClass at /usr/local/lib/ruby/1.8/net/protocol.rb:176:in`write0’
> 
> > bytes  
> > Doubtful.

I’ve found a workaround for our code, anyway. I’m not quite sure why I  
was surprised that the HTTP library isn’t thread-safe 🙂

Thanks,
