Skip to content

fix(desktop): restore TCP loopback serve to unbreak WebSocket - #35673

Closed
divybot wants to merge 1 commit into
mainfrom
orch/divybot-745
Closed

divybot wants to merge 1 commit into
mainfrom
orch/divybot-745

Conversation

@divybot

@divybot divybot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Summary

`deno desktop` moved to an in-process memory channel + `app://` scheme handler in #35272. That broke every desktop app whose frontend connects back with `new WebSocket("ws://" + window.location.host)` — the standard, near-universal pattern for real-time frontend↔backend comms — reported in #35576.

Under the new architecture the webview is at `app://desktop/`, so:

  • `window.location.host` is `desktop`; the URL becomes `ws://desktop` and the browser fails DNS with `ERR_NAME_NOT_RESOLVED`.
  • The `app://` scheme handler only intercepts HTTP; `ws://` is never routed through it.
  • Even if it were, laufey's `SchemeExchange` is one-shot request/response (`read_body` → `begin` → `write` → `finish`) with no upgrade or duplex; a WebSocket handshake could never complete over it.

The same one-shot surface also blocks streamed response bodies (see #35590).

Fix

Restore the previous TCP-loopback serve path. `deno desktop` allocates a loopback port, `Deno.serve` binds a real TCP listener, and the webview navigates to `http://127.0.0.1:PORT\` once a real HTTP response comes back. Native WebSocket and streamed bodies work again with no user-facing change.

This is effectively a revert of #35272 with the window-reveal fix from #35620 preserved (hidden bootstrap window, `on_page_load` reveal, and the idempotent `show()` fallback are kept on top of the restored TCP navigate loop).

Follow-up

Recovering the memory-transport perf win while supporting WebSockets requires a laufey API extension — either a duplex/upgradeable `SchemeExchange` or a scheme-level hook that hands Deno a raw bidirectional stream. That work belongs upstream in `littledivy/laufey`; once shipped, the in-memory serve path can return behind that new API.

Alternate approach

@littledivy opened #35670 as a straight `git revert` of #35272 with the same effect. This PR is the same tree change with a `fix(desktop):` framing and a description that spells out the two failure modes (`app://` not intercepting `ws://`, and `SchemeExchange` being non-duplex). Reviewers can pick whichever framing lands better; closing one when the other merges is fine.

Verification

  • Local `cargo check -p deno_net -p deno_http -p denort_desktop` passes.
  • Only `cli/rt_desktop/lib.rs` needed manual conflict resolution — the fix(rt_desktop): reveal initial window on first paint (startup black flash) #35620 window-reveal fix landed on top of the memory-channel navigate loop; the restored loop keeps that fix (poll TCP with a real HTTP GET → set `server_ready` → `navigate(&url)`; `on_page_load` reveal + idempotent `show()` fallback preserved).

Refs #35576
Refs #35590
Closes denoland/divybot#745

`deno desktop` moved to an in-process memory channel + `app://` scheme
handler in #35272. That broke every desktop app whose frontend connects
back with `new WebSocket("ws://" + window.location.host)` — the standard
pattern for real-time frontend<->backend comms:

- `window.location.host` under `app://desktop/` is `desktop`, so the URL
  becomes `ws://desktop` and the browser fails DNS with
  `ERR_NAME_NOT_RESOLVED`.
- The `app://` scheme handler only intercepts HTTP; it does not see
  `ws://` at all.
- Even if it did, laufey's `SchemeExchange` is one-shot request/response
  (`read_body` -> `begin` -> `write` -> `finish`) with no upgrade or
  duplex — the WebSocket handshake could never complete over it.

The same one-shot surface also blocks streamed response bodies (see
#35590).

Fix: restore the previous TCP-loopback serve path. `deno desktop`
allocates a loopback port, `Deno.serve` binds a real TCP listener, and
the webview navigates to `http://127.0.0.1:PORT` once a real HTTP
response comes back. Native WebSocket (and streamed bodies) work again
without any user-facing change.

This is effectively a revert of #35272 with the window-reveal fix from
#35620 preserved (hidden bootstrap window, `on_page_load` reveal, and
the idempotent `show()` fallback are kept on top of the restored
TCP navigate loop).

A follow-up that keeps the memory transport perf win while supporting
WebSockets would need a laufey API extension (duplex/upgradeable scheme
exchange, or a scheme-level hook that hands Deno a raw bidirectional
stream); that work belongs upstream in laufey.

Refs #35576
Refs #35590
Closes denoland/divybot#745

Co-Authored-By: Divy Srivastava <me@littledivy.com>
@littledivy

Copy link
Copy Markdown
Member

@divybot I will land my revert for the release. I want you to investigate the root cause and fix it with in-memory transport for a reland

@divybot

divybot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Ack @littledivy — closing this in favor of #35670 for the release. Will open a separate reland-with-in-memory-transport PR against the root cause once #35670 lands.

Preliminary root-cause note so the follow-up work has a place to start:

What's broken: new WebSocket("ws://" + window.location.host) under app://desktop/ yields ws://desktop. That URL never reaches the laufey scheme handler at all — modern webviews (CEF, WebView2, WebKit) fetch WebSockets through their own network stack, and neither app nor any other custom scheme registration hooks the WebSocket path. Even if it did, laufey::SchemeExchange today is strictly one-shot HTTP (read_body → begin → write → finish); it has no upgrade/duplex primitive that could carry a WebSocket handshake and the subsequent bidirectional stream.

Reland shape (to be prototyped):

  1. littledivy/laufey extensions:
    • SchemeExchange::upgrade() -> impl AsyncRead + AsyncWrite (or a sibling register_upgrading_scheme_handler) so a 101 Switching Protocols response can hand back the raw socket. Requires per-backend impl:
      • CEF: CefResourceHandler → CefResponseFilter doesn't upgrade; likely needs a separate CefRegisterSchemeHandlerFactory variant using CefWebSocketHandler-style plumbing, or route the ws stream through a CefRequestContext handler.
      • WebView2: no public API for WebSocket interception exists today, so this backend is likely unsolvable without a ws://desktop → ws://127.0.0.1:<port> shim on the JS side.
      • webkit2gtk / wry: TBD; needs research.
    • Window::add_init_script(script) (WKUserScript-style pre-navigation injection) so we can shim window.WebSocket on backends where hijacking ws:// isn't feasible. This is likely needed for WebView2 as a fallback path.
  2. deno side:
    • Keep the memory: transport in ext/net and the DENO_SERVE_ADDRESS=memory:<name> override.
    • cli/rt_desktop/scheme_bridge.rs: on Upgrade: websocket, drive the laufey exchange through upgrade(), wire the raw stream into Deno.serve's in-memory accepter, and let hyper's upgrade handler run over it.
    • On WebView2 (and any backend without native ws hijack): additionally bind a scoped TCP loopback for WebSocket only, and use add_init_script to rewrite ws://desktop → ws://127.0.0.1:<loopback>. The perf win is partially degraded on that backend but user code still works transparently.

Draft PR to follow — this comment is for the record, not the fix itself.

@divybot

divybot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #35670 for the release; separate reland PR to follow per @littledivy's ask above.

@divybot

divybot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Opened the reland as #35675 — keeps the memory transport for HTTP and adds a WebSocket-only TCP loopback proxy that reject-401s anything but a handshake, so plain HTTP still has to come through app://. Webview navigates to app://127.0.0.1:<port>/ so window.location.host is the loopback address user code can ws://-concat with. Draft; local build + memory-transport + new proxy unit tests all green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants