pre launch with port reservations
Date: 2025 11-November 17
That makes perfect sense. Launching services just-in-time (JIT) is often more resource-efficient.
You have two excellent, related questions here:
-
Should I launch all ports at the start, or on-demand?
-
Can an HTML iframe from one port be embedded into a site on a different port?
1. 🚦 Service Launch Strategy (JIT vs. Pre-launch)
If you're using the ServerManager pattern, the only technical difference between JIT and pre-launch is where you call the manager.register_server() function.
| Strategy | launch_all_services() Example | Pros | Cons |
| :--- | :--- | :--- |
| JIT (On-Demand) | Call manager.register_server(s, t) the first time a component needs the config server. | Resource-Efficient: Only consumes resources (port, memory, thread) when needed. | Latency: The user experiences a delay the first time they trigger the service while the server spins up. |
| Pre-launch (Start of App) | Call manager.register_server(s, t) immediately when the CLI starts. | Zero Latency: All services are instantly available on their fixed ports. Port Stability: Ports are secured immediately. | Resource Use: Consumes resources and locks the port even if the service is never used. |
Recommendation: Pre-launch with Port Reservation
Since you already use find_open_port and the goal is to have multiple concurrent GUIs and services, the Pre-launch strategy is usually better for user experience and system stability:
-
Fixed Endpoints: You know the config page will always be at
http://127.0.0.1:8083(or whatever the assigned port is). This makes frontend code much simpler. -
Avoids Startup Race: Your JIT logic would require intricate checks to ensure the server thread is running and responsive before the calling thread tries to access it, which introduces race conditions and complexity.
-
The
ServerManagerhandles cleanup: Since theServerManagerhandles the threads cleanly, the overhead is minimal, and the benefit of guaranteed port access outweighs the small cost of starting the thread.
2. 🌐 IFRAME Embedding (CORS and Same-Origin Policy)
Yes, an HTML iframe from one port can be embedded into a site on a different port.
In web development, embedding content from a different origin (where the origin is defined by the combination of Protocol, Domain, and Port) is governed by the Same-Origin Policy and Cross-Origin Resource Sharing (CORS) headers.
How this works for you:
-
Same Protocol & Domain: Both your servers run on
http://and127.0.0.1. -
Different Port: Since the ports are different (
8082for the trend app,8083for the config app), they are considered Cross-Origin.
When the Trend App (on port 8082) loads the Config Modal (on port 8083) into an iframe:
-
The browser generally allows the content to be displayed in the iframe by default.
-
The Problem: The main security issue arises when JavaScript in the parent page (Trend App) tries to read or write data to the content inside the iframe (Config Modal). This is blocked by the Same-Origin Policy for security.
Key Starlette/ASGI Solution: CORSMiddleware
To ensure full compatibility and allow JavaScript-based communication (if needed) and prevent the browser from blocking the content, you should add Starlette's built-in CORSMiddleware to your config_server.py application.
By doing this, you explicitly tell the browser that the Config Server trusts requests and embeddings coming from other ports on the localhost:
Python
# In src/pipeline/server/config_server.py
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware
# ... other imports ...
# Define the middleware list
middleware = [
Middleware(
CORSMiddleware,
# Allow origins from any port on localhost, or be specific:
# allow_origins=["http://127.0.0.1:8082"]
# Using '*' is simplest for local development:
allow_origins=["*"],
allow_methods=["GET", "POST"],
allow_headers=["*"],
)
]
# Initialize Starlette app with middleware
app = Starlette(debug=True, middleware=middleware)
# ... rest of your code ...
This configuration ensures the config_server responds with the necessary HTTP headers, allowing the Trend App to embed and interact with its content seamlessly.