Browse Source

fix(portable): delay plugin stop to avoid errors for instant restart

Signed-off-by: Jiyong Huang <huangjy@emqx.io>
Jiyong Huang 1 năm trước cách đây
mục cha
commit
5c09bd75a8

+ 15 - 7
internal/plugin/portable/runtime/plugin_ins_manager.go

@@ -23,6 +23,7 @@ import (
 	"os"
 	"os"
 	"os/exec"
 	"os/exec"
 	"sync"
 	"sync"
+	"time"
 )
 )
 
 
 var (
 var (
@@ -121,16 +122,23 @@ func (i *PluginIns) StopSymbol(ctx api.StreamContext, ctrl *Control) error {
 		referred := false
 		referred := false
 		i.Lock()
 		i.Lock()
 		delete(i.commands, ctrl.Meta)
 		delete(i.commands, ctrl.Meta)
-		referred = len(i.commands) > 0
 		i.Unlock()
 		i.Unlock()
 		ctx.GetLogger().Infof("stopped symbol %s", ctrl.SymbolName)
 		ctx.GetLogger().Infof("stopped symbol %s", ctrl.SymbolName)
 		if !referred {
 		if !referred {
-			err := GetPluginInsManager().Kill(i.name)
-			if err != nil {
-				ctx.GetLogger().Infof("fail to stop plugin %s: %v", i.name, err)
-				return err
-			}
-			ctx.GetLogger().Infof("stop plugin %s", i.name)
+			go func() {
+				// delay to kill the plugin process
+				time.Sleep(1 * time.Second)
+				i.RLock()
+				defer i.RUnlock()
+				if len(i.commands) == 0 {
+					err := GetPluginInsManager().Kill(i.name)
+					if err != nil {
+						ctx.GetLogger().Errorf("fail to stop plugin %s: %v", i.name, err)
+						return
+					}
+					ctx.GetLogger().Infof("stop plugin %s", i.name)
+				}
+			}()
 		}
 		}
 	}
 	}
 	return err
 	return err