# Net::Imap thread safety

**URL:** <https://rubytalk.org/t/net-imap-thread-safety/49689>\
**Category:** ruby-talk\
**Created:** [17 October 2008 11:10 UTC](https://rubytalk.org/t/net-imap-thread-safety/49689 "2008-10-17T11:10:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ingmar](https://avatars.discourse-cdn.com/v4/letter/i/cc9497/32.png) [@Ingmar](https://rubytalk.org/u/Ingmar)\
**Post date:** [17 October 2008 11:10 UTC](https://rubytalk.org/t/net-imap-thread-safety/49689/1 "2008-10-17T11:10:57Z")

</div>

Hi,

I have some problems with the Net::Imap standard library. In the docs,  
it claims to supports concurrent threads. However, running a lot of  
multiple threads invoking the same IMAP command on a Net::Imap object  
results in errors:

undefined method `[]' for nil:NilClass (NoMethodError)

The exact places where the error occurs are different depending on  
whether Ruby 1.8 or 1.9 are used. Here's an example program spawning a  
lot of concurrent threads:

#!/usr/bin/env ruby -w  
require 'net/imap'

$HOST = ""  
$PORT = 993  
$SSL = true  
$AUTH\_MECH = "LOGIN"

$USER = ""  
$PASSWORD = ""

conn = Net::IMAP.new($HOST, $PORT, $SSL, nil, false)  
conn.authenticate($AUTH\_MECH, $USER, $PASSWORD)

threads = []  
100.times do  
&nbsp;&nbsp;threads \<\< Thread.new do  
&nbsp;&nbsp;&nbsp;&nbsp;conn.capability  
&nbsp;&nbsp;end  
end  
threads.each { |t| t.join }

(you will need to specify your own server/login data)

Ruby 1.8 will usually terminate at /usr/lib/ruby/1.8/net/imap.rb:  
314:in `capability'  
Ruby 1.9 will usually terminate at /usr/lib/ruby/1.9.0/net/imap.rb:  
317:in `block in capability'

Apparently (from looking at the source)  
@responses.delete("CAPABILITY") seems to return Nil. Net::Imap is  
using Monitor to synchronize access to @responses, however I can't  
imagine Monitor to be flawed.

Is it possible that I'm simply querying the server too frequently? If  
so, shouldn't Net::Imap's error handling be improved at this point?  
Because it's hard for someone using the same Net::Imap object in  
concurrent threads to limit the frequency of the access.

Thanks for your thoughts!
