Skip to content

Commit d525f21

Browse files
authored
Merge pull request #39 from itsyogesh/fix/pvm-compile-timeout
Fix PVM compilation timeout and handle non-JSON error responses
2 parents ef36a44 + cd5ec2b commit d525f21

3 files changed

Lines changed: 43 additions & 8 deletions

File tree

app/api/compile/route.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { resolveAllImports, resolveAllImportsSources } from "@/lib/import-resolv
55
import { checkRateLimit, getRateLimitReset } from "@/lib/rate-limiter";
66

77
export const runtime = "nodejs";
8-
export const maxDuration = 60;
8+
export const maxDuration = 120;
99

1010
const MAX_BODY_SIZE = 512000; // 500KB
11-
const WORKER_TIMEOUT_MS = 30_000;
11+
const WORKER_TIMEOUT_EVM_MS = 30_000;
12+
const WORKER_TIMEOUT_PVM_MS = 80_000; // resolc is slower; 20s resolver + 80s compile = 100s < 120s maxDuration
1213

1314
interface CompileRequest {
1415
source?: string;
@@ -28,17 +29,18 @@ interface CompileResponse {
2829
function compileInWorker(
2930
input: string,
3031
mode: string,
31-
timeoutMs = WORKER_TIMEOUT_MS
32+
timeoutMs?: number
3233
): Promise<any> {
34+
const timeout = timeoutMs ?? (mode === "pvm" ? WORKER_TIMEOUT_PVM_MS : WORKER_TIMEOUT_EVM_MS);
3335
return new Promise((resolve, reject) => {
3436
const worker = new Worker(COMPILE_WORKER_CODE, {
3537
eval: true,
3638
workerData: { mode, input },
3739
});
3840
const timer = setTimeout(() => {
3941
worker.terminate();
40-
reject(new Error("Compilation timed out after 30s"));
41-
}, timeoutMs);
42+
reject(new Error(`Compilation timed out after ${Math.round(timeout / 1000)}s`));
43+
}, timeout);
4244

4345
worker.on("message", (msg) => {
4446
clearTimeout(timer);

lib/compile-client.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@ export async function compileSolidity(
2222
body: JSON.stringify({ source, mode }),
2323
});
2424

25-
const data = await res.json();
25+
let data: any;
26+
try {
27+
data = await res.json();
28+
} catch {
29+
// Server returned non-JSON (e.g., HTML 502/504 gateway timeout)
30+
return {
31+
success: false,
32+
contracts: null,
33+
contractNames: [],
34+
errors: [
35+
{
36+
message: `Server error (HTTP ${res.status}). PVM compilation may need more time — try again or use a simpler contract.`,
37+
severity: "error",
38+
},
39+
],
40+
warnings: [],
41+
};
42+
}
2643

2744
if (!res.ok) {
2845
return {
@@ -91,7 +108,23 @@ export async function compileSoliditySources(
91108
body,
92109
});
93110

94-
const data = await res.json();
111+
let data: any;
112+
try {
113+
data = await res.json();
114+
} catch {
115+
return {
116+
success: false,
117+
contracts: null,
118+
contractNames: [],
119+
errors: [
120+
{
121+
message: `Server error (HTTP ${res.status}). PVM compilation may need more time — try again or use a simpler contract.`,
122+
severity: "error",
123+
},
124+
],
125+
warnings: [],
126+
};
127+
}
95128

96129
if (!res.ok) {
97130
return {

lib/import-resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const PER_FETCH_TIMEOUT_MS = 10_000;
1515
const MAX_FILE_SIZE = 500_000; // 500KB per file
1616
const MAX_FILES = 50;
1717
const MAX_ITERATIONS = 10;
18-
const TOTAL_BUDGET_MS = 30_000;
18+
const TOTAL_BUDGET_MS = 20_000; // 20s — leaves headroom within the 120s function limit
1919

2020
interface ResolvedSources {
2121
sources: Record<string, { content: string }>;

0 commit comments

Comments
 (0)