avoid request stalls during proactive rebuilds

This commit is contained in:
udo
2026-07-13 12:12:14 +00:00
parent c1039f5094
commit 5195ebeb25
7 changed files with 80 additions and 12 deletions
+31 -3
View File
@@ -296,7 +296,7 @@ String compiler_registry_lock_file_name(Request* context)
return(compiler_registry_file_name(context) + ".lock");
}
int compiler_open_lock_file(String file_name, String purpose)
int compiler_open_lock_file(String file_name, String purpose, bool nonblocking = false)
{
(void)purpose;
auto lock_dir = dirname(file_name);
@@ -309,8 +309,13 @@ int compiler_open_lock_file(String file_name, String purpose)
return(fdlock);
}
fcntl(fdlock, F_SETFD, FD_CLOEXEC);
if(flock(fdlock, LOCK_EX) != 0)
if(flock(fdlock, LOCK_EX | (nonblocking ? LOCK_NB : 0)) != 0)
{
if(nonblocking && (errno == EWOULDBLOCK || errno == EAGAIN))
{
close(fdlock);
return(-2);
}
close(fdlock);
printf("(!) Could not lock file %s\n", file_name.c_str());
return(-1);
@@ -845,7 +850,19 @@ SharedUnit* compiler_get_shared_unit_internal(Request* context, String file_name
SharedUnit* su = new SharedUnit();
setup_unit_paths(context, su, file_name);
int fdlock = compiler_open_lock_file(su->wasm_name + ".lock", "shared-unit:" + file_name);
int fdlock = compiler_open_lock_file(su->wasm_name + ".lock", "shared-unit:" + file_name, !force_recompile);
if(fdlock == -2 && file_exists(su->wasm_name))
{
auto state = inspect_shared_unit_filesystem(context, su);
su->api_declarations = split(file_get_contents(su->api_file_name), "\n");
su->last_compiled = state.compiled_time;
su->compile_status = "rebuilding";
compiler_record_observed_filesystem_state(su, state);
context->server->units[file_name] = su;
return(su);
}
if(fdlock == -2)
fdlock = compiler_open_lock_file(su->wasm_name + ".lock", "shared-unit:" + file_name);
if(fdlock == -1)
{
su->compiler_messages = "could not open compile lock";
@@ -947,6 +964,17 @@ bool compiler_unit_compile_pending(Request* context, String file_name)
return(true);
}
bool compiler_unit_compile_in_progress(Request* context, String file_name)
{
SharedUnit su;
setup_unit_paths(context, &su, compiler_normalize_unit_path(context, file_name));
int fdlock = compiler_open_lock_file(su.wasm_name + ".lock", "compile-probe", true);
if(fdlock == -2)
return(true);
compiler_close_lock_file(fdlock);
return(false);
}
void unit_render(String file_name)
{
unit_render(file_name, *context);