Skip to content

Commit 193be3b

Browse files
committed
Better error handling in the registry proxy and catch uncaught exceptions and unhandled promise rejections instead of crashing the server
1 parent 3579a39 commit 193be3b

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

apps/webapp/app/entry.server.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import {
1414
OperatingSystemContextProvider,
1515
OperatingSystemPlatform,
1616
} from "./components/primitives/OperatingSystemProvider";
17-
import { env } from "./env.server";
1817
import { getSharedSqsEventConsumer } from "./services/events/sqsEventConsumer";
1918
import { singleton } from "./utils/singleton";
19+
import { logger } from "./services/logger.server";
2020

2121
const ABORT_DELAY = 30000;
2222

@@ -178,7 +178,15 @@ function logError(error: unknown, request?: Request) {
178178

179179
const sqsEventConsumer = singleton("sqsEventConsumer", getSharedSqsEventConsumer);
180180

181-
export { wss } from "./v3/handleWebsockets.server";
181+
export { apiRateLimiter } from "./services/apiRateLimit.server";
182182
export { socketIo } from "./v3/handleSocketIo.server";
183+
export { wss } from "./v3/handleWebsockets.server";
183184
export { registryProxy } from "./v3/registryProxy.server";
184-
export { apiRateLimiter } from "./services/apiRateLimit.server";
185+
186+
process.on("uncaughtException", (error, origin) => {
187+
logger.error("Uncaught Exception", { error, origin });
188+
});
189+
190+
process.on("unhandledRejection", (reason, promise) => {
191+
logger.error("Unhandled Rejection", { reason });
192+
});

apps/webapp/app/v3/registryProxy.server.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ export class RegistryProxy {
259259
proxyRes.pipe(response, { end: true });
260260
});
261261

262+
request.on("close", () => {
263+
logger.debug("Client closed the connection");
264+
proxyReq.destroy();
265+
cleanupTempFile();
266+
});
267+
268+
request.on("abort", () => {
269+
logger.debug("Client aborted the connection");
270+
proxyReq.destroy(); // Abort the proxied request
271+
cleanupTempFile(); // Clean up the temporary file if necessary
272+
});
273+
262274
if (tempFilePath) {
263275
const readStream = createReadStream(tempFilePath);
264276

@@ -427,14 +439,22 @@ function initializeProxy() {
427439
});
428440
}
429441

430-
async function streamRequestBodyToTempFile(request: IncomingMessage): Promise<string> {
431-
const tempDir = await mkdtemp(`${tmpdir()}/`);
432-
const tempFilePath = `${tempDir}/requestBody.tmp`;
433-
const writeStream = createWriteStream(tempFilePath);
442+
async function streamRequestBodyToTempFile(request: IncomingMessage): Promise<string | undefined> {
443+
try {
444+
const tempDir = await mkdtemp(`${tmpdir()}/`);
445+
const tempFilePath = `${tempDir}/requestBody.tmp`;
446+
const writeStream = createWriteStream(tempFilePath);
434447

435-
await pipeline(request, writeStream);
448+
await pipeline(request, writeStream);
436449

437-
return tempFilePath;
450+
return tempFilePath;
451+
} catch (error) {
452+
logger.error("Failed to stream request body to temp file", {
453+
error: error instanceof Error ? error.message : error,
454+
});
455+
456+
return;
457+
}
438458
}
439459

440460
type DockerImageParts = {

0 commit comments

Comments
 (0)