Add server timeout configuration and improve error handling for script save operations

- Configure HTTP server timeouts (requestTimeout: 5min, headersTimeout: 2min, keepAliveTimeout: 65s)
- Add better error logging in PUT /scripts endpoint
- Improve writeFileWithLock error handling with descriptive messages and proper cleanup
- Ensure lock is always released even on error

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-12-27 08:43:11 +00:00
parent 31261190f0
commit 82514e65e1
3 changed files with 54 additions and 16 deletions

View File

@ -206,6 +206,7 @@ export default (app: Router) => {
}), }),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try { try {
let { filename, content, path } = req.body as { let { filename, content, path } = req.body as {
filename: string; filename: string;
@ -223,6 +224,7 @@ export default (app: Router) => {
await writeFileWithLock(filePath, content); await writeFileWithLock(filePath, content);
return res.send({ code: 200 }); return res.send({ code: 200 });
} catch (e) { } catch (e) {
logger.error('🔥 error saving script: %o', e);
return next(e); return next(e);
} }
}, },

View File

@ -16,6 +16,17 @@ export class HttpServerService {
metricsService.record('http_service_start', 1, { metricsService.record('http_service_start', 1, {
port: port.toString(), port: port.toString(),
}); });
// Set server timeouts to prevent premature connection drops
if (this.server) {
// Timeout for receiving the entire request (including body) - 5 minutes
this.server.requestTimeout = 300000;
// Timeout for headers - 2 minutes
this.server.headersTimeout = 120000;
// Keep-alive timeout - 65 seconds (slightly more than typical load balancer timeout)
this.server.keepAliveTimeout = 65000;
}
resolve(this.server); resolve(this.server);
}); });

View File

@ -3,6 +3,7 @@ import os from 'os';
import path from 'path'; import path from 'path';
import { writeFile, open, chmod } from 'fs/promises'; import { writeFile, open, chmod } from 'fs/promises';
import { fileExist } from '../config/util'; import { fileExist } from '../config/util';
import Logger from '../loaders/logger';
function getUniqueLockPath(filePath: string) { function getUniqueLockPath(filePath: string) {
const sanitizedPath = filePath const sanitizedPath = filePath
@ -19,13 +20,21 @@ export async function writeFileWithLock(
if (typeof options === 'string') { if (typeof options === 'string') {
options = { encoding: options }; options = { encoding: options };
} }
try {
if (!(await fileExist(filePath))) { if (!(await fileExist(filePath))) {
const fileHandle = await open(filePath, 'w'); const fileHandle = await open(filePath, 'w');
fileHandle.close(); await fileHandle.close();
}
} catch (error) {
throw new Error(`Failed to create file ${filePath}: ${error}`);
} }
const lockfilePath = getUniqueLockPath(filePath);
const release = await lock(filePath, { const lockfilePath = getUniqueLockPath(filePath);
let release: (() => Promise<void>) | null = null;
try {
release = await lock(filePath, {
retries: { retries: {
retries: 10, retries: 10,
factor: 2, factor: 2,
@ -34,9 +43,25 @@ export async function writeFileWithLock(
}, },
lockfilePath, lockfilePath,
}); });
} catch (error) {
throw new Error(`Failed to acquire lock for ${filePath}: ${error}`);
}
try {
await writeFile(filePath, content, { encoding: 'utf8', ...options }); await writeFile(filePath, content, { encoding: 'utf8', ...options });
if (options?.mode) { if (options?.mode) {
await chmod(filePath, options.mode); await chmod(filePath, options.mode);
} }
} catch (error) {
throw new Error(`Failed to write to file ${filePath}: ${error}`);
} finally {
if (release) {
try {
await release(); await release();
} catch (error) {
// Log but don't throw on release failure
Logger.error(`Failed to release lock for ${filePath}:`, error);
}
}
}
} }