@@ -447,6 +447,35 @@ void WasmBase::finishShutdown() {
447447 }
448448}
449449
450+ bool WasmHandleBase::canary (const std::shared_ptr<PluginBase> &plugin,
451+ const WasmHandleCloneFactory &clone_factory) {
452+ if (this ->wasm () == nullptr ) {
453+ return false ;
454+ }
455+ auto configuration_canary_handle = clone_factory (shared_from_this ());
456+ if (!configuration_canary_handle) {
457+ this ->wasm ()->fail (FailState::UnableToCloneVm, " Failed to clone Base Wasm" );
458+ return false ;
459+ }
460+ if (!configuration_canary_handle->wasm ()->initialize ()) {
461+ configuration_canary_handle->wasm ()->fail (FailState::UnableToInitializeCode,
462+ " Failed to initialize Wasm code" );
463+ return false ;
464+ }
465+ auto *root_context = configuration_canary_handle->wasm ()->start (plugin);
466+ if (root_context == nullptr ) {
467+ configuration_canary_handle->wasm ()->fail (FailState::StartFailed, " Failed to start base Wasm" );
468+ return false ;
469+ }
470+ if (!configuration_canary_handle->wasm ()->configure (root_context, plugin)) {
471+ configuration_canary_handle->wasm ()->fail (FailState::ConfigureFailed,
472+ " Failed to configure base Wasm plugin" );
473+ return false ;
474+ }
475+ configuration_canary_handle->kill ();
476+ return true ;
477+ }
478+
450479std::shared_ptr<WasmHandleBase> createWasm (const std::string &vm_key, const std::string &code,
451480 const std::shared_ptr<PluginBase> &plugin,
452481 const WasmHandleFactory &factory,
@@ -465,44 +494,29 @@ std::shared_ptr<WasmHandleBase> createWasm(const std::string &vm_key, const std:
465494 base_wasms->erase (it);
466495 }
467496 }
468- if (wasm_handle) {
469- return wasm_handle;
470- }
471- wasm_handle = factory (vm_key);
472497 if (!wasm_handle) {
473- return nullptr ;
498+ // If no cached base_wasm, creates a new base_wasm, loads the code and initializes it.
499+ wasm_handle = factory (vm_key);
500+ if (!wasm_handle) {
501+ return nullptr ;
502+ }
503+ if (!wasm_handle->wasm ()->load (code, allow_precompiled)) {
504+ wasm_handle->wasm ()->fail (FailState::UnableToInitializeCode, " Failed to load Wasm code" );
505+ return nullptr ;
506+ }
507+ if (!wasm_handle->wasm ()->initialize ()) {
508+ wasm_handle->wasm ()->fail (FailState::UnableToInitializeCode,
509+ " Failed to initialize Wasm code" );
510+ return nullptr ;
511+ }
512+ (*base_wasms)[vm_key] = wasm_handle;
474513 }
475- (*base_wasms)[vm_key] = wasm_handle;
476514 }
477515
478- if (!wasm_handle->wasm ()->load (code, allow_precompiled)) {
479- wasm_handle->wasm ()->fail (FailState::UnableToInitializeCode, " Failed to load Wasm code" );
480- return nullptr ;
481- }
482- if (!wasm_handle->wasm ()->initialize ()) {
483- wasm_handle->wasm ()->fail (FailState::UnableToInitializeCode, " Failed to initialize Wasm code" );
484- return nullptr ;
485- }
486- auto configuration_canary_handle = clone_factory (wasm_handle);
487- if (!configuration_canary_handle) {
488- wasm_handle->wasm ()->fail (FailState::UnableToCloneVm, " Failed to clone Base Wasm" );
489- return nullptr ;
490- }
491- if (!configuration_canary_handle->wasm ()->initialize ()) {
492- wasm_handle->wasm ()->fail (FailState::UnableToInitializeCode, " Failed to initialize Wasm code" );
493- return nullptr ;
494- }
495- auto *root_context = configuration_canary_handle->wasm ()->start (plugin);
496- if (root_context == nullptr ) {
497- configuration_canary_handle->wasm ()->fail (FailState::StartFailed, " Failed to start base Wasm" );
516+ // Either creating new one or reusing the existing one, apply canary for each plugin.
517+ if (!wasm_handle->canary (plugin, clone_factory)) {
498518 return nullptr ;
499519 }
500- if (!configuration_canary_handle->wasm ()->configure (root_context, plugin)) {
501- configuration_canary_handle->wasm ()->fail (FailState::ConfigureFailed,
502- " Failed to configure base Wasm plugin" );
503- return nullptr ;
504- }
505- configuration_canary_handle->kill ();
506520 return wasm_handle;
507521};
508522
0 commit comments