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);
+1
View File
@@ -17,6 +17,7 @@ void compile_shared_unit(Request* context, SharedUnit* su);
SharedUnit* get_shared_unit(Request* context, String file_name);
String compiler_error_page_unit(Request* context, String config_key);
bool compiler_unit_compile_pending(Request* context, String file_name);
bool compiler_unit_compile_in_progress(Request* context, String file_name);
String compiler_site_directory(Request* context);
StringList compiler_scan_site_units(Request* context);
StringList compiler_list_known_units(Request* context);
+1 -1
View File
@@ -100,7 +100,7 @@ static bool wasm_artifact_exists(Request* context, const String& entry_unit)
// metadata mismatches, which can leave stale wasm with old imports.
bool source_missing = false;
if(compiler_unit_needs_recompile(context, entry_unit, &source_missing))
return(false);
return(compiler_unit_compile_in_progress(context, entry_unit));
if(source_missing)
return(false);
return(true);
+1 -1
View File
@@ -1606,7 +1606,7 @@ private:
return(1);
}
if(!file_exists_host(worker.unit_wasm_path(resolved)) || compiler_unit_needs_recompile(context, resolved, 0))
if(!file_exists_host(worker.unit_wasm_path(resolved)) || (compiler_unit_needs_recompile(context, resolved, 0) && !compiler_unit_compile_in_progress(context, resolved)))
get_shared_unit(context, resolved);
size_t unit_index = 0;