set_time_limit
set_time_limit(int $seconds): bool
Resets the script's maximum execution time in seconds (the counter restarts at the call); 0 means unlimited; on timeout the script is terminated with a fatal error.
C C23
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
static void on_alarm(int sig) { (void)sig; /* a real app would abort or longjmp */ }
int main(void) {
/* POSIX alarm(2) is the direct ancestor: SIGALRM after N seconds */
signal(SIGALRM, on_alarm);
unsigned int previous = alarm(120);
printf("watchdog armed, previous: %u\n", previous);
unsigned int cancelled = alarm(0); /* cancel; returns the time left */
printf("watchdog disarmed, was: %u\n", cancelled);
return 0;
}
watchdog armed, previous: 0 watchdog disarmed, was: 120
C++ C++26
#include <csignal>
#include <cstdio>
#include <unistd.h>
static void onAlarm(int) { /* a real app would abort or throw */ }
int main() {
// POSIX alarm(2) again - C++ adds no language-level limit either
std::signal(SIGALRM, onAlarm);
unsigned int previous = alarm(120);
std::printf("watchdog armed, previous: %u\n", previous);
unsigned int cancelled = alarm(0);
std::printf("watchdog disarmed, was: %u\n", cancelled);
return 0;
}
watchdog armed, previous: 0 watchdog disarmed, was: 120
C# v11.0.100
// C#: no script limit; CancellationTokenSource is the idiomatic deadline
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
try {
await Task.Delay(10, cts.Token);
Console.WriteLine("work finished within deadline");
} catch (TaskCanceledException) {
Console.WriteLine("cancelled: timeout");
}
work finished within deadline
Go v1.27.4
package main
import (
"context"
"fmt"
"time"
)
func main() {
// Go: context.WithTimeout is the idiomatic deadline
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
select {
case <-time.After(10 * time.Millisecond):
fmt.Println("work finished within deadline")
case <-ctx.Done():
fmt.Println("cancelled:", ctx.Err())
}
}
work finished within deadline
Java v27.0.1
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) throws Exception {
// Java: no script limit; a Future with get(timeout) bounds one task
ExecutorService pool = Executors.newSingleThreadExecutor();
Future<String> f = pool.submit(() -> {
Thread.sleep(10);
return "work finished within deadline";
});
try {
System.out.println(f.get(100, TimeUnit.MILLISECONDS));
} catch (TimeoutException e) {
System.out.println("cancelled: timeout");
} finally {
pool.shutdownNow();
}
}
}
work finished within deadline
JavaScript v26.13.1
// JS has no engine-level script limit: a manual watchdog with Promise.race
let timer;
const watchdog = new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error("timeout")), 1000);
});
const work = Promise.resolve("work finished within deadline").then((v) => {
clearTimeout(timer);
return v;
});
Promise.race([work, watchdog]).then((v) => console.log(v)).catch((e) => console.log(e.message));
work finished within deadline
PHP v8.6.0
// set_time_limit caps this script's own execution time; 0 means unlimited
var_dump(set_time_limit(120));
echo ini_get("max_execution_time"), "\n"; // reads back what was set
set_time_limit(0);
echo ini_get("max_execution_time") === "0" ? "unlimited\n" : "still capped\n";
bool(true) 120 unlimited
Python v3.15.2
# Python: no script-wide limit; signal.alarm is the classic Unix watchdog
import signal
signal.alarm(120) # SIGALRM would fire in 120s (Unix only)
print("watchdog armed for 120s")
signal.alarm(0) # disarm
print("watchdog disarmed")
watchdog armed for 120s watchdog disarmed
Rust v1.99.1
fn main() {
// Rust std has no timeout; async runtimes like tokio provide time::timeout.
// Std-only stand-in: a deadline checked by hand.
let deadline = std::time::Instant::now() + std::time::Duration::from_millis(100);
std::thread::sleep(std::time::Duration::from_millis(10)); // the "work"
if std::time::Instant::now() < deadline {
println!("work finished within deadline");
} else {
println!("overrun");
}
}
work finished within deadline
TypeScript v26.13.1
// TS: same as JS - timeouts are hand-rolled around Promises
let timer: ReturnType<typeof setTimeout>;
const watchdog: Promise<never> = new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error("timeout")), 1000);
});
const work: Promise<string> = Promise.resolve("work finished within deadline").then((v: string) => {
clearTimeout(timer);
return v;
});
Promise.race([work, watchdog]).then((v: string) => console.log(v)).catch((e: Error) => console.log(e.message));
work finished within deadline
Language differences
C: alarm(2) is the ancestor - SIGALRM after N seconds; alarm(0) cancels and returns the seconds left.
C++: same as C - no language-level time limit either.
C#: CancellationTokenSource is the idiomatic deadline; Task.Delay(token) cooperates with cancellation.
Go: context.WithTimeout attaches a deadline to the call chain and the cancel signal propagates with ctx.
Java: no script limit; Future.get(timeout) bounds a single task (as in the demo).
JavaScript: NO engine-level script limit; watchdogs are hand-rolled around Promises, or a worker gets terminated.
PHP: only the script's own execution time counts - sleep/system calls do not (except on Windows, which counts real time); equivalent to ini_set('max_execution_time'); each call restarts the counter; the CLI defaults to 0 (unlimited).
Python: no built-in script limit; on Unix, signal.alarm + SIGALRM is the classic watchdog (main thread only), or the third-party func_timeout.
Rust: no timeout in std; tokio's time::timeout is the async-world answer.
TypeScript: same as JavaScript.