Skip to content

Commit e8ba433

Browse files
committed
fix browser cookies crashing websocket connection
1 parent 8778ed4 commit e8ba433

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

webgpu/jupyter.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,7 @@ def Draw(
486486

487487
def _webgpu_js(server):
488488
js = _link_js_code + """
489-
const __is_vscode = (typeof location !== 'undefined' && location.protocol === 'vscode-webview:');
490-
const __webgpu_host = __is_vscode ? '127.0.0.1' : ((typeof location !== 'undefined' && location.hostname) || '127.0.0.1');
491-
WebsocketLink('ws://' + __webgpu_host + ':{port}?token={token}');
489+
WebsocketLink('ws://127.0.0.1:{port}?token={token}');
492490
""".format(port=server.port, token=server.auth_token)
493491
display(Javascript(js))
494492

webgpu/link/websocket.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import asyncio
22
import json
3+
import os
34
import secrets
45
import threading
56
from concurrent.futures import ThreadPoolExecutor
67
from urllib.parse import parse_qs, urlparse
78

9+
# Browser WebSocket handshakes automatically attach cookies for the target
10+
# (loopback) host, and the JS WebSocket API exposes no way to omit them. On
11+
# machines with large cookies for localhost/127.0.0.1 the resulting Cookie
12+
# header line can exceed websockets' default 8192-byte limit, making the
13+
# handshake fail with SecurityError("line too long"). Raise the limit before
14+
# importing websockets (the value is read at import time of websockets.http11).
15+
# Note: only honored on websockets versions that support this env var.
16+
os.environ.setdefault("WEBSOCKETS_MAX_LINE_LENGTH", "32768")
17+
818
import websockets
919
from websockets.http11 import Response
1020
from websockets.datastructures import Headers

0 commit comments

Comments
 (0)