Skip to content

Commit bccd9f3

Browse files
fix(server): keep connector lifetimes and HTTP handlers untraced
1 parent 2c8bd6b commit bccd9f3

2 files changed

Lines changed: 45 additions & 43 deletions

File tree

‎apps/server/src/cloud/ManagedEndpointRuntime.ts‎

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ export const make = Effect.gen(function* () {
185185
yield* stopConnector(active);
186186
});
187187

188-
const superviseConnector = Effect.fn("ManagedEndpointRuntime.superviseConnector")(
188+
// Lives until connector exit and starts replacements; avoid nested lifetime spans.
189+
const superviseConnector = Effect.fnUntraced(
189190
function* (connector: ActiveConnector) {
190191
const result = yield* Effect.result(connector.child.exitCode);
191192
const activeAtExit = yield* Ref.get(activeRef);
@@ -370,7 +371,8 @@ export const make = Effect.gen(function* () {
370371
yield* Effect.forkIn(retryPinnedRelayClientInstall, runtimeScope);
371372
});
372373

373-
const watchConnectorRegistration = Effect.fn("ManagedEndpointRuntime.watchConnectorRegistration")(
374+
// Registration retries can last for the connector lifetime; keep this loop untraced.
375+
const watchConnectorRegistration = Effect.fnUntraced(
374376
function* (connector: ActiveConnector) {
375377
yield* Effect.sleep(CONNECTOR_REGISTRATION_TIMEOUT);
376378
if (yield* Deferred.isDone(connector.registered)) return true;
@@ -389,44 +391,43 @@ export const make = Effect.gen(function* () {
389391
);
390392

391393
// Only an explicit 503 counts: a failed request says nothing about the
392-
const watchConnectorReadiness = Effect.fn("ManagedEndpointRuntime.watchConnectorReadiness")(
393-
function* (connector: ActiveConnector) {
394-
const readyUrl = `http://${yield* Deferred.await(connector.metricsAddress)}/ready`;
395-
let notReadyChecks = 0;
396-
while (true) {
397-
yield* Effect.sleep(CONNECTOR_READINESS_INTERVAL);
398-
const status = yield* httpClient.get(readyUrl).pipe(
399-
Effect.map((response) => response.status),
400-
Effect.scoped,
401-
Effect.withTracerEnabled(false),
402-
Effect.timeoutOption(CONNECTOR_READINESS_REQUEST_TIMEOUT),
403-
Effect.orElseSucceed(() => Option.none<number>()),
404-
);
405-
if (Option.isNone(status)) continue;
406-
if (status.value === 200) {
407-
notReadyChecks = 0;
408-
yield* Ref.set(notReadyChecksBeforeRestartRef, CONNECTOR_NOT_READY_CHECKS_BEFORE_RESTART);
409-
continue;
410-
}
411-
if (status.value !== 503) continue;
412-
notReadyChecks += 1;
413-
const threshold = yield* Ref.get(notReadyChecksBeforeRestartRef);
414-
if (notReadyChecks < threshold) continue;
394+
// Readiness polling runs for the connector lifetime; keep this loop untraced.
395+
const watchConnectorReadiness = Effect.fnUntraced(function* (connector: ActiveConnector) {
396+
const readyUrl = `http://${yield* Deferred.await(connector.metricsAddress)}/ready`;
397+
let notReadyChecks = 0;
398+
while (true) {
399+
yield* Effect.sleep(CONNECTOR_READINESS_INTERVAL);
400+
const status = yield* httpClient.get(readyUrl).pipe(
401+
Effect.map((response) => response.status),
402+
Effect.scoped,
403+
Effect.withTracerEnabled(false),
404+
Effect.timeoutOption(CONNECTOR_READINESS_REQUEST_TIMEOUT),
405+
Effect.orElseSucceed(() => Option.none<number>()),
406+
);
407+
if (Option.isNone(status)) continue;
408+
if (status.value === 200) {
415409
notReadyChecks = 0;
416-
yield* Ref.set(
417-
notReadyChecksBeforeRestartRef,
418-
Math.min(threshold * 2, CONNECTOR_NOT_READY_CHECKS_MAX),
419-
);
420-
yield* Effect.logWarning("Relay client has no tunnel connection; restarting it", {
421-
pid: Number(connector.child.pid),
422-
notReadyChecks: threshold,
423-
tunnelId: connector.config.tunnelId,
424-
tunnelName: connector.config.tunnelName,
425-
});
426-
yield* connector.child.kill().pipe(Effect.ignore);
410+
yield* Ref.set(notReadyChecksBeforeRestartRef, CONNECTOR_NOT_READY_CHECKS_BEFORE_RESTART);
411+
continue;
427412
}
428-
},
429-
);
413+
if (status.value !== 503) continue;
414+
notReadyChecks += 1;
415+
const threshold = yield* Ref.get(notReadyChecksBeforeRestartRef);
416+
if (notReadyChecks < threshold) continue;
417+
notReadyChecks = 0;
418+
yield* Ref.set(
419+
notReadyChecksBeforeRestartRef,
420+
Math.min(threshold * 2, CONNECTOR_NOT_READY_CHECKS_MAX),
421+
);
422+
yield* Effect.logWarning("Relay client has no tunnel connection; restarting it", {
423+
pid: Number(connector.child.pid),
424+
notReadyChecks: threshold,
425+
tunnelId: connector.config.tunnelId,
426+
tunnelName: connector.config.tunnelName,
427+
});
428+
yield* connector.child.kill().pipe(Effect.ignore);
429+
}
430+
});
430431

431432
const observeConnectorOutput = (connector: ActiveConnector) => {
432433
let rejectedRegistrations = 0;

‎apps/server/src/preview/ServerBrowserStream.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ const parseMessage = (chunk: Uint8Array | string): unknown => {
5151
}
5252
};
5353

54-
const makeHandler = Effect.fn("ServerBrowserStream.makeHandler")(function* (
55-
browser: ServerBrowser.ServerBrowser["Service"],
56-
) {
54+
// Request middleware owns tracing, including long-lived WebSocket sessions.
55+
const makeHandler = Effect.fnUntraced(function* (browser: ServerBrowser.ServerBrowser["Service"]) {
5756
const request = yield* HttpServerRequest.HttpServerRequest;
5857
const url = HttpServerRequest.toURL(request);
5958
if (Option.isNone(url)) return HttpServerResponse.text("Bad Request", { status: 400 });
@@ -221,7 +220,8 @@ const isSandboxError = Schema.is(PreviewBrowserHost.PreviewBrowserSandboxError);
221220
const isLibrariesError = Schema.is(PreviewBrowserHost.PreviewBrowserLibrariesError);
222221
const encodeHostSetup = Schema.encodeSync(Schema.fromJsonString(PreviewStreamHostSetup));
223222

224-
const serveDownload = Effect.fn("ServerBrowserStream.serveDownload")(function* (
223+
// The request span already covers this download route.
224+
const serveDownload = Effect.fnUntraced(function* (
225225
browser: ServerBrowser.ServerBrowser["Service"],
226226
params: URLSearchParams,
227227
) {
@@ -243,7 +243,8 @@ const serveDownload = Effect.fn("ServerBrowserStream.serveDownload")(function* (
243243
const UPLOAD_MAX_FILE_BYTES = 100 * 1024 * 1024;
244244
const UPLOAD_MAX_FILES = 20;
245245

246-
const receiveUpload = Effect.fn("ServerBrowserStream.receiveUpload")(
246+
// The request span already covers this upload route.
247+
const receiveUpload = Effect.fnUntraced(
247248
function* (browser: ServerBrowser.ServerBrowser["Service"], params: URLSearchParams) {
248249
yield* authenticateMediaRequest(AuthPreviewOperateScope);
249250
const request = yield* HttpServerRequest.HttpServerRequest;

0 commit comments

Comments
 (0)