@@ -157,11 +157,6 @@ fn acquire_counter_permit(
157157 }
158158}
159159
160- fn acquire_compile_permit ( ) -> Result < CounterPermit , WasmExecutionError > {
161- let slots = compile_slots ( ) ;
162- acquire_counter_permit ( slots, MAX_CONCURRENT_COMPILES )
163- }
164-
165160impl FluxWasmRuntime {
166161 pub fn new ( limits : WasmSandboxLimits ) -> Result < Self , WasmExecutionError > {
167162 let limits = limits. validate ( ) ?;
@@ -268,7 +263,16 @@ impl FluxWasmRuntime {
268263 }
269264
270265 fn compile_module ( & self , plugin : & WasmPluginFile ) -> Result < Module , WasmExecutionError > {
271- let compile_permit = acquire_compile_permit ( ) ?;
266+ self . compile_module_with_counter ( plugin, compile_slots ( ) , MAX_CONCURRENT_COMPILES )
267+ }
268+
269+ fn compile_module_with_counter (
270+ & self ,
271+ plugin : & WasmPluginFile ,
272+ counter : & ' static AtomicUsize ,
273+ limit : usize ,
274+ ) -> Result < Module , WasmExecutionError > {
275+ let compile_permit = acquire_counter_permit ( counter, limit) ?;
272276 let engine = self . engine . clone ( ) ;
273277 let bytes = plugin. bytes ( ) . to_vec ( ) ;
274278 let ( result_sender, result_receiver) = mpsc:: sync_channel ( 1 ) ;
@@ -328,6 +332,7 @@ mod tests {
328332
329333 #[ test]
330334 fn compiled_module_execution_does_not_need_compile_slots ( ) {
335+ let counter = Box :: leak ( Box :: new ( AtomicUsize :: new ( 0 ) ) ) ;
331336 let directory = tempfile:: tempdir ( ) . unwrap ( ) ;
332337 let plugin_path = write_wat_plugin (
333338 & directory,
@@ -337,19 +342,24 @@ mod tests {
337342 let plugin =
338343 load_plugin_file ( & plugin_path, & [ directory. path ( ) . to_path_buf ( ) ] , limits) . unwrap ( ) ;
339344 let runtime = FluxWasmRuntime :: new ( limits) . unwrap ( ) ;
340- let module = runtime. compile_plugin_module ( & plugin) . unwrap ( ) ;
341- let permits = ( 0 ..MAX_CONCURRENT_COMPILES )
342- . map ( |_| acquire_compile_permit ( ) . unwrap ( ) )
343- . collect :: < Vec < _ > > ( ) ;
345+ let module = FluxWasmCompiledModule {
346+ module : runtime
347+ . compile_module_with_counter ( & plugin, counter, 1 )
348+ . unwrap ( ) ,
349+ plugin_sha256 : plugin. sha256_hex ( ) . to_owned ( ) ,
350+ } ;
351+ let permit = acquire_counter_permit ( counter, 1 ) . unwrap ( ) ;
344352
345353 let outcome = runtime
346354 . run_compiled_i32_no_args ( & module, "decision" )
347355 . unwrap ( ) ;
348- let error = runtime. run_i32_no_args ( & plugin, "decision" ) . unwrap_err ( ) ;
356+ let error = runtime
357+ . compile_module_with_counter ( & plugin, counter, 1 )
358+ . unwrap_err ( ) ;
349359
350360 assert_eq ! ( outcome. result, 7 ) ;
351361 assert ! ( matches!( error, WasmExecutionError :: CompileConcurrencyLimit ) ) ;
352- drop ( permits ) ;
362+ drop ( permit ) ;
353363 }
354364
355365 #[ test]
0 commit comments