Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit af3acb5

Browse files
authored
Merge pull request #682 from teawater/fixqmpcrash
QemuContext.qmpSend: new function that check ctx status before send d…
2 parents 0ce977e + c735bee commit af3acb5

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

hypervisor/context.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,18 @@ func (ctx *VmContext) IsRunning() bool {
235235
return running
236236
}
237237

238+
func (ctx *VmContext) IsClosedLocked() bool {
239+
return ctx.current == StateNone
240+
}
241+
242+
func (ctx *VmContext) Rlock() {
243+
ctx.lock.RLock()
244+
}
245+
246+
func (ctx *VmContext) RUnlock() {
247+
ctx.lock.RUnlock()
248+
}
249+
238250
// User API
239251
func (ctx *VmContext) SetNetworkEnvironment(net *api.SandboxConfig) {
240252
ctx.lock.Lock()

hypervisor/qemu/qemu.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ func (qc *QemuContext) Shutdown(ctx *hypervisor.VmContext) {
178178
qmpQemuQuit(ctx, qc)
179179
}
180180

181+
func (qc *QemuContext) qmpSend(ctx *hypervisor.VmContext, s QmpInteraction) {
182+
ctx.Rlock()
183+
defer ctx.RUnlock()
184+
if ctx.IsClosedLocked() {
185+
glog.Errorf("Cannot send message to closed sandbox %s %+v", ctx.Id, s)
186+
} else {
187+
glog.V(3).Infof("Send message to sandbox %s start", ctx.Id)
188+
qc.qmp <- s
189+
glog.V(3).Infof("Send message to sandbox %s done", ctx.Id)
190+
}
191+
}
192+
181193
func (qc *QemuContext) Kill(ctx *hypervisor.VmContext) {
182194
defer func() {
183195
err := recover()
@@ -215,12 +227,12 @@ func (qc *QemuContext) Pause(ctx *hypervisor.VmContext, pause bool) error {
215227
}
216228

217229
result := make(chan error, 1)
218-
qc.qmp <- &QmpSession{
230+
qc.qmpSend(ctx, &QmpSession{
219231
commands: commands,
220232
respond: func(err error) {
221233
result <- err
222234
},
223-
}
235+
})
224236
return <-result
225237
}
226238

@@ -321,15 +333,15 @@ func (qc *QemuContext) SetCpus(ctx *hypervisor.VmContext, cpus int) error {
321333
}
322334

323335
result := make(chan error, 1)
324-
qc.qmp <- &QmpSession{
336+
qc.qmpSend(ctx, &QmpSession{
325337
commands: commands,
326338
respond: func(err error) {
327339
if err == nil {
328340
qc.cpus = cpus
329341
}
330342
result <- err
331343
},
332-
}
344+
})
333345
return <-result
334346
}
335347

@@ -352,10 +364,10 @@ func (qc *QemuContext) AddMem(ctx *hypervisor.VmContext, slot, size int) error {
352364
},
353365
}
354366
result := make(chan error, 1)
355-
qc.qmp <- &QmpSession{
367+
qc.qmpSend(ctx, &QmpSession{
356368
commands: commands,
357369
respond: func(err error) { result <- err },
358-
}
370+
})
359371
return <-result
360372
}
361373

@@ -385,10 +397,10 @@ func (qc *QemuContext) Save(ctx *hypervisor.VmContext, path string) error {
385397

386398
result := make(chan error, 1)
387399
// TODO: use query-migrate to query until completed
388-
qc.qmp <- &QmpSession{
400+
qc.qmpSend(ctx, &QmpSession{
389401
commands: commands,
390402
respond: func(err error) { result <- err },
391-
}
403+
})
392404

393405
return <-result
394406
}

0 commit comments

Comments
 (0)