Skip to content

Commit 316a2ab

Browse files
authored
Merge pull request #186 from langgenius/fix/infinity-environment-setup
fix( full-duplex lifecycle): avoid infinity setup loop
2 parents 0a556df + 7d07808 commit 316a2ab

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

internal/core/plugin_manager/install_to_local.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package plugin_manager
33
import (
44
"time"
55

6+
"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
67
"github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
78
"github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
89
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
@@ -61,7 +62,14 @@ func (p *PluginManager) InstallToLocal(
6162
return
6263
case err := <-errChan:
6364
if err != nil {
64-
// if error occurs, stop the plugin
65+
// if error occurs, delete the plugin from local and stop the plugin
66+
identity, err := runtime.Identity()
67+
if err != nil {
68+
log.Error("get plugin identity failed: %s", err.Error())
69+
}
70+
if err := p.installedBucket.Delete(identity); err != nil {
71+
log.Error("delete plugin from local failed: %s", err.Error())
72+
}
6573
response.Write(PluginInstallResponse{
6674
Event: PluginInstallEventError,
6775
Data: err.Error(),

internal/core/plugin_manager/runtime_lifetime.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ func (p *PluginManager) AddPluginRegisterHandler(handler func(r plugin_entities.
1313
p.pluginRegisters = append(p.pluginRegisters, handler)
1414
}
1515

16+
// fullDuplexLifecycle takes the responsibility of full-duplex lifecycle of a plugin
17+
// it will block the thread until the plugin is stopped so it's important to call it in a new goroutine
18+
// 1. try to init environment until succeed or plugin has failed too many times
19+
// 2. launchedChan and errChan are used to synchronize the plugin launch process
20+
// only if received non-nil message from errChan, it's considered the setup process has failed
21+
// 3. after exit, environment will be cleaned up
22+
//
23+
// NOTE: the size of launchedChan and errChan should always be 0 to keep the sync mechanism working
1624
func (p *PluginManager) fullDuplexLifecycle(
1725
r plugin_entities.PluginFullDuplexLifetime,
1826
launchedChan chan bool,
@@ -65,6 +73,8 @@ func (p *PluginManager) fullDuplexLifecycle(
6573
close(launchedChan)
6674
}
6775
})
76+
77+
return
6878
}
6979

7080
log.Info("init environment for plugin %s", configuration.Identity())

0 commit comments

Comments
 (0)