Skip to content

Commit

Permalink
EnvironmentSetupUtils: fix the process cannot access the file because…
Browse files Browse the repository at this point in the history
… it is being used by another process error on Windows (references #449)

Fixed by using synchronous file APIs isntead of asynchronous. Determined that even after Event.CLOSE is dispatched by the FileStream, the file may still be considered in use. If opened synchronously instead, file appears to be completely closed immediately after calling close().

Even with the 1 second delay between calls, the being used by another process error could still appear. This started happening more frequently when calling java -version before starting language servers, if Moonshine opened with multiple projects.
  • Loading branch information
joshtynjala committed Dec 10, 2020
1 parent 11f8f04 commit 176dcab
Showing 1 changed file with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,20 @@ package actionScripts.utils
return;
}

//this previously used FileUtils.writeToFileAsync(), but commands
//would sometimes fail because the file would still be in use, even
//after the FileStream dispatched Event.CLOSE
windowsBatchFile = File.applicationStorageDirectory.resolvePath("setLocalEnvironment.cmd");
FileUtils.writeToFileAsync(windowsBatchFile, setCommand, onBatchFileWriteComplete, onBatchFileWriteError);
try
{
FileUtils.writeToFile(windowsBatchFile, setCommand);
}
catch(e:Error)
{
onBatchFileWriteError(e.toString());
return;
}
onBatchFileWriteComplete();
}

private function executeOSX():void
Expand Down Expand Up @@ -311,26 +323,19 @@ package actionScripts.utils

private function onBatchFileWriteComplete():void
{
// following timeout is to overcome process-holding error
// in vagarant as reported by Joel at
// https://github.com/prominic/Moonshine-IDE/issues/449#issuecomment-473418675
var timeoutValue:uint = setTimeout(function():void
if (externalCallCompletionHandler != null)
{
clearTimeout(timeoutValue);
if (externalCallCompletionHandler != null)
{
// returns batch file path to be
// executed by the caller's nativeProcess process
if (windowsBatchFile) externalCallCompletionHandler(windowsBatchFile.nativePath);
// returns batch file path to be
// executed by the caller's nativeProcess process
if (windowsBatchFile) externalCallCompletionHandler(windowsBatchFile.nativePath);

isSingleProcessRunning = false;
flush();
}
else if (windowsBatchFile)
{
onCommandLineExecutionWith(windowsBatchFile.nativePath);
}
}, 1000);
isSingleProcessRunning = false;
flush();
}
else if (windowsBatchFile)
{
onCommandLineExecutionWith(windowsBatchFile.nativePath);
}
}

private function onCommandLineExecutionWith(command:String):void
Expand Down

0 comments on commit 176dcab

Please sign in to comment.