gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

Error `ERR_STREAM_WRITE_AFTER_END` during OAuth login on WSL.

Open TommrraraSnow opened this issue 1 year ago • 8 comments

What happened?

The error occurs during what appears to be the OAuth2 authentication flow on WSL. I opened the OAuth page through VSCode Auto Forwarded. Then the CLI immediately crashed and leave the error below.

What did you expect to happen?

OAuth should be normal to use.

Client information

node:events:496
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at ServerResponse.end (node:_http_outgoing:1098:15)
    at Server.<anonymous> (file:///home/linuxbrew/.linuxbrew/lib/node_modules/@google/gemini-cli/node_modules/@google/gemini-cli-core/dist/src/code_assist/oauth2.js:215:25)
    at Server.emit (node:events:518:28)
    at parserOnIncoming (node:_http_server:1153:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17)
Emitted 'error' event on ServerResponse instance at:
    at emitErrorNt (node:_http_outgoing:928:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:91:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}

Node.js v22.18.0

Login information

No login

Anything else we need to know?

Platform: WSL Ubuntu-22.04, Node.js 22.18 On latest Windows 11

TommrraraSnow avatar Aug 20 '25 03:08 TommrraraSnow

I just want to be sure we have the details clear on this one. You said "I opened the OAuth page through VSCode Auto Forwarded." - can you explain that in a bit more detail, just so we're sure we can reproduce?

Also, to be clear, you're only seeing this when running on WSL?

bdmorgan avatar Aug 20 '25 13:08 bdmorgan

I connected via remote to WSL in VSCode, then launched gemini in the VSCode terminal. However, unlike when I run it directly in native Windows, no browser popped up. After some exploration, I found the port from gemini that VSCode automatically forwarded. But as soon as I opened it, gemini crashed and the above error occurred.

However, I just used the Windows WSL terminal, and it was able to normally pop up a browser to open the login page. This is probably a redirect issue in the VSCode terminal. In any case, the problem can be solved by not logging in through the VSCode terminal.

And I only tried on Windows, WSL and native Ubuntu with GUI. This issue only occured when connecting via VSCode remote to WSL.

TommrraraSnow avatar Aug 21 '25 01:08 TommrraraSnow

I think the key problem is that if the browser didn't pop up automatically in some way, the user cannot login to Google account at all. Maybe add a link to click or copy, add a QR code to scan, or even add an input way to manually type token.

TommrraraSnow avatar Aug 21 '25 03:08 TommrraraSnow

I'm experiencing the same OAuth issue described here.

Environment

  • macOS (Apple Silicon)
  • Node.js v25.1.0 (installed via Homebrew)
  • gemini-cli 0.11.3 (/opt/homebrew/Cellar/gemini-cli/0.11.3/libexec/lib/node_modules/@google/gemini-cli)
  • Installed with Homebrew (brew install gemini-cli)

Steps to reproduce

  1. Run gemini or gemini whoami
  2. CLI opens the Google OAuth URL (e.g. http://localhost:57205/oauth2callback?...)
  3. Complete Google sign-in successfully in the browser
  4. Browser redirects to http://localhost:57205/oauth2callback?..., but the page stays blank
  5. CLI terminal hangs at “Waiting for auth...” and then crashes with:
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at ServerResponse.end (node:_http_outgoing:1042:15)
    at Server.<anonymous> (.../node_modules/@google/gemini-cli-core/dist/src/code_assist/oauth2.js:238:25)
    at Server.emit (node:events:508:28)
...
Node.js v25.1.0

Notes

  • The local redirect URL is correct and accessible; the crash happens right after the OAuth redirect returns.
  • This looks like the callback handler tries to write to a closed stream (ERR_STREAM_WRITE_AFTER_END).
  • The CLI stays stuck in “Waiting for auth...” until it crashes.
  • Downgrading to Node 22 LTS or using the GEMINI_API_KEY flow works around the problem.

Workaround Using an API key from Google AI Studio or running with Node 22 fixes the issue.

It seems the local OAuth server logic in @google/gemini-cli-core/dist/src/code_assist/oauth2.js isn’t compatible with Node 25.

alexgarciab avatar Nov 03 '25 18:11 alexgarciab

Hello! As part of our effort to keep our backlog manageable and focus on the most active issues, we are tidying up older reports.

It looks like this issue hasn't been active for a while, so we are closing it for now. However, if you are still experiencing this bug on the latest stable build, please feel free to comment on this issue or create a new one with updated details.

Thank you for your contribution!

gemini-cli[bot] avatar Dec 03 '25 22:12 gemini-cli[bot]

Found possible duplicate issues:

  • #2515

If you believe this is not a duplicate, please remove the status/possible-duplicate label.

gemini-cli[bot] avatar Dec 04 '25 19:12 gemini-cli[bot]

This issue describes an unhandled promise rejection crashing the CLI during the OAuth login flow, which is a high-priority bug.

Effort Analysis: The error ERR_STREAM_WRITE_AFTER_END occurs in packages/core/src/code_assist/oauth2.ts during local HTTP server callbacks when authenticating. Port forwarders (like VS Code on WSL) can send multiple background requests (e.g., favicon.ico). If the server rejects the promise on an unexpected route, it attempts to respond again after res.end() has already been called on the response stream, throwing an unhandled socket error. The fix requires a localized error handling adjustment in oauth2.ts to properly manage the HTTP response lifecycle and prevent writing to a closed stream. According to the guidelines, this localized bug fix and straightforward promise rejection maps to effort/small.

gemini-cli[bot] avatar May 09 '26 07:05 gemini-cli[bot]

Please reopen if still happening with latest gemini-cli version

cocosheng-g avatar May 13 '26 16:05 cocosheng-g