@@ -19,7 +19,7 @@ var ports Ports
1919
2020type Ports struct {
2121 daemonPorts map [string ]int64
22- daemonsRunning map [string ]struct {}
22+ daemonsRunning map [string ]func ()
2323 daemonLock sync.Mutex
2424
2525 startPort , endPort int64
@@ -57,6 +57,17 @@ func CloseDaemons() {
5757 ports .daemonWG .Wait ()
5858}
5959
60+ func StopDaemon (url string ) {
61+ ports .daemonLock .Lock ()
62+ defer ports .daemonLock .Unlock ()
63+
64+ if stop := ports .daemonsRunning [url ]; stop != nil {
65+ stop ()
66+ }
67+
68+ delete (ports .daemonsRunning , url )
69+ }
70+
6071func nextPort () int64 {
6172 if ports .startPort == 0 {
6273 ports .startPort = 10240
@@ -118,7 +129,7 @@ func (e *Engine) startDaemon(tool types.Tool) (string, error) {
118129
119130 port , ok := ports .daemonPorts [tool .ID ]
120131 url := fmt .Sprintf ("http://127.0.0.1:%d%s" , port , path )
121- if ok {
132+ if ok && ports . daemonsRunning [ url ] != nil {
122133 return url , nil
123134 }
124135
@@ -172,13 +183,13 @@ func (e *Engine) startDaemon(tool types.Tool) (string, error) {
172183
173184 if ports .daemonPorts == nil {
174185 ports .daemonPorts = map [string ]int64 {}
175- ports .daemonsRunning = map [string ]struct {} {}
186+ ports .daemonsRunning = map [string ]func () {}
176187 }
177188 ports .daemonPorts [tool .ID ] = port
178- ports .daemonsRunning [url ] = struct {}{}
189+ ports .daemonsRunning [url ] = stop
179190
180- killedCtx , cancel := context .WithCancelCause (ctx )
181- defer cancel (nil )
191+ killedCtx , killedCancel := context .WithCancelCause (ctx )
192+ defer killedCancel (nil )
182193
183194 ports .daemonWG .Add (1 )
184195 go func () {
@@ -189,7 +200,7 @@ func (e *Engine) startDaemon(tool types.Tool) (string, error) {
189200 _ = r .Close ()
190201 _ = w .Close ()
191202
192- cancel (err )
203+ killedCancel (err )
193204 stop ()
194205 ports .daemonLock .Lock ()
195206 defer ports .daemonLock .Unlock ()
0 commit comments