Skip to content

Fix port connection issues on proxy urls#1411

Merged
deep1401 merged 5 commits intomainfrom
fix/port-connection-issues
Feb 27, 2026
Merged

Fix port connection issues on proxy urls#1411
deep1401 merged 5 commits intomainfrom
fix/port-connection-issues

Conversation

@deep1401
Copy link
Copy Markdown
Member

@deep1401 deep1401 commented Feb 26, 2026

  • To test: Start a machine on runpod. Run this build and expose 8338. This should work under this branch but not on main without TL_API_URL set

Summary by CodeRabbit

  • Improvements
    • Enhanced API endpoint URL detection to intelligently handle different deployment environments, including localhost, frontend dev servers, and cloud-hosted instances.
    • Improved connection resilience with automatic fallback mechanism that attempts alternate port connections when primary connection fails.
    • Better handling of port configuration for secure and standard connections.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 26, 2026

📝 Walkthrough

Walkthrough

These changes enhance API URL resolution across the renderer layer. They implement environment-aware fallbacks for API endpoints when TL_API_URL is undefined or default: using localhost:8338, frontend port 1212 → 8338, cloud/hosted origins with port preservation, and a new ConnectionLostModal fallback mechanism for port 8338 recovery attempts.

Changes

Cohort / File(s) Summary
API URL Configuration
src/renderer/App.tsx, src/renderer/components/Login/LoginPage.tsx
Enhanced API URL normalization with environment-aware fallbacks. When TL_API_URL is undefined, "default", or empty, both files now implement logic to select: localhost/127.0.0.1 on port 8338, frontend port 1212 → API on 8338, cloud/hosted origins with port preservation (omitting default ports 80/443), and fallback to frontend origin. Includes trailing-slash normalization for explicit URLs.
Connection Recovery Fallback
src/renderer/components/Shared/ConnectionLostModal.tsx
Added buildPortFallback helper function and fallbackTriedRef to attempt port 8338 fallback on primary healthz check failure. If fallback succeeds, updates TransformerLab.API_URL and current connection, halting further retries. Retains existing localhost and port 8338 behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

mode:multiuser

Suggested reviewers

  • dadmobile
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix port connection issues on proxy urls' directly addresses the main change: enhancing API URL normalization logic across multiple components to handle port fallbacks and proxy URL scenarios. The PR implements intelligent port detection and fallback mechanisms to fix connection issues specifically on proxy URLs.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/port-connection-issues

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/renderer/components/Shared/ConnectionLostModal.tsx (1)

71-78: ⚠️ Potential issue | 🟠 Major

Reset fallbackTriedRef when connection changes.

After one disconnect cycle, fallbackTriedRef.current stays true, so later reconnect attempts skip the 8338 fallback entirely.

💡 Proposed fix
     checkCountRef.current = 0;
     setCheckCount(0);
+    fallbackTriedRef.current = false;
     let interval: ReturnType<typeof setInterval> | null = null;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/renderer/components/Shared/ConnectionLostModal.tsx` around lines 71 - 78,
The useEffect that runs on connection changes resets checkCountRef and
checkCount but does not reset fallbackTriedRef, causing the 8338 fallback to be
skipped on later reconnects; inside the same useEffect body (the one referencing
checkCountRef, setCheckCount, and interval), set fallbackTriedRef.current =
false when a new connection value is observed so the fallback logic can run
again on subsequent disconnect cycles.
🧹 Nitpick comments (1)
src/renderer/App.tsx (1)

211-251: Extract this URL resolver into a shared utility and add unit tests.

This logic is now duplicated across files and is easy to drift. Please centralize it (e.g., resolveApiBaseUrl) and add focused cases for localhost, :1212, Runpod proxy hostnames, default ports, and explicit env URLs.

As per coding guidelines, "Write unit tests for all utility functions and complex hooks in frontend".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/renderer/App.tsx` around lines 211 - 251, Extract the inline URL
resolution logic from App.tsx into a shared utility function named
resolveApiBaseUrl(envUrl?: string, location?: {protocol: string, hostname:
string, port: string}) that returns a normalized base URL with a trailing slash;
inside the function replicate the existing rules (fallbacks for
undefined/'default'/empty TL_API_URL, localhost/127.0.0.1 -> port 8338, frontend
port '1212' -> API 8338, same-origin for default http/https ports, preserving
explicit port otherwise, and ensuring trailing slash), then replace the IIFE in
App.tsx to call resolveApiBaseUrl(process.env.TL_API_URL, window.location). Add
unit tests for resolveApiBaseUrl covering: undefined/null/'default'/empty env,
hostname localhost and 127.0.0.1, frontend port '1212', Runpod/proxy-style
hostnames (ensure same-origin behavior), default http/https ports, explicit env
URL (with and without trailing slash), and edge cases (whitespace in env); keep
tests deterministic by passing a mock location object.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/renderer/App.tsx`:
- Around line 227-243: The URL resolution block in src/renderer/App.tsx (the
code that builds the API base URL using protocol, hostname, and port—the
variables port, hostname, protocol and the isDefaultPort logic) fails for Runpod
proxy hosts like pod-1212.proxy.runpod.net because the browser port is empty;
add a host check (e.g., detect hostnames matching
/^pod-\d+\.proxy\.runpod\.net$/ or containing ".proxy.runpod.net" with a pod-
prefix) and when matched return `${protocol}//${hostname}:8338/` (same routing
as the existing port === '1212' branch), then add unit tests for the hostname
cases (pod-1212.proxy.runpod.net, normal localhost:1212, default-origin hosts)
to cover the new logic and prevent regressions.

In `@src/renderer/components/Shared/ConnectionLostModal.tsx`:
- Around line 101-106: The code currently uses unsafe casts "(window as
any).TransformerLab" when setting TransformerLab.API_URL; remove the casts and
access the typed global directly (use window.TransformerLab) inside the existing
typeof window !== 'undefined' check, updating references to set
window.TransformerLab.API_URL = fallbackBase so the assignment uses the declared
Window/TransformerLab types (no any).
- Around line 42-58: The buildPortFallback function incorrectly appends :8338 as
a port for Runpod hostnames — update its logic to detect Runpod patterns (e.g.,
pod-<id>.proxy.runpod.net) and map port 8338 into the hostname
(pod-8338.proxy.runpod.net) instead of using a port parameter; also ensure
buildPortFallback still returns null for localhost and when port already equals
'8338'. In the component useEffect that triggers fallbacks, reset
fallbackTriedRef.current = false at the start of the effect when the connection
prop changes so a new connection can attempt a fallback. Finally, remove all
(window as any).TransformerLab casts by declaring a proper Window interface
extension (e.g., interface Window { TransformerLab?: ... }) and use
window.TransformerLab with the correct typing instead of any to satisfy
TypeScript guidelines.

---

Outside diff comments:
In `@src/renderer/components/Shared/ConnectionLostModal.tsx`:
- Around line 71-78: The useEffect that runs on connection changes resets
checkCountRef and checkCount but does not reset fallbackTriedRef, causing the
8338 fallback to be skipped on later reconnects; inside the same useEffect body
(the one referencing checkCountRef, setCheckCount, and interval), set
fallbackTriedRef.current = false when a new connection value is observed so the
fallback logic can run again on subsequent disconnect cycles.

---

Nitpick comments:
In `@src/renderer/App.tsx`:
- Around line 211-251: Extract the inline URL resolution logic from App.tsx into
a shared utility function named resolveApiBaseUrl(envUrl?: string, location?:
{protocol: string, hostname: string, port: string}) that returns a normalized
base URL with a trailing slash; inside the function replicate the existing rules
(fallbacks for undefined/'default'/empty TL_API_URL, localhost/127.0.0.1 -> port
8338, frontend port '1212' -> API 8338, same-origin for default http/https
ports, preserving explicit port otherwise, and ensuring trailing slash), then
replace the IIFE in App.tsx to call resolveApiBaseUrl(process.env.TL_API_URL,
window.location). Add unit tests for resolveApiBaseUrl covering:
undefined/null/'default'/empty env, hostname localhost and 127.0.0.1, frontend
port '1212', Runpod/proxy-style hostnames (ensure same-origin behavior), default
http/https ports, explicit env URL (with and without trailing slash), and edge
cases (whitespace in env); keep tests deterministic by passing a mock location
object.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 670f565 and 75c3e23.

📒 Files selected for processing (3)
  • src/renderer/App.tsx
  • src/renderer/components/Login/LoginPage.tsx
  • src/renderer/components/Shared/ConnectionLostModal.tsx

Comment thread src/renderer/App.tsx
Comment thread src/renderer/components/Shared/ConnectionLostModal.tsx Outdated
Comment thread src/renderer/components/Shared/ConnectionLostModal.tsx Outdated
@deep1401 deep1401 merged commit 4bf519c into main Feb 27, 2026
4 checks passed
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