Fix wasm compile source race

This commit is contained in:
udo
2026-07-13 22:47:48 +00:00
parent 36ea0cd2a5
commit 096138d6a1
3 changed files with 103 additions and 17 deletions
+45 -15
View File
@@ -215,16 +215,31 @@ String compiler_unit_build_token()
);
}
String compiler_unit_metadata_text(Request* context, SharedUnit* su)
String compiler_unit_metadata_text(Request* context, SharedUnit* su, String input_signature = "")
{
if(input_signature == "")
input_signature = compiler_unit_input_signature(context, su);
return(
"format=uce-unit-metadata-v1\n"
"unit_abi_version=" + std::to_string(UCE_UNIT_ABI_VERSION) + "\n"
"input_signature=" + compiler_unit_input_signature(context, su) + "\n"
"input_signature=" + input_signature + "\n"
"build_token=" + compiler_unit_build_token() + "\n"
);
}
String compiler_cached_wasm_path(String wasm_path)
{
if(wasm_path.size() >= 5 && wasm_path.rfind(".wasm", wasm_path.size() - 5) == wasm_path.size() - 5)
return(wasm_path.substr(0, wasm_path.size() - 5) + ".cwasm");
return(wasm_path + ".cwasm");
}
void compiler_unlink_unit_wasm_artifacts(SharedUnit* su)
{
file_unlink(su->wasm_name);
file_unlink(compiler_cached_wasm_path(su->wasm_name));
}
String compiler_wasm_compile_script(Request* context)
{
String script = "scripts/compile_wasm_unit";
@@ -832,33 +847,48 @@ void compile_shared_unit(Request* context, SharedUnit* su)
}
shell_exec("mkdir -p " + shell_escape(su->pre_path));
file_put_contents(su->pre_path + "/" + su->pre_file_name, preprocess_shared_unit(context, su));
file_put_contents(su->api_file_name, join(su->api_declarations, "\n"));
String compiled_input_signature;
for(u64 attempt = 0; attempt < 2; attempt++)
{
su->api_declarations.clear();
compiled_input_signature = compiler_unit_input_signature(context, su);
file_put_contents(su->pre_path + "/" + su->pre_file_name, preprocess_shared_unit(context, su));
file_put_contents(su->api_file_name, join(su->api_declarations, "\n"));
su->compiler_messages = trim(shell_exec(shell_escape(compiler_wasm_compile_script(context))+" "+
shell_escape(su->src_path)+" "+
shell_escape(su->bin_path)+" "+
shell_escape(su->file_name)+" "+
shell_escape(su->pre_file_name)+" "+
shell_escape(su->wasm_file_name)
));
su->compiler_messages = trim(shell_exec(shell_escape(compiler_wasm_compile_script(context))+" "+
shell_escape(su->src_path)+" "+
shell_escape(su->bin_path)+" "+
shell_escape(su->file_name)+" "+
shell_escape(su->pre_file_name)+" "+
shell_escape(su->wasm_file_name)
));
if(su->compiler_messages.length() == 0 && !file_exists(su->wasm_name))
su->compiler_messages = "wasm compile script completed without creating " + su->wasm_name;
if(su->compiler_messages.length() == 0 && !file_exists(su->wasm_name))
su->compiler_messages = "wasm compile script completed without creating " + su->wasm_name;
if(su->compiler_messages.length() > 0)
break;
String current_input_signature = compiler_unit_input_signature(context, su);
if(current_input_signature == compiled_input_signature)
break;
compiler_unlink_unit_wasm_artifacts(su);
if(attempt == 1)
su->compiler_messages = "source changed during wasm compile; retry required";
}
if(su->compiler_messages.length() > 0)
{
String raw_messages = su->compiler_messages;
file_put_contents(su->compile_output_file_name, raw_messages + "\n");
file_put_contents(su->wasm_check_file_name, raw_messages + "\n");
file_unlink(su->wasm_name);
compiler_unlink_unit_wasm_artifacts(su);
compiler_record_compile_result(su, time_precise() - comp_start, false, "compile_error", raw_messages);
printf("%s \n", compiler_format_compile_failure(su, raw_messages).c_str());
}
else
{
su->last_compiled = file_mtime(su->wasm_name);
file_put_contents(su->meta_file_name, compiler_unit_metadata_text(context, su));
file_unlink(compiler_cached_wasm_path(su->wasm_name));
file_put_contents(su->meta_file_name, compiler_unit_metadata_text(context, su, compiled_input_signature));
file_unlink(su->compile_output_file_name);
file_unlink(su->wasm_check_file_name);
compiler_record_compile_result(