[vbox-dev] Race condition?

Klaus Espenlaub klaus.espenlaub at oracle.com
Mon Feb 25 16:38:17 GMT 2013

Hi Max,

On 23.02.2013 03:17, Maxime Dor wrote:
> Hello,
> Given the following Java code snipet :
> I am running this using the 4.2 API (from the 4.2.4 SDK to be precise)
> against Web Services running on a Debian Testing box.
> When running my Unit tests, I keep having an intermitent error on the
> following part :
>> if (session.getState().equals(SessionState.Locked)) {
>> session.unlockMachine();
>> }
> /session.unlockMachine()/ throws an exception with : "/The session is
> not locked (session state: Unlocked) (0x8000FFFF)/". This exception is
> thrown about 1 out of 5 times I use this method or a copy of this one
> with only/.saveState()/ instead of/.powerDown()/ on the machine's
> console object.
> Am I missing something? From what I see here, it looks like the session
> state changes between the IF statement and the actual unlockMachine()
> call, looking like a race condition - or a bug?

You're not missing anything, and it's not a bug either. It's an inherent 
API race, since there is nothing preventing session state changes 
between the if statement and the unlockMachine() call (and there can't 
be anything preventing it).

The cause is that the operations you're doing (saveState/powerDown) are 
terminating the VM, and at the very end of those operations the VM 
session will be taken down (which implies that the associated shared 
session locks are gone too). Your API client code is racing with exactly 
this session cleanup which can extend slightly after the operation 
already signaled completion through the corresponding IProgress object.

So it's up to well-written code to be prepared for such spurious session 
state changes (which might happen for other reasons actually, e.g. if 
the VM process is terminated concurrently by some other API client, 
which can happen even if your code doesn't). Your code has to handle it.


> Thank you for your insight.
> Best regards,
> Max
> _______________________________________________
> vbox-dev mailing list
> vbox-dev at virtualbox.org
> https://www.virtualbox.org/mailman/listinfo/vbox-dev

Oracle <http://www.oracle.com>
Dr. Klaus Espenlaub | Software Development Director
Phone: +49 7151 60405 205 <tel:+49715160405205>
Oracle VM VirtualBox

ORACLE Deutschland B.V. & Co. KG | Werkstr. 24 | 71384 Weinstadt

ORACLE Deutschland B.V. & Co. KG
Hauptverwaltung: Riesstr. 25, D-80992 München
Registergericht: Amtsgericht München, HRA 95603
Geschäftsführer: Jürgen Kunz

Komplementärin: ORACLE Deutschland Verwaltung B.V.
Hertogswetering 163/167, 3543 AS Utrecht, Niederlande
Handelsregister der Handelskammer Midden-Niederlande, Nr. 30143697
Geschäftsführer: Alexander van der Ven, Astrid Kepper, Val Maher

Green Oracle <http://www.oracle.com/commitment> 	Oracle is committed to
developing practices and products that help protect the environment

More information about the vbox-dev mailing list