WSL: Fetch requests to web app accessed on localhost gets stuck in pending state

Version

Windows 11 Pro 21H2 (10.0.22000.469)

WSL Version

  • WSL 2
  • WSL 1

Kernel Version

5.10.81.1

Distro Version

Ubuntu 20.04

Other Software

Node web app using any of the following:

Repro Steps

I’ve experienced the issue describe below using a few Node frameworks but the quickest way to reproduce is to do one of the following

Next.js Blog Example

  1. Open a Terminal into WSL2 within which Node is installed.
  2. Get the IP address for WSL2 ip addr | grep eth0 for use later
  3. Create a directory to use for our app and cd into it.
  4. Create the Next.js app using the blog template npx create-next-app -e blog .
  5. Run npm run dev to start the app on port 3000 (default port)
  6. In Windows open any browser and open the Dev Tools on the Networking tab
  7. Access the app on http://localhost:3000
  8. Wait over 5 seconds and click on “Posts” link
  9. Note the fetch request in the Network tab is stuck in a pending state. The Timing section of the fetch request in the Network tab shows it stuck in the Initial Connection phase.
  10. Now access the site against the IP address on port 3000 and you’ll see the fetch requests always work

Express-based Hello World

  1. Open a Terminal into WSL2 within which Node is installed
  2. Get the IP address for WSL2 ip addr | grep eth0 for use later
  3. Create a directory to use for our app and cd into it.
  4. Initialise a new Node app npm init -y.
  5. Install Express npm install --save express
  6. Create app.js with the following Hello World code
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

const server = app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
  1. Run the app node app.js
  2. In Windows open any browser and open the Dev Tools on the Networking tab
  3. Access the app on http://localhost:3000
  4. Refresh the app a few times and you’ll notice the fetch request eventually gets stuck in a Pending state. The Timing section of the fetch request in the Network tab shows it stuck in the Initial Connection phase.
  5. Now access the site against the IP address on port 3000 and you’ll see the fetch requests always work
  6. Close the app (Ctrl + C) and edit the app.js and add the following as the last line in the app
server.keepAliveTimeout = 0;
  1. Rerun the app node app.js and you’ll find the app no longer gets stuck in a pending state against http://localhost:3000.

Expected Behavior

Browsing a node-based web app running in WSL2 should not have network issues when accessed on localhost.

Actual Behavior

If I access a Node application running inside of WSL2 in the browser on localhost, the Network tab will show that some fetch requests get stuck in a pending state. The Network details shows that it gets stuck in the Initial Connection stage. This is corroborated by there being no hits in the app logs if logging is implemented in the app.

This only happens if I try to access the app from Windows using http://localhost against the relevant port (usually 3000 or 8000 depending on the app I’m running). If I access the site using the IP address for WSL2 then the GET requests are never stuck in the pending state.

I have had the issue since the last update - everything was running perfectly against localhost prior to the update.

The common thread is Node’s HTTP library which has a default keep-alive timeout of 5 seconds. I have experienced the issue using Express, Next.js, and Remix - Gatsby was unaffected.

I confirmed this by creating the Hello World Express app, verified the issue still occurred under this simplest of apps, then explicitly set the http server’s keepAliveTimeout setting to 0. The issue went away completely.

To reiterate, the issue only showed up since the last update and was working as expected prior to that allowing localhost to be used without issue.

Diagnostic Logs

No response

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 16
  • Comments: 18 (3 by maintainers)

Most upvoted comments

Release 0.51.3 is in testing and has a fix for this issue.

I’ve started seeing the same issue since yesterday as well, this completely broke my current web dev workflow.

Same here. It’s really breaking productivity these days.

Uninstalling the store version of WSL fixed it for me.

I can also confirm this and that uninstalling the store version fixed the problem. Probably the most interesting thing is that this is only affecting Chromium based browsers (Chrome, Edge). Firefox is working fine.

We’re working on fleshing out some more testcases so this doesn’t regress in the future, thanks for the bug report!

Uninstalling “Windows Subsystem for Linux Preview” and running wsl --update in PS did the trick for me.

Awesome, it really did the trick for me too. Before that I found some workarounds. You can really use firefox or install chrome or edge in WSL and launch them from there. An experience is pretty similar, with some caveats, however, as a temporary solution it’s an option.

Same issue, it works ok only under VPN