Jump to content

AutoIt stops responding after Run()


Recommended Posts

Since I got new hardware I'm running into a new Issue that I have trouble to work around.

Randomly the script stops responding and I have to stop execution with CTRL + BREAK.

If the problem occurs, it does at the Run command in following function:

Func _vbmExecute($vbmCMD, $ConsoleWrite = 1, $FileDir = @ProgramFilesDir & "\Oracle\VirtualBox\", $vbmFLAG = "@SW_HIDE")
    If FileExists(@ProgramFilesDir & "\Oracle\VirtualBox\" & "VBoxManage.exe") Then $FileDir = @ProgramFilesDir & "\Oracle\VirtualBox\"
    If FileExists($FileDir & "VBoxManage.exe") = 0 Then
        Switch StringInStr($FileDir, "Program Files (x86)") ;x64 x86 swap
            Case 0
                $FileDir = StringReplace($FileDir, "Files", "Files (x86)", 1)
            Case Else
                $FileDir = StringReplace($FileDir, "Files (x86)", "Files", 1)
        EndSwitch
    EndIf

    $vbmCMD = $FileDir &"VBoxManage.exe" & " " & $vbmCMD
    
    ; SCRIPT STOPS RESPONDING WITH THIS LINE:
    Local $PID = Run($vbmCMD, $FileDir, @SW_HIDE, 0x2 + 0x4)

    ;Catch Response
    Local $ResponseText, $ErrorText, $rTimer = TimerInit()
    While TimerDiff($rTimer) < 15000
        $ResponseText &= StdoutRead($PID)
        $ErrorText &= StderrRead($PID)
        If @error Then ExitLoop
    WEnd

    If $ConsoleWrite = 1 Then
        ConsoleWrite($ResponseText)
        ConsoleWriteError($ErrorText)
    EndIf

    Return($ResponseText)
    #comments-start
        clonevm <target> --name <name> --basefolder <basefolder> --register
        startvm <target>
        controlvm <target> poweroff
        unregistervm <target> --delete
        http://www.virtualbox.org/manual/ch08.html
    #comments-end
EndFunc   ;==>_vbmExecute

 

The last time it stopped trying to poweroff the VM.

How can I prevent the script from hanging when VBoxManage.exe messes up again?

 

Edited by bootybay
Link to comment
Share on other sites

Two suggestions:

1 - Temporarily change the @SW_HIDE to @SW_SHOW to see what is happening in the run window.  This may give you a clue as to what is freezing up.

2 - @ProgramFilesDir will give different results depending on whether you are running the script in 32-bit or 64-bit modes.  If you are running 32-bit, but VirtualBox is installed 64-bit (or vise-versa), you will be attempting to run a non-existing executable.  Try pulling the location from the registry instead by looking at HKEY_LOCAL_MACHINE\SOFTWARE\Oracle\VirtualBox\InstallDir and HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Oracle\VirtualBox\InstallDir.

Link to comment
Share on other sites

I changed @SW_HIDE to @SW_SHOW and the script didn't stop yet (40 cycles). It usually breaks between 5 to 20 cycles. I do not like the CMD window popping up though. There is no information displayed in it anyway. But I want to be able to just rerun VBoxManage.exe if it fails. I'm already catching the stdout to be able to handle problems.

The script not responding kinda eliminates handling anything.

Link to comment
Share on other sites

Try adding more log statements, like consolewriting in the while loop, so you know more exactly where it stops.

$vbmCMD = $FileDir &"VBoxManage.exe" & " " & $vbmCMD

; SCRIPT STOPS RESPONDING WITH THIS LINE:
Local $PID = Run($vbmCMD, $FileDir, @SW_HIDE, 0x2 + 0x4)

ConsoleWrite("Pid: " & $PID)

;Catch Response
Local $ResponseText, $ErrorText, $rTimer = TimerInit()
While TimerDiff($rTimer) < 15000
    $ResponseText &= StdoutRead($PID)
    $ErrorText &= StderrRead($PID)
    
    sleep(3000) ; don't spam the console
    beep(1000,100) ; beep, so you know it's still going. (What if something is wrong with the console writing?)
    ConsoleWrite("Waited for " & TimerDiff($rTimer) & " msec. Out: '" & $ResponseText  & "'. Err: '" & $ErrorText & "'. @error: '" & @error & "'.")
    
    If @error Then ExitLoop
WEnd

ConsoleWrite("Done waiting. Out: '" & $ResponseText  & "'. Err: '" & $ErrorText & "'. @error: '" & @error & "'.")

If $ConsoleWrite = 1 Then
    ConsoleWrite($ResponseText)
    ConsoleWriteError($ErrorText)
EndIf

 

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

@Sadbunny: I already did and commented in the code. Its exactly after the run command.

; HERE EVERYTHING IS FINE
Local $PID = Run($vbmCMD, $FileDir, @SW_HIDE, 0x2 + 0x4)
; EXACTLY AFTER THIS LINE THE SCRIPT HANGS

So the ConsoleWrite("Pid: " & $PID) in your example will not be displayed.

It happened also with @SH_SHOW now and you don't see the CMD window. Just the process VBoxManage.exe is still running with 0% CPU usage and 108K Memory usage.

 

How can I terminate VBoxManage.exe and rerun the command since AutoIt is not responding when VBoxManage.exe doesn't terminate?

It somehow seems like it's acting almost like RunWait and when VBoxManage.exe freaks out again and doesn't shut down autoit won't proceed.

Edited by bootybay
Link to comment
Share on other sites

Suggestion, log the values of the two variables directly before on each run, status after. As soon as it stops working, see if they are different on the fails?

Knowing the content of $vbmCMD and $FileDir is when it works, and fails should help.

Skysnake

Why is the snake in the sky?

Link to comment
Share on other sites

It's always happening with those parameters:

$vbmCMD = "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe controlvm DBClient poweroff"

$FileDir = "C:\Program Files\Oracle\VirtualBox\"

When it works the process VBoxManage.exe closes, otherwise it remains.

 

ProcessClose($PID) after the Run command doesn't help since it never finishes executing the Run command.

I just don't know how to handle VBoxManage.exe freezing when it somehow freezes autoit too. (Although autoit shouldn't care if it does)

 

 

EDIT: This is the windows error report:

Problem signature:
  Problem Event Name:   AppHangB1
  Application Name: AutoIt3.exe
  Application Version:  3.3.12.0
  Application Timestamp:    538b6694
  Hang Signature:   4b03
  Hang Type:    1
  OS Version:   6.1.7601.2.1.0.256.49
  Locale ID:    1031
  Additional Hang Signature 1:  4b03caab50f6acbcce282dd86e6f4f58
  Additional Hang Signature 2:  7154
  Additional Hang Signature 3:  71540a720fa18f0de663ec1302977db9
  Additional Hang Signature 4:  4b03
  Additional Hang Signature 5:  4b03caab50f6acbcce282dd86e6f4f58
  Additional Hang Signature 6:  7154
  Additional Hang Signature 7:  71540a720fa18f0de663ec1302977db9

 

 

Edited by bootybay
Link to comment
Share on other sites

It's always happening with those parameters:

$vbmCMD = "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe controlvm DBClient poweroff"

$FileDir = "C:\Program Files\Oracle\VirtualBox\"

Is the executable actually in Program Files, or is it in Program Files (x86)

Link to comment
Share on other sites

It's in that exact path and it is working too. It just stops responding randomly 1 out of 20 times.

 

I tried ShellExecute() and no freezes. Makes the handling of VBoxManage.exe quite stupid since I can't read the stdout anymore.

Maybe the stdout is the problem after all.

Link to comment
Share on other sites

Very strange. Maybe a feasible workaround is to run it with ShellExecute, but redirect the stdout and stderr to tempfiles and read those tempfiles? Demo:

C:\tmp>((echo hoi) & fail) 1>c:\tmp\stdout 2>c:\tmp\stderr

C:\tmp>type stderr
'fail' is not recognized as an internal or external command,
operable program or batch file.

C:\tmp>type stdout
hoi

 

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

I would love if you could elaborate on your suggestion since I'm really not fond of my CMD and BATCH capabilities. I'm also not quite sure how to execute your suggestion.

Should I run VBoxManage.exe via CMD and append >>C:\VBMecho to the command? And if this is the case should I use Run(@ComSpec & " /c " & $VBMdir & $VBMcmd) or rather Shellexecute CMD with $VBMdir & $VBMcmd as paremeters + enabling echo and directing it into a file?

Because the version with Run seems to have the same effect of freezing autoit according to this topic https://www.autoitscript.com/forum/topic/138827-runcomspec-c-~-application-hangs/

 

Link to comment
Share on other sites

Well, as you seem to have trouble with Run and not with ShellExecute, I'd go with the latter for the time being :) And then specifically ShellExecuteWait, so that you know the command has completed before you read the output files (otherwise you may not be able to open them for reading anyway).

Say we have this batchfile:

C:\tmp>type mytest.bat
@echo off

echo This is printed to stdout
echo This is printed to stderr >&2

This will represent the VBoxManage.exe and it will write stuff to both stdout and stderr. Now, if we want to catch both those streams in two separate tempfiles from outside of that batchfile, we would execute in dos: mytest.bat 2>my_stderr_output.txt >my_stdout_output.txt . So that's what we have to reproduce in AutoIt. Something like this:

#include <Array.au3>
#include <File.au3>

$file_stdout = _TempFile(@TempDir, "")
$file_stderr = _TempFile(@TempDir, "")

ShellExecuteWait("c:\tmp\mytest.bat", " 2> " & $file_stderr & " > " & $file_stdout, "", "", @SW_HIDE)

Local $aOut = FileReadToArray($file_stdout)
Local $aErr = FileReadToArray($file_stderr)

FileDelete($file_stdout)
FileDelete($file_stderr)

_ArrayDisplay($aOut, "stdout")
_ArrayDisplay($aErr, "stderr")

Using _TempFile() guarantees your tempfile will be unique, making it safer against concurrent runs and such headaches. Using >> instead of > (append instead of overwrite) doesn't make a lot of sense as you're interested in the output of a single system call, if I read you correctly.

Hope this helps?

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...