[WSL Only] Connection timeout exceptions don't get caught

On WSL (ubuntu), I use the following code to check if there is a connection to Redis

      redis = Redis.new
    rescue e
      puts "Redis Error: #{e}"

This works perfectly fine on any non-WSL system (tested it on my debian vps). It catches the exception perfectly and normally.
However, on WSL this exception does NOT get raised! The console just hangs indefinitely (forever). This is also true for TCPSocket as well, not just Redis.

I know I could just dual boot and go to Linux, but this is the first time WSL has ever been an issue. Been using WSL for a year+ with no issues and love it dearly. Of course this is not a problem with crystal itself because it works fine on non-WSL systems.

If anyone does have some extra time and could help with this, it would be greatly appreciated, thanks!

My personal advice is to simply install something like VMware workstation ( look on ebay for cheap keys ), install Ubuntu and run your code on this. Its faster in compiling ( WSL is a lot slower because of the Windows IO access ) and it avoids WSL issues. I found WSL can create plenty of issues. It works but its those edge cases that cost you a lot of time to figure out, if its Crystal or WSL.

1 Like

The networking stack in WSL is a weird mix of Linux and Windows behaviour. Most things work work really well, but it’s been known for also having many issues.

There is really nothing you can do about it.

You should setup a VM and use that for network related stuff.

1 Like

Oh really? Did not know the compilation process is more performant on non-WSL. I did buy a SSD to dual boot Linux Mint (or any distro really), but I accidentally purchased an M2 one instead of SATA :stuck_out_tongue:. I think I might just go the dual boot route. I don’t really want to go the VM route because last time I did that, it was a PITA to setup editing files and whatnot.

I don’t really want to go the VM route because last time I did that, it was a PITA to setup editing files and whatnot.

You really do not need to do a lot of setup editing files…

  • Install VmWare Workstation
  • Select create VM + Select ISO file
  • Set Disk size
  • Do install process ( what you need to do anyway for dual booting )

And then SSH into the IP your VM runs ( and install Crystal etc ). These days its very easy. The only difficulty is that your remote editing files ( Assuming that you only install the Server edition ), so your editor needs to support some protocol that you can SSH into your vmbox.

If you want the full experience, install Ubuntu and run the VM full screen. With 3D acceleration enabled, its feels almost as fast as the real deal.

And you will thank us because your boot sector is not some horrible mess the next time you need to reinstall windows or linux.

Setting up a virtual machine is really not a big effort. And much more comfortable than dual boot.

The easiest path to do that for Crystal development is probably to install Virtualbox (can use VmWare as well, but Virtualbox is open source) and vagrant and run vagrant up xenial64 in the checked out repository crystal-lang/crystal. It has a Vagrantfile which sets up everything for you.
Note that you need to run vagrant commands from Windows prompt, it won’t work from WSL. Instead of vagrant ssh you need to manually setup ssh from vagrant ssh-config.

1 Like

Thanks all for the help! I actually am not going to use Redis. Too much of an undertaking atm and far too much code that needs to be rewritten.
I will definitely try the virtual machine route later though for sure, ya’ll have convinced me :stuck_out_tongue:

Mostly because I was looking at the MT pull request. Seems like once that gets added, Redis (for my use case, scaling crystal instances on one server… wouldn’t really be ideal).