work around Windows Subsystem for Linux issue #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This isn't quiiiite an ideal solution. The idea is to attempt to open two servers on one port, and if that succeeds, assume that we're on WSL and switch future logic from using opening servers to using opening connections, which is slower.
The issue where WSL lets you open multiple servers on one port only seems to manifest if all of those servers were opened from within WSL. If one was already opened from within Windows itself, attempts to open one from within WSL fail with the expected EADDRINUSE. I'm guessing what's going on is that all of WSL is running as one Windows process and is taking care of the proxying to the virtual Linux processes itself - and so this is an understandable bug, but something I'd still consider a bug.
Anyway, what this means is that, with this PR, when we attempt to open two concurrent servers on one port, it might be that the first one was already opened in Windows, and that we still really are running in WSL - but in this case, we won't be able to determine that with this port. What I'm doing here is just assuming that means we're good to go (i.e., we're not on WSL) which isn't ideal. So either we need to check for WSL using a port that Definitely For Real Probably isn't in use by anything else that's likely to be running on the user's computer, or else we try with multiple different ports and keep going until we find one that we can open the first time (this is probably the best solution, it just would make this look uglier), or else we need to use a completely separate method for determining whether we're on WSL (which smells like an analogue of browser UA sniffing as opposed to feature detection, which isn't great).
Anyway, what there is here works better on WSL than what there was before (although not perfect). and it works marginally slower than before on non-WSL (because of the extra check at the beginning).