@@ -19,7 +19,7 @@ var ports Ports
19
19
20
20
type Ports struct {
21
21
daemonPorts map [string ]int64
22
- daemonsRunning map [string ]struct {}
22
+ daemonsRunning map [string ]func ()
23
23
daemonLock sync.Mutex
24
24
25
25
startPort , endPort int64
@@ -57,6 +57,17 @@ func CloseDaemons() {
57
57
ports .daemonWG .Wait ()
58
58
}
59
59
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
+
60
71
func nextPort () int64 {
61
72
if ports .startPort == 0 {
62
73
ports .startPort = 10240
@@ -118,7 +129,7 @@ func (e *Engine) startDaemon(tool types.Tool) (string, error) {
118
129
119
130
port , ok := ports .daemonPorts [tool .ID ]
120
131
url := fmt .Sprintf ("http://127.0.0.1:%d%s" , port , path )
121
- if ok {
132
+ if ok && ports . daemonsRunning [ url ] != nil {
122
133
return url , nil
123
134
}
124
135
@@ -172,13 +183,13 @@ func (e *Engine) startDaemon(tool types.Tool) (string, error) {
172
183
173
184
if ports .daemonPorts == nil {
174
185
ports .daemonPorts = map [string ]int64 {}
175
- ports .daemonsRunning = map [string ]struct {} {}
186
+ ports .daemonsRunning = map [string ]func () {}
176
187
}
177
188
ports .daemonPorts [tool .ID ] = port
178
- ports .daemonsRunning [url ] = struct {}{}
189
+ ports .daemonsRunning [url ] = stop
179
190
180
- killedCtx , cancel := context .WithCancelCause (ctx )
181
- defer cancel (nil )
191
+ killedCtx , killedCancel := context .WithCancelCause (ctx )
192
+ defer killedCancel (nil )
182
193
183
194
ports .daemonWG .Add (1 )
184
195
go func () {
@@ -189,7 +200,7 @@ func (e *Engine) startDaemon(tool types.Tool) (string, error) {
189
200
_ = r .Close ()
190
201
_ = w .Close ()
191
202
192
- cancel (err )
203
+ killedCancel (err )
193
204
stop ()
194
205
ports .daemonLock .Lock ()
195
206
defer ports .daemonLock .Unlock ()
0 commit comments