igrigorik / em-synchrony

Fiber aware EventMachine clients and convenience classes
http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers
MIT License
1.04k stars 151 forks source link

MongoDB Socket Exception breaks inserts #99

Open tobyhede opened 12 years ago

tobyhede commented 12 years ago

Run into a weird one.

I cannot insert when using 'em-synchrony/em-mongo' , but I can read.

My insert code is pretty much lifted from the specs:

EventMachine.synchrony do
  db = EM::Mongo::Connection.new("localhost", 27017).db('hot_test')
  collection = db.collection('test')

  collection.insert(:name => 'two', :position => 1)
  EventMachine.stop
end

I can insert using the 'regular' em-mongo gem with EventMachine.

I am seeing a SocketException in the mongo log.

Sun Dec 25 13:15:42 [initandlisten] connection accepted from 127.0.0.1:51392 #4
Sun Dec 25 13:15:42 [conn4] Socket recv() conn closed? 127.0.0.1:51392
Sun Dec 25 13:15:42 [conn4] SocketException: remote: 127.0.0.1:51392 error: 9001 socket exception [0] server [127.0.0.1:51392] 
Sun Dec 25 13:15:42 [conn4] end connection 127.0.0.1:51392

This Exception also occurs when reading, but the values the read works:

Sun Dec 25 13:13:37 [initandlisten] connection accepted from 127.0.0.1:51378 #2
Sun Dec 25 13:13:37 [conn2] runQuery called hot_test.test {}
Sun Dec 25 13:13:37 [conn2]    used cursor: 0x102603a70
Sun Dec 25 13:13:37 [conn2] query hot_test.test nreturned:1 reslen:70 0ms
Sun Dec 25 13:13:37 [conn2] Socket recv() conn closed? 127.0.0.1:51378
Sun Dec 25 13:13:37 [conn2] SocketException: remote: 127.0.0.1:51378 error: 9001 socket exception [0] server [127.0.0.1:51378]

Using MongoDB v2.0.1 on OS X.

tobyhede commented 12 years ago

The plot thickens.

If I have a reference to collection.find in my code, everything suddenly works as expected.

So the following will do the insert:

EventMachine.synchrony do
  db = EM::Mongo::Connection.new("localhost", 27017).db('hot_test')
  collection = db.collection('test')

  collection.insert(:name => 'two', :position => 1)
  collection.find
  EventMachine.stop
end

If I remove collection.find, the insert does nothing.

In all cases I see the SocketException in the mongo log

igrigorik commented 12 years ago

Toby, currently em-synchrony only patches the find methods in em-mongo: https://github.com/igrigorik/em-synchrony/blob/master/lib/em-synchrony/em-mongo.rb#L62

Sorry about the confusion with this.. Two things we can do: (a) extend the patch to more methods, or (b) for a "quick fix" you can use Synchrony.sync interface to make the insert operations behave as you would expect.

igrigorik commented 12 years ago

FYI: https://github.com/igrigorik/em-synchrony/pull/98

tobyhede commented 12 years ago

Thanks for the info. Had me stumped, but eventually had discovered the missing methods and started my own implementation. May progress with this while keeping an eye on these other branches to see what I may be able to contribute.

sujal commented 12 years ago

@tobyhede - I just updated my branch on that pull request with more functions wrapped or rewritten. Would love to both see your work and get your thoughts on the work I've done so far. As of right now, I'm close to code complete, am moving onto testing/bug fixing unless someone finds something wrong or broken.

We're going to try and put this into production soon (Weeks), so would love to get another set of eyes and or opinions into the mix.

Sujal

igrigorik commented 12 years ago

Any updates on this?