views:

3530

answers:

3

I am writing a very simple RMI server, and I am seeing intermittent java.rmi.NoSuchObjectExceptions in the unit tests.

I have a string of remote method calls on the same object, and while the first few go through, the later ones will sometimes fail. I am not doing anything to unregister the server object in between.

These error do not appear always, and if I put in breakpoints they tend to not appear. Are those Heisenbugs, whose race conditions dissolve when looking at them through the slowed down execution of the debugger? There is no multi-threading going on in my test or server code (though maybe inside of the RMI stack?).

I am running this on Mac OS X 10.5 (Java 1.5) through Eclipse's JUnit plugin, and the RMI server and client are both in the same JVM.

What can cause these exceptions?

A: 

It's difficult to answer this question without looking at the code (which I guess will be big enough to not be publishable here). However, using Occam's razor, you have two possibilies

  • Server objects must be getting unregistered somehow
  • Since breakpoints stop the errors, it's definitely a race condition.

I would suggest you go over the code paths carefully keeping the two points above in mind.

talonx
+2  A: 

Some other questions to consider - First are you referencing an object instance or is the stub interface itself gone? If some object instance is gone, its for the usual reasons, it got dereferenced and GC'd, but if it's the interface then your RMI server end point loop quit for some reason.

The best debugging tool I've found so far is to turn on the java.rmi.server.logCalls=true property (see http://java.sun.com/j2se/1.5.0/docs/guide/rmi/javarmiproperties.html) and watch all the wonderfull information stream down your log window. This tells me what's up every time.

jos

jottos
+7  A: 
Greg Mattes
So what you are saying is that I need (on the server) to manually retain a reference to my server object, because the exported UnicastRemoteObject that I put into the Registry will not keep that object from being garbage-collected, which would leave me with a dangling reference in the Registry?
Thilo
I would have hoped that until I "unbind" the object, the RMI system would keep it alive.
Thilo
Bingo. The responsibility for keeping the reference seems to fall on the programmer. I had the same hope about the RMI system keeping the reference while binding is in effect. There's probably a good reason for it being the way it is (I've not studied the RMI sources). In any case, it doesn't seem to be overly intuitive.
Greg Mattes
Six months later ... and thanks.
Milan Ramaiya
Great analysis and a very illustrative example. You deserve more than a measly +1 for this ;)
Steen
Thank you! Hope this helped you. :)
Greg Mattes