Packer: Session locked on deleting port forwarding

Created on 22 May 2014  ·  13Comments  ·  Source: hashicorp/packer

Probably because of my not-so-performant host environment (Windows 7 x64, one Intel Core i5, 4Gb RAM) , I always hit the following error when trying to build a virtualbox VM:

==> virtualbox-iso: Gracefully halting virtual machine...
    virtualbox-iso: Removing floppy drive...
==> virtualbox-iso: Preparing to export machine...
    virtualbox-iso: Deleting forwarded port mapping for SSH (host port 3499)
==> virtualbox-iso: Error deleting port forwarding rule: VBoxManage error: VBoxManage.exe: error: The machine 'packer-virtualbox-iso' is already locked for a session (or
being unlocked)
==> virtualbox-iso: VBoxManage.exe: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component Machine, interface IMachine, callee IUnknown
==> virtualbox-iso: VBoxManage.exe: error: Context: "LockMachine(a->session, LockType_Write)" at line 462 of file VBoxManageModifyVM.cpp

I'm aware of https://github.com/mitchellh/packer/issues/184, but suggested workarounds don't work in my case, so I decided to investigate more.

It seems that for some reason VirtualBox need some time after the VM shutdown for terminating the session and release locks. During that time (_several seconds_ in my case) the VM status is already _powered off_, and since Packer only check for VM status before going ahead the above error happens.

Perhaps it might be worth sleeping for a bit after detecting the powered off state, or retry a couple of times on lock error before giving up.

buildevirtualbox good first issue upstream-bug

Most helpful comment

So maybe someone should open up a PR..

All 13 comments

Packer should retry a few times, I've tagged this as a bug to add that.

I added retry and timeout to builder/virtualbox/common/step_export.go. If deletion of the ports does not succeed within the max retries, the last error encountered is returned. Each failed attempt results in a short sleep to give the VM a little time to shutdown.

I choose arbitrary numbers of 5 retries and 200 milliseconds of sleep after each attempt.

1) Should sleep and max retries be settings?
2) Thoughts on the current settings? Should the max retries and sleep time be different?

I'm also leaning towards making the loop a for {}, making the evaluation at the bottom of the loop. The current way results in a wasted sleep cycle if the port deletion fails due to max retries.

Link to the branch on my fork:
https://github.com/mohae/packer/tree/1193-vbox-del-port

I had similar error recently (Packer 0.10.1 ; VirtualBox 5.1.2).
I think I solved it changing:
time packr build $VM_NAME.json || VBoxManage import output-$VM_NAME/$VM_NAME.ovf
to

time packr build $VM_NAME.json 
VBoxManage import output-$VM_NAME/$VM_NAME.ovf

(I will add a test to check that the file exists ...)

Logs:

    vb-fedora-24: + rm -f /home/toto/VBoxGuestAdditions.iso
==> vb-fedora-24: Gracefully halting virtual machine...
==> vb-fedora-24: Preparing to export machine...
    vb-fedora-24: Deleting forwarded port mapping for the communicator (SSH, WinRM, etc) (host port 2284)
==> vb-fedora-24: Error deleting port forwarding rule: VBoxManage error: VBoxManage: error: The machine 'vb-fedora-24' is already locked for a session (or being unlocked)
==> vb-fedora-24: VBoxManage: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component MachineWrap, interface IMachine, callee nsISupports
==> vb-fedora-24: VBoxManage: error: Context: "LockMachine(a->session, LockType_Write)" at line 506 of file VBoxManageModifyVM.cpp
==> vb-fedora-24: Unregistering and deleting virtual machine...
==> vb-fedora-24: Deleting output directory...
Build 'vb-fedora-24' errored: Error deleting port forwarding rule: VBoxManage error: VBoxManage: error: The machine 'vb-fedora-24' is already locked for a session (or being unlocked)
VBoxManage: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component MachineWrap, interface IMachine, callee nsISupports
VBoxManage: error: Context: "LockMachine(a->session, LockType_Write)" at line 506 of file VBoxManageModifyVM.cpp

==> Some builds didn't complete successfully and had errors:
--> vb-fedora-24: Error deleting port forwarding rule: VBoxManage error: VBoxManage: error: The machine 'vb-fedora-24' is already locked for a session (or being unlocked)
VBoxManage: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component MachineWrap, interface IMachine, callee nsISupports
VBoxManage: error: Context: "LockMachine(a->session, LockType_Write)" at line 506 of file VBoxManageModifyVM.cpp

==> Builds finished but no artifacts were created.

I am also affected by this two year old bug.

So maybe someone should open up a PR..

i just started getting this error recently and it doesn't seem to be going away. any idea's on a fix?

I've just confirmed that this issue isn't seen on Virtualbox 5.1.2 (i was using 5.1.4)

* update *

The version of virtualbox isn't the problem (i'm seeing the same issue on 5.1.2). The fix is to shutdown all your vm's and kill virtualbox completely (all processes).

Maybe it is related to this issue https://github.com/mitchellh/packer/issues/3744
Was it in headless mode?
Also VirtualBox forum seems to suggest that there could be an VirtualBox API to call to know when it is ready (instead of polling).

Was it in headless mode?

No.

On Sep 5, 2016, at 4:39 AM, Bruno Vernay [email protected] wrote:

Maybe it is related with this issue #3744 https://github.com/mitchellh/packer/issues/3744
Was it in headless mode? Also VirtualBox forum seems to suggest that there could be an VirtualBox API to call to know when it is ready (instead of polling).


You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/mitchellh/packer/issues/1193#issuecomment-244690588, or mute the thread https://github.com/notifications/unsubscribe-auth/AAI68YwKRkmE3vIizEFZGejHcUtQDa0zks5qm9U1gaJpZM4B9V6F.

From the above ticket

For all affected versions, a workaround is to provision the instance headless.

I'm going to close this since it's an upstream issue, and so it's not actionable

{ "_comment": "Build withpacker build -var-file=centos74.json centos.json", "vm_name": "centos74", "cpus": "2", "disk_size": "524288", "http_directory": "kickstart/centos7", "iso_checksum": "aae20c8052a55cf179af88d9dd35f1a889cd5773", "iso_checksum_type": "sha1", "iso_name": "CentOS-7-x86_64-Minimal-1708.iso", "iso_url": "file:///data/downloads/packer1.2.3/centos/CentOS-7-x86_64-Minimal-1708.iso", "memory": "2048", "parallels_guest_os_type": "centos7", "post_shutdown_delay": "30s" }
You can try add to "post_shutdown_delay": "30s" centosxx.json file.

I export box successful under windows os finally.

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings