@@ -556,67 +556,52 @@ std::shared_ptr<WasmHandleBase> getThreadLocalWasm(std::string_view vm_key) {
556
556
557
557
void setWasmFailCallback (const std::string &vm_key,
558
558
const std::shared_ptr<WasmHandleBase> &wasm_handle) {
559
- std::weak_ptr<WasmHandleBase> wasm_handle_for_copy = wasm_handle;
560
- wasm_handle->wasm ()->wasm_vm ()->addFailCallback (
561
- [vm_key, wasm_handle_for_copy](proxy_wasm::FailState fail_state) {
562
- if (fail_state == proxy_wasm::FailState::RuntimeError) {
563
- // If VM failed, erase the entry so that:
564
- // 1) we can recreate the new thread local VM from the same base_wasm.
565
- // 2) we wouldn't reuse the failed VM for new plugins accidentally.
566
- local_wasms.erase (vm_key);
567
- auto wasm_handle = wasm_handle_for_copy.lock ();
568
- if (!wasm_handle) {
569
- return ;
570
- }
571
- wasm_handle->setNeedRecover ();
572
- }
573
- });
559
+ wasm_handle->wasm ()->wasm_vm ()->addFailCallback ([vm_key](proxy_wasm::FailState fail_state) {
560
+ if (fail_state == proxy_wasm::FailState::RuntimeError) {
561
+ // If VM failed, erase the entry so that:
562
+ // 1) we can recreate the new thread local VM from the same base_wasm.
563
+ // 2) we wouldn't reuse the failed VM for new plugins accidentally.
564
+ local_wasms.erase (vm_key);
565
+ }
566
+ });
574
567
}
575
568
576
569
void setWasmRecoverCallback (const std::string &vm_key,
577
570
const std::shared_ptr<WasmHandleBase> &wasm_handle,
578
571
const std::shared_ptr<WasmHandleBase> &base_handle,
579
572
const WasmHandleCloneFactory &clone_factory) {
580
- std::weak_ptr<WasmHandleBase> wasm_handle_for_copy = wasm_handle;
581
- wasm_handle->setRecoverVmCallback ([vm_key, wasm_handle_for_copy, base_handle,
582
- clone_factory]() -> std::shared_ptr<WasmHandleBase> {
583
- const auto &integration = base_handle->wasm ()->wasm_vm ()->integration ();
584
- integration->trace (" Start recover wasm_handle" );
585
- auto it = local_wasms.find (vm_key);
586
- if (it != local_wasms.end ()) {
587
- auto wasm_handle = it->second .lock ();
588
- if (wasm_handle) {
589
- integration->trace (" Wasm handle already exists" );
590
- return wasm_handle;
591
- }
592
- local_wasms.erase (vm_key);
593
- }
594
- // try to recover wasm vm
595
- auto wasm_handle = wasm_handle_for_copy.lock ();
596
- if (!wasm_handle) {
597
- base_handle->wasm ()->fail (FailState::RecoverError, " Wasm handle lock failed" );
598
- return nullptr ;
599
- }
600
- auto new_handle = clone_factory (base_handle);
601
- if (!new_handle) {
602
- base_handle->wasm ()->fail (FailState::RecoverError,
603
- " Failed to clone Base Wasm during recover" );
604
- return nullptr ;
605
- }
573
+ wasm_handle->setRecoverVmCallback (
574
+ [vm_key, base_handle, clone_factory]() -> std::shared_ptr<WasmHandleBase> {
575
+ const auto &integration = base_handle->wasm ()->wasm_vm ()->integration ();
576
+ integration->trace (" Start recover wasm_handle" );
577
+ auto it = local_wasms.find (vm_key);
578
+ if (it != local_wasms.end ()) {
579
+ auto wasm_handle = it->second .lock ();
580
+ if (wasm_handle) {
581
+ integration->trace (" Wasm handle already exists" );
582
+ return wasm_handle;
583
+ }
584
+ local_wasms.erase (vm_key);
585
+ }
586
+ // Try to recover wasm vm
587
+ auto new_handle = clone_factory (base_handle);
588
+ if (!new_handle) {
589
+ base_handle->wasm ()->fail (FailState::RecoverError,
590
+ " Failed to clone Base Wasm during recover" );
591
+ return nullptr ;
592
+ }
606
593
607
- if (!new_handle->wasm ()->initialize ()) {
608
- base_handle->wasm ()->fail (FailState::RecoverError,
609
- " Failed to initialize Wasm code during recover" );
610
- return nullptr ;
611
- }
612
- // avoid the context use the stale wasm ptr
613
- wasm_handle->wasm ()->clearWasmInContext ();
614
- wasm_handle->swap (new_handle);
615
- local_wasms[vm_key] = wasm_handle;
616
- integration->trace (" Wasm handle has been recovered" );
617
- setWasmFailCallback (vm_key, wasm_handle);
618
- return wasm_handle;
619
- });
594
+ if (!new_handle->wasm ()->initialize ()) {
595
+ base_handle->wasm ()->fail (FailState::RecoverError,
596
+ " Failed to initialize Wasm code during recover" );
597
+ return nullptr ;
598
+ }
599
+ local_wasms[vm_key] = new_handle;
600
+ setWasmFailCallback (vm_key, new_handle);
601
+ setWasmRecoverCallback (vm_key, new_handle, base_handle, clone_factory);
602
+ integration->trace (" Wasm handle has been recovered" );
603
+ return new_handle;
604
+ });
620
605
}
621
606
622
607
static std::shared_ptr<WasmHandleBase>
@@ -651,59 +636,54 @@ getOrCreateThreadLocalWasm(const std::shared_ptr<WasmHandleBase> &base_handle,
651
636
}
652
637
653
638
void setPluginFailCallback (const std::string &key,
654
- const std::shared_ptr<WasmHandleBase> &wasm_handle,
655
- const std::shared_ptr<PluginHandleBase> &plugin_handle) {
656
- std::weak_ptr<PluginHandleBase> plugin_handle_for_copy = plugin_handle;
657
- wasm_handle->wasm ()->wasm_vm ()->addFailCallback (
658
- [key, plugin_handle_for_copy](proxy_wasm::FailState fail_state) {
659
- if (fail_state == proxy_wasm::FailState::RuntimeError) {
660
- // If VM failed, erase the entry so that:
661
- // 1) we can recreate the new thread local plugin from the same base_wasm.
662
- // 2) we wouldn't reuse the failed VM for new plugin configs accidentally.
663
- local_plugins.erase (key);
664
- auto plugin_handle = plugin_handle_for_copy.lock ();
665
- if (!plugin_handle) {
666
- return ;
667
- }
668
- plugin_handle->setNeedRecover ();
669
- }
670
- });
639
+ const std::shared_ptr<WasmHandleBase> &wasm_handle) {
640
+ wasm_handle->wasm ()->wasm_vm ()->addFailCallback ([key](proxy_wasm::FailState fail_state) {
641
+ if (fail_state == proxy_wasm::FailState::RuntimeError) {
642
+ // If VM failed, erase the entry so that:
643
+ // 1) we can recreate the new thread local plugin from the same base_wasm.
644
+ // 2) we wouldn't reuse the failed VM for new plugin configs accidentally.
645
+ local_plugins.erase (key);
646
+ }
647
+ });
671
648
}
672
649
673
650
void setPluginRecoverCallback (const std::string &key,
674
651
const std::shared_ptr<PluginHandleBase> &plugin_handle,
675
652
const std::shared_ptr<WasmHandleBase> &base_handle,
676
- const std::shared_ptr<PluginBase> &plugin) {
677
- std::weak_ptr<PluginHandleBase> plugin_handle_for_copy = plugin_handle;
678
- plugin_handle->setRecoverPluginCallback ([key, plugin_handle_for_copy, base_handle,
679
- plugin](std::shared_ptr<WasmHandleBase> &wasm_handle) {
680
- const auto &integration = base_handle->wasm ()->wasm_vm ()->integration ();
681
- integration->trace (" Start recover plugin_handle" );
682
- // We cannot reuse plugin that other have created because the life cycle of the plugin
683
- // handle is controlled by the upper layer.
684
- auto plugin_handle = plugin_handle_for_copy.lock ();
685
- if (!plugin_handle) {
686
- base_handle->wasm ()->fail (FailState::RecoverError, " Plugin handle lock failed" );
687
- return false ;
688
- }
689
- plugin_handle->updateWasm (wasm_handle);
690
- // Create and initialize new thread-local Plugin.
691
- auto *plugin_context = wasm_handle->wasm ()->start (plugin);
692
- if (plugin_context == nullptr ) {
693
- base_handle->wasm ()->fail (FailState::RecoverError,
694
- " Failed to start thread-local Wasm during recover" );
695
- return false ;
696
- }
697
- if (!wasm_handle->wasm ()->configure (plugin_context, plugin)) {
698
- base_handle->wasm ()->fail (FailState::RecoverError,
699
- " Failed to configure thread-local Wasm plugin during recover" );
700
- return false ;
701
- }
702
- local_plugins[key] = plugin_handle;
703
- integration->trace (" Plugin_handle has been recovered" );
704
- setPluginFailCallback (key, wasm_handle, plugin_handle);
705
- return true ;
706
- });
653
+ const std::shared_ptr<PluginBase> &plugin,
654
+ const PluginHandleFactory &plugin_factory) {
655
+ plugin_handle->setRecoverPluginCallback (
656
+ [key, base_handle, plugin, plugin_factory](
657
+ std::shared_ptr<WasmHandleBase> &wasm_handle) -> std::shared_ptr<PluginHandleBase> {
658
+ const auto &integration = base_handle->wasm ()->wasm_vm ()->integration ();
659
+ integration->trace (" Start recover plugin_handle" );
660
+ auto it = local_plugins.find (key);
661
+ if (it != local_plugins.end ()) {
662
+ auto plugin_handle = it->second .lock ();
663
+ if (plugin_handle) {
664
+ return plugin_handle;
665
+ }
666
+ local_plugins.erase (key);
667
+ }
668
+ // Try to recover wasm plugin
669
+ auto *plugin_context = wasm_handle->wasm ()->start (plugin);
670
+ if (plugin_context == nullptr ) {
671
+ base_handle->wasm ()->fail (FailState::RecoverError,
672
+ " Failed to start thread-local Wasm during recover" );
673
+ return nullptr ;
674
+ }
675
+ if (!wasm_handle->wasm ()->configure (plugin_context, plugin)) {
676
+ base_handle->wasm ()->fail (FailState::RecoverError,
677
+ " Failed to configure thread-local Wasm plugin during recover" );
678
+ return nullptr ;
679
+ }
680
+ auto new_handle = plugin_factory (wasm_handle, plugin);
681
+ local_plugins[key] = new_handle;
682
+ setPluginFailCallback (key, wasm_handle);
683
+ setPluginRecoverCallback (key, new_handle, base_handle, plugin, plugin_factory);
684
+ integration->trace (" Plugin handle has been recovered" );
685
+ return new_handle;
686
+ });
707
687
}
708
688
709
689
std::shared_ptr<PluginHandleBase> getOrCreateThreadLocalPlugin (
@@ -738,8 +718,8 @@ std::shared_ptr<PluginHandleBase> getOrCreateThreadLocalPlugin(
738
718
}
739
719
auto plugin_handle = plugin_factory (wasm_handle, plugin);
740
720
local_plugins[key] = plugin_handle;
741
- setPluginFailCallback (key, wasm_handle, plugin_handle );
742
- setPluginRecoverCallback (key, plugin_handle, base_handle, plugin);
721
+ setPluginFailCallback (key, wasm_handle);
722
+ setPluginRecoverCallback (key, plugin_handle, base_handle, plugin, plugin_factory );
743
723
return plugin_handle;
744
724
}
745
725
0 commit comments