Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell: Initialization Error #603

Closed
cmcknz77 opened this issue Mar 23, 2017 · 43 comments
Closed

Powershell: Initialization Error #603

cmcknz77 opened this issue Mar 23, 2017 · 43 comments
Labels
Issue-Bug A bug to squash.

Comments

@cmcknz77
Copy link

cmcknz77 commented Mar 23, 2017

logs.zip

System Details

  • Operating system name and version: Windows 10 - x64 - 10.0.14393
  • VS Code version: 1.10.2
  • PowerShell extension version: 0.10.1
  • Output from $PSVersionTable:
    Name Value

PSVersion 5.1.14393.953
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.953
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Issue Description

When I open a file with a recognised 'Powershell' extension (.ps1,.psm1,*.psd1 etc) or having typed in some script I tell 'Code' that the language is 'PowerShell' instead of plain text, I get a notice that says 'Initialization Error' in the bottom right hand corner.
When I click on it I get an error in the Error bar that says "Error: Could not start language service:"
And if I click on the logs I see the following:

[0:2:23] Language server starting...
powershell.exe started --
pid: 928
exe: C:\WINDOWS\Sysnative\WindowsPowerShell\v1.0\powershell.exe
args: C:\Users\myuser\.vscode\extensions\ms-vscode.PowerShell-0.10.1\scripts\Start-EditorServices.ps1 -EditorServicesVersion '0.10.1' -HostName 'Visual Studio Code Host' -HostProfileId 'Microsoft.VSCode' -HostVersion '0.10.1' -BundledModulesPath 'C:\Users\myuser\.vscode\extensions\ms-vscode.PowerShell-0.10.1\modules' -EnableConsoleRepl -LogLevel 'Verbose' -LogPath 'C:\Users\myuser\.vscode\extensions\ms-vscode.PowerShell-0.10.1\logs\1490227343-bdec66a5-f1e8-4d56-9ce5-5e057642a56f1490227309017\EditorServices.log' -SessionDetailsPath 'C:\Users\myuser\.vscode\extensions\ms-vscode.PowerShell-0.10.1\sessions\PSES-VSCode-10944'

powershell.exe terminated or terminal UI was closed

[0:3:23] Language server startup failed.
Could not start language service:
Timed out waiting for session file to appear.

Now... I'm in a Domain environment where our Security Policy forced down from 'on high' by Group Policy is that we're in an allsigned environment. I have tried Code and the Powershell Extension on a number of machines now and I always get the same experience.

Incidentally, it appears that the module files:
modules\Plaster\Templates\NewPowerShellManifestModule\Module.psm1
modules\Plaster\Templates\NewPowerShellManifestModule\test\Module.T.ps1
are not signed - I don't know if this is related.

Attached Logs

logs.zip

@rkeithhill
Copy link
Contributor

rkeithhill commented Mar 23, 2017

Looks like we might also have a logging bug where we are not appending a \ between the home dir and the .vscode dir e.g. C:\Users\myuser.vscode should really be C:\Users\myuser\.vscode

@cmcknz77
Copy link
Author

Oooop. No Keith, that's my bad - I manually edited that output to obfuscate my username in this publicly visible post...

@cmcknz77
Copy link
Author

cmcknz77 commented Apr 15, 2017

New version of VSCode installed - March 2017 (version 1.11)
Powershell Extension upgraded to 0.12.2
Problem remains as described above - new logs attached.

System Details
Operating system name and version: Windows 10 - x64 - 10.0.14393
VS Code version: 1.11.0
PowerShell extension version: 0.12.2
Output from $PSVersionTable:
PSVersion 5.1.14393.953
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.953
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

logs-1.11.0.zip

@daviwil daviwil added this to the 1.3.0 milestone May 25, 2017
@cmcknz77
Copy link
Author

New version of VSCode installed - May 2017 (version 1.12.2)
Powershell Extension upgraded to 1.1.0
Problem remains as described above - new logs attached.
System Details
Operating system name and version: Windows 10 - x64 - 10.0.14393
VS Code version: 1.12.2
PowerShell extension version: 1.1.0
Output from $PSVersionTable:
PSVersion 5.1.14393.953
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.953
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

New logs attached
logs.zip

@daviwil
Copy link
Contributor

daviwil commented May 26, 2017

Thanks @cmcknz77! There's definitely an exception happening during initialization that we're not capturing in a way that you can observe. Can you try these steps:

  1. Open the file C:\Users\cmckenzie\.vscode\extensions\ms-vscode.powershell-1.1.0\scripts\Start-EditorServices.ps1 in the editor
  2. Replace its contents entirely with the contents of this script: https://raw.githubusercontent.com/daviwil/vscode-powershell/eb8b2d31d2fafe5e2e09cdfed7f811c74d9d1bcf/scripts/Start-EditorServices.ps1
  3. Restart VS Code and open a PowerShell script.
  4. Copy the error that gets printed to the PowerShell Integrated Console when it starts up

Hopefully an error will be printed so that we can see what exception you're getting. This improvement to the Start-EditorServices script will be included with the next extension update.

@ghost
Copy link

ghost commented May 27, 2017

Thanks @daviwil . I had same issue with VScode on linux. That did the trick for me!

@daviwil
Copy link
Contributor

daviwil commented May 27, 2017

@Ghoshnik interesting, this script shouldn't have fixed anything, it just prints out more diagnostic information. Can you tell me what version of PowerShell Core you're using on Linux?

@ghost
Copy link

ghost commented May 27, 2017

Weird.

Name Value


PSVersion 6.0.0-beta
PSEdition Core
BuildVersion 3.0.0.0

@daviwil
Copy link
Contributor

daviwil commented May 27, 2017

Thanks! That's the right version to use for now. What was happening before, the Integrated Console just wouldn't start? I'll have to look at my script change a little more closely to see if there's an explanation for why it might have resolved that.

@daviwil daviwil modified the milestones: 1.3.0, June 2017 Jun 1, 2017
@xandora
Copy link

xandora commented Jun 7, 2017

@daviwil Is this the "diagnostic information" your modified Start-EditorServices.ps1 script should be outputting?

[15:30:49] Language server started.
{"debugServicePort":18142,"status":"started","channel":"tcp","languageServicePort":14149}
Connecting to language service on port 14149...

I was getting the Language Server failures this morning and tried your modified script, it seems to have fixed the issue as well, even though I understand it's supposed to just explain it better.

@daviwil
Copy link
Contributor

daviwil commented Jun 7, 2017

@xandora I was referring to error output in the Integrated Console if there was a crash on startup. The latest version of the PowerShell extension (1.2.1) includes this updated script, were you seeing the startup crash there too before using the script I linked to?

My suspicion is that the problem is being caused by the Start-EditorServices.ps1 script being signed in the official releases. If any of you have a few moments, could you uninstall/reinstall the PowerShell extension and then run the official Start-EditorServices.ps1 script inside of PowerShell.exe? You don't need to worry about the parameters, what I'm looking for is whether you get prompted about whether you want to run a signed script (Run Once should be one of the options).

Thanks!

@andrewlathom
Copy link

I'm having the same (or similar) problem. I get "Starting Powershell..." in the lower right forever. My log is attached. I've re-installed the 1.3.2 extension. What else can I try?

Thanks!
-Andy

EditorServices.log.txt

@daviwil
Copy link
Contributor

daviwil commented Jun 16, 2017

@andrewlathom could you turn on verbose logging and send new logs?

https://github.com/PowerShell/vscode-powershell#2-capture-verbose-logs-and-send-them-to-us

The current log is helpful but I'm not getting the full picture. Thanks!

@andrewlathom
Copy link

Here you go - thanks for looking at them

-Andy

EditorServices.log.txt

@daviwil
Copy link
Contributor

daviwil commented Jun 16, 2017

Strange that you'd see "Starting PowerShell" forever since it seems that your session has loaded successfully. Looks like you started the debugger at the end of the logs, was the debugger working for you after you did that or did it seem to hang?

@andrewlathom
Copy link

Whenever I try to start a script, either with or without the debugger, it doesn't run. I get the little Pause/Stop floating bar, but nothing executes.

@pythoninthegrass
Copy link

pythoninthegrass commented Jun 19, 2017

@daviwil Cross-posted in #572 after enabling verbose logging and replacing Start-EditorServices.ps1 with https://raw.githubusercontent.com/daviwil/vscode-powershell/eb8b2d31d2fafe5e2e09cdfed7f811c74d9d1bcf/scripts/Start-EditorServices.ps1

Thanks again!

EDIT 6-22-17
Turns out, I was running the Alpha 15 version. Had to upgrade from OS X 10.10 to 10.12 then install v6.0.0-beta.2. The PowerShell integrated console is running like a champ.

@daviwil daviwil modified the milestones: June 2017, July 2017 Jul 11, 2017
@CharlesSpencer
Copy link

Hi. I'm new here, so I hope I'm not violating any unwritten rules or stepping on any toes. I'm a Windows admin by trade and don't know jack-squat about development, so I apologize if I'm not expressing myself properly.

I'm having what I think is the same problem. VSCode itself starts okay but as soon as I open an existing file or try to start a new one I get an error ‘The language service could not be started’. I also get a 'PowerShell Initialization Error' message in the bottom right corner of the VSCode window. I uninstalled and reinstalled everything from scratch this afternoon, but apparently I didn't even do that properly since the few customizations I made were retained.

Is there a solution for this yet? My understanding of the previous posts is that the replacement script @daviwil linked is supposed to gather diagnostic data and isn't actually a fix.

Thanks, regardless.

PS 5, W10 64-bit Ent, VSC 1.14.2 64-bit

@andrewlathom
Copy link

andrewlathom commented Jul 21, 2017 via email

@janhanekom
Copy link

Had similar symptoms; in my case, it appears to be related to antivirus. McAfee had this to say in my Windows Application Event Log:

ExP:Illegal API Use Blocked an attempt to exploit C:\WINDOWS\SYSTEM32\WINDOWSPOWERSHELL\V1.0\POWERSHELL.EXE, which targeted the AtlComPtrAssign API.

I'll be taking it up with my AV admin.

@daviwil daviwil modified the milestones: July 2017, Future Oct 26, 2017
@cmcknz77
Copy link
Author

Bizarrely for me this problem appears to have disappeared after updateing to the latest VSCode code...

@TylerLeonhardt
Copy link
Member

Just wanted to check in since a few folks had their issues resolved. Is anyone else still having issues? I'd love some fresh logs if you have them. Thanks!

@janhanekom
Copy link

Thanks for following up... My issue persisted until my IT department added an exclusion in McAfee Endpoint Security's Exploit Prevention module that allows winpty-agent.exe to call PowerShell.exe using the AtlComPtrAssign API.

I don't know if it's possible to change the way the PowerShell console is launched inside of VS Code that makes it more friendly towards McAfee (and possibly other AV products), but I anticipate other McAfee users may run into similar issues in future.

@amitbha
Copy link

amitbha commented Apr 10, 2018

System Details

  • Operating system name and version: Archlinux - x64
  • VS Code version: 1.22.1
  • PowerShell extension version: 1.6.0
  • Output from $PSVersionTable:
Name Value
PSVersion 6.0.2
PSEdition Core
GitCommitId v6.0.2
OS Linux 4.14.31-1-MANJARO #1 SMP PREEMPT Wed Mar 28 21:42:49 UTC 2018
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

Issue Description

When I open a .ps1 file, then get a notice that says 'Initialization Error' in the vscode bottom. The detail log is:

Language server startup failed.
Could not start language service: Timed out waiting for session file to appear.

I change the setting "powershell.developer.editorServicesLogLevel" to "Verbose", and restart vscode, but there is no verb outputs, nothing changed. I think the Start-EditorServices.ps1 doesn't even run. I manually run it like below:

/home/phper/.vscode/extensions/ms-vscode.powershell-1.6.0/scripts/Start-EditorServices.ps1-EditorServicesVersion '1.6.0' -HostName 'Visual Studio Code Host' -HostProfileId 'Microsoft.VSCode' -HostVersion '1.6.0' -AdditionalModules @('PowerShellEditorServices.VSCode') -BundledModulesPath '/home/phper/.vscode/extensions/ms-vscode.powershell-1.6.0/modules' -EnableConsoleRepl -LogLevel 'Verbose' -LogPath '/tmp/VSCode Crashes/EditorServices.log' -SessionDetailsPath '/tmp/VSCode Crashes' -FeatureFlags @()

And get some error logs:

VERBOSE: /home/phper/.local/share/powershell/Modules
VERBOSE: /usr/local/share/powershell/Modules
VERBOSE: /opt/microsoft/powershell/6.0.2/Modules
VERBOSE: /home/phper/.vscode/extensions/ms-vscode.powershell-1.6.0/modules
VERBOSE: Testing module availability PowerShellGet
VERBOSE: PowerShellGet found
VERBOSE: Testing module availability PowerShellEditorServices 1.6.0
VERBOSE: PowerShellEditorServices 1.6.0 found
VERBOSE: Importing PowerShellEditorServices
VERBOSE: Searching for available socket port for the language service
VERBOSE: Checking port: 19024, attempts remaining 10 --------------------
VERBOSE: Testing availability of port 19024 at address 127.0.0.1 / InterNetwork
VERBOSE: Testing availability of port 19024 at address ::1 / InterNetworkV6
VERBOSE: SocketException on port 19024: System.Net.Sockets.SocketException (0x80004005): Cannot assign requested address
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at CallSite.Target(Closure , CallSite , Object )
VERBOSE: Port: 19024 is NOT available
VERBOSE: Checking port: 10635, attempts remaining 9 --------------------
VERBOSE: Testing availability of port 10635 at address 127.0.0.1 / InterNetwork
VERBOSE: Testing availability of port 10635 at address ::1 / InterNetworkV6
VERBOSE: SocketException on port 10635: System.Net.Sockets.SocketException (0x80004005): Cannot assign requested address
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at CallSite.Target(Closure , CallSite , Object )
VERBOSE: Port: 10635 is NOT available
VERBOSE: Checking port: 17063, attempts remaining 8 --------------------
VERBOSE: Testing availability of port 17063 at address 127.0.0.1 / InterNetwork
VERBOSE: Testing availability of port 17063 at address ::1 / InterNetworkV6
VERBOSE: SocketException on port 17063: System.Net.Sockets.SocketException (0x80004005): Cannot assign requested address
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at CallSite.Target(Closure , CallSite , Object )
VERBOSE: Port: 17063 is NOT available
VERBOSE: Checking port: 10331, attempts remaining 7 --------------------
VERBOSE: Testing availability of port 10331 at address 127.0.0.1 / InterNetwork
VERBOSE: Testing availability of port 10331 at address ::1 / InterNetworkV6
VERBOSE: SocketException on port 10331: System.Net.Sockets.SocketException (0x80004005): Cannot assign requested address
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at CallSite.Target(Closure , CallSite , Object )

VERBOSE: Did not find any available ports!!
Failed to find an open socket port for either the language or debug service.

What can I do?

@TylerLeonhardt
Copy link
Member

@amitbha I see you're using Arch. Has the extension ever worked for you?

@rkeithhill your extra logging is working :)

Cannot assign requested address

@amitbha
Copy link

amitbha commented Apr 12, 2018

@tylerl0706 My first time using vscode-powershell extension. It has never worked properly.

@TylerLeonhardt
Copy link
Member

TylerLeonhardt commented Apr 12, 2018

@amitbha: That's what I figured... this could very well be a PowerShell or .NET Core issue. We'll have to look deeper.

@TylerLeonhardt
Copy link
Member

@janhanekom:

RE: winpty-agent.exe to call PowerShell.exe using the AtlComPtrAssign API

Are you able to see if the PowerShell terminal that ships with VSCode works? In other words, not the PowerShell Integrated Console in VSCode, but the normal PowerShell console in VSCode.

@TylerLeonhardt
Copy link
Member

TylerLeonhardt commented Apr 12, 2018

@amitbha I'm not on Arch so it's going to be difficult to repro so I'm going to need your help.

Here's the function that tests the availability of a port:

function Test-PortAvailability {
    param(
        [Parameter(Mandatory=$true)]
        [int]
        $PortNumber
    )

    $portAvailable = $true

    try {
        if ($isPS5orLater) {
            $ipAddresses = [System.Net.Dns]::GetHostEntryAsync("localhost").Result.AddressList
        }
        else {
            $ipAddresses = [System.Net.Dns]::GetHostEntry("localhost").AddressList
        }

        foreach ($ipAddress in $ipAddresses)
        {
            Log "Testing availability of port ${PortNumber} at address ${ipAddress} / $($ipAddress.AddressFamily)"

            $tcpListener = New-Object System.Net.Sockets.TcpListener @($ipAddress, $PortNumber)
            $tcpListener.Start()
            $tcpListener.Stop()
        }
    }
    catch [System.Net.Sockets.SocketException] {
        $portAvailable = $false

        # Check the SocketErrorCode to see if it's the expected exception
        if ($_.Exception.SocketErrorCode -eq [System.Net.Sockets.SocketError]::AddressAlreadyInUse) {
            Log "Port $PortNumber is in use."
        }
        else {
            Log "SocketException on port ${PortNumber}: $($_.Exception)"
        }
    }

    $portAvailable
}

Can you tell me the exact line that causes the exception? You will probably need to remove the try/catch.

Let me know if you need help.

@amitbha
Copy link

amitbha commented Apr 12, 2018

@tylerl0706 I remove the try/catch, and the error message is:

An error occurred while starting PowerShell Editor Services:

Exception calling "Start" with "0" argument(s): "Cannot assign requested address"
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
   at System.Management.Automation.PSScriptCmdlet.DoEndProcessing()
   at System.Management.Automation.CommandProcessorBase.Complete()
Cannot assign requested address
   at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Bind(EndPoint localEP)
   at System.Net.Sockets.TcpListener.Start(Int32 backlog)
   at CallSite.Target(Closure , CallSite , Object )

@amitbha
Copy link

amitbha commented Apr 12, 2018

I print the parameters before tcpListener:

write-host "$ipAddress, $PortNumber"
$tcpListener = New-Object System.Net.Sockets.TcpListener @($ipAddress, $PortNumber)
$tcpListener.Start()
$tcpListener.Stop()

and the ouput is:

127.0.0.1, 28897
::1, 28897
127.0.0.1, 23946
::1, 23946
127.0.0.1, 11824
::1, 11824
127.0.0.1, 13519
::1, 13519
127.0.0.1, 10183
::1, 10183
127.0.0.1, 21058
::1, 21058
127.0.0.1, 26056
::1, 26056
127.0.0.1, 29872
::1, 29872
127.0.0.1, 13568
::1, 13568
127.0.0.1, 17054
::1, 17054
127.0.0.1, 24736
::1, 24736
127.0.0.1, 10205
::1, 10205
127.0.0.1, 18065
::1, 18065
127.0.0.1, 24230
::1, 24230
127.0.0.1, 23751
::1, 23751
127.0.0.1, 13816
::1, 13816
127.0.0.1, 28652
::1, 28652
127.0.0.1, 18357
::1, 18357
127.0.0.1, 12179
::1, 12179
127.0.0.1, 23602
::1, 23602

@TylerLeonhardt
Copy link
Member

@amitbha: So do all of those values throw the exception?

@amitbha
Copy link

amitbha commented Apr 12, 2018

@tylerl0706
Yes, all of them.

@TylerLeonhardt
Copy link
Member

TylerLeonhardt commented Apr 16, 2018

@amitbha I think this might be a bug on the .NET Core team. For some reason, it's having a hard time listening on ports. Here's what I think you should do:

if you have the dotnet sdk installed on your Arch machine:

Create a small C# console app that just does the following:

var tcpListener = new System.Net.Sockets.TcpListener("127.0.0.1", "12179");
tcpListener.Start();
tcpListener.Stop();

var tcpListener = new System.Net.Sockets.TcpListener("::1", "12179");
tcpListener.Start();
tcpListener.Stop();

I might not have the syntax perfectly.

If you DON'T have the dotnet sdk installed on your Arch machine:

Just use PowerShell

$tcpListener = New-Object System.Net.Sockets.TcpListener @("127.0.0.1", "12179")
$tcpListener.Start()
$tcpListener.Stop()

$tcpListener = New-Object System.Net.Sockets.TcpListener @("::1", "12179")
$tcpListener.Start()
$tcpListener.Stop()

Put that code and the exception thrown in a new issue here:
https://github.com/dotnet/corefx

along with anything else they need. If you're using the PowerShell example, mention that you're only using PowerShell because you don't have the .NET sdk installed.

Also, be sure to link this issue with that issue.

@amitbha
Copy link

amitbha commented Apr 17, 2018

@tylerl0706

Thanks for your patience. This code exactly find where the problem is:

1 $tcpListener = New-Object System.Net.Sockets.TcpListener @("127.0.0.1", "12179")
2 $tcpListener.Start()
3 $tcpListener.Stop()
4
5 $tcpListener = New-Object System.Net.Sockets.TcpListener @("::1", "12179")
6 $tcpListener.Start()
7 $tcpListener.Stop()

Line 6 throws the exception:

Exception calling "Start" with "0" argument(s): "Cannot assign requested address"
At tcp.test.ps1:6 char:1
$tcpListener.Start()
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : SocketException

I remember that, I disabled ipv6 on all network interfaces in sysctl.conf like this:
net.ipv6.conf.all.disable_ipv6 = 1

So, bug or not?

@rkeithhill
Copy link
Contributor

@amitbha Can you test this code:

function Log([string[]]$msg) {
    $msg | Write-Verbose
}

function Test-PortAvailability {
    param(
        [Parameter(Mandatory=$true)]
        [int]
        $PortNumber
    )

    if ($isPS5orLater) {
        $ipAddresses = [System.Net.Dns]::GetHostEntryAsync("localhost").Result.AddressList
    }
    else {
        $ipAddresses = [System.Net.Dns]::GetHostEntry("localhost").AddressList
    }

    foreach ($ipAddress in $ipAddresses)
    {
        try {
            Log "Testing availability of port ${PortNumber} at address ${ipAddress} / $($ipAddress.AddressFamily)"

            $tcpListener = New-Object System.Net.Sockets.TcpListener @($ipAddress, $PortNumber)
            $tcpListener.Start()
            $tcpListener.Stop()
            return $ipAddress
        }
        catch [System.Net.Sockets.SocketException] {
            # Check the SocketErrorCode to see if it's the expected exception
            if ($_.Exception.SocketErrorCode -eq [System.Net.Sockets.SocketError]::AddressAlreadyInUse) {
                Log "Port $PortNumber is in use."
            }
            else {
                Log "SocketException on port ${PortNumber}: $($_.Exception)"
            }
        }
    }

    $null
}

Test-PortAvailability 12179

The problem is that we test both ipv4 and ipv6 and if either of those fail, then we fail the port. That is not correct. Whichever one succeeds first, we should indicate that port is available but not only that the port is available but which address family we should use to connect. Hence the above returns $ipAddress on sucess and $null on failure.

@amitbha
Copy link

amitbha commented Apr 17, 2018

@rkeithhill The code runs well. The output is:

Testing availability of port 12179 at address 127.0.0.1 / InterNetwork


AddressFamily      : InterNetwork
ScopeId            : 
IsIPv6Multicast    : False
IsIPv6LinkLocal    : False
IsIPv6SiteLocal    : False
IsIPv6Teredo       : False
IsIPv4MappedToIPv6 : False
Address            : 16777343
IPAddressToString  : 127.0.0.1




[Done] exited with code=0 in 1.052 seconds

BTW, strangely, the Write-Verbose didn't output anything, so I changed it to Write-Host.

@amitbha
Copy link

amitbha commented Apr 17, 2018

I updated the 'Start-EditorServices.ps1' with the new code, and open the vscode In hope. But the problem 'Language server startup failed' persisted.
I checked the "sessions" directory, and there is no session file.

I copy the command in output and manually run it ike below:

/home/phper/.vscode/extensions/ms-vscode.powershell-1.6.0/scripts/Start-EditorServices.ps1-EditorServicesVersion '1.6.0' -HostName 'Visual Studio Code Host' -HostProfileId 'Microsoft.VSCode' -HostVersion '1.6.0' -AdditionalModules @('PowerShellEditorServices.VSCode') -BundledModulesPath '/home/phper/.vscode/extensions/ms-vscode.powershell-1.6.0/modules' -EnableConsoleRepl -LogLevel 'Verbose' -LogPath '/home/phper/.vscode/extensions/ms-vscode.powershell-1.6.0/logs/EditorServices.log' -SessionDetailsPath '/home/phper/.vscode/extensions/ms-vscode.powershell-1.6.0/sessions/PSES-VSCode-17491-944549' -FeatureFlags @()

And the session file is created sucessfully! Its content is:

{"channel":"tcp","status":"started","languageServicePort":13399,"debugServicePort":10317}

👽

@rkeithhill
Copy link
Contributor

Thanks for trying that. I'll submit a PR later tonight for this change to the script. The callsite for the Test-Availability function has to change to capture the IP address and pass it up to its caller.

@TylerLeonhardt
Copy link
Member

@rkeithhill didn't we recently make a change to check for IP v6 as well as IP v4 because sometimes a port was already taken with IP v6 but would show available to IP v4?

@rkeithhill
Copy link
Contributor

Yup and this proves that one of the two primary address families may not be available on a machine. Now need to fix it to handle that case without failing. :-)

rkeithhill added a commit that referenced this issue Apr 21, 2018
Fix #603 where system has ipv6 addresses disabled.

This is also more consistent with the PSES TCP listener which always
[IPAddress]::Loopback as the IP address.
rkeithhill added a commit that referenced this issue Apr 22, 2018
…1281)

Fix #603 where system has ipv6 addresses disabled.

This is also more consistent with the PSES TCP listener which always
[IPAddress]::Loopback as the IP address.
@TylerLeonhardt
Copy link
Member

@amitbha The fix will be included in the next release - going out next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug A bug to squash.
Projects
None yet
Development

No branches or pull requests

10 participants