diff --git a/hosts/ocaml/bin/sx_server.ml b/hosts/ocaml/bin/sx_server.ml index 4434de68..193394de 100644 --- a/hosts/ocaml/bin/sx_server.ml +++ b/hosts/ocaml/bin/sx_server.ml @@ -2658,10 +2658,30 @@ let http_mode port = Unix.bind sock (Unix.ADDR_INET (Unix.inet_addr_any, port)); Unix.listen sock 1024; Printf.eprintf "[sx-http] Listening on port %d (%d render workers, non-blocking)\n%!" port n_workers; + + (* Auto-restart: check if binary has changed every N requests *) + let binary_path = Sys.executable_name in + let binary_mtime = try (Unix.stat binary_path).Unix.st_mtime with _ -> 0.0 in + let request_count = ref 0 in + let check_interval = 10 in (* check every 10 requests *) + let check_restart () = + incr request_count; + if !request_count mod check_interval = 0 then begin + let current_mtime = try (Unix.stat binary_path).Unix.st_mtime with _ -> 0.0 in + if current_mtime > binary_mtime then begin + Printf.eprintf "[sx-http] Binary changed, restarting...\n%!"; + (* Close listen socket, then exec self *) + Unix.close sock; + Unix.execv binary_path (Array.of_list (Array.to_list Sys.argv)) + end + end + in + (try while true do (* Accept a connection *) let (client, _addr) = Unix.accept sock in + check_restart (); (* Read request — non-blocking: set a short timeout *) Unix.setsockopt_float client Unix.SO_RCVTIMEO 5.0; Unix.setsockopt_float client Unix.SO_SNDTIMEO 10.0;