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

Commit c735bee

Browse files
committed
QemuContext.qmpSend: new function that check ctx status before send data to qc.qmp
Got following panic in hyperd: hyperd[26359]: E0326 06:39:44.147320 26359 stop.go:20] failed to shuting down: not in running state hyperd[26359]: panic: send on closed channel hyperd[26359]: goroutine 28404 [running]: hyperd[26359]: github.com/hyperhq/hyperd/vendor/github.com/hyperhq/runv/hypervisor/qemu.newDiskDelSession(0xc421f0e2c0, 0xc422aa6180, 0x1, 0x219a420, 0xc420b38880 hyperd[26359]: /root/gopath/src/github.com/hyperhq/hyperd/vendor/github.com/hyperhq/runv/hypervisor/qemu/qmp_wrapper.go:80 +0x4bd hyperd[26359]: github.com/hyperhq/hyperd/vendor/github.com/hyperhq/runv/hypervisor/qemu.(*QemuContext).RemoveDisk(0xc422aa6180, 0xc421f0e2c0, 0xc42035cb00, 0x219a hyperd[26359]: /root/gopath/src/github.com/hyperhq/hyperd/vendor/github.com/hyperhq/runv/hypervisor/qemu/qemu.go:259 +0x61 hyperd[26359]: github.com/hyperhq/hyperd/vendor/github.com/hyperhq/runv/hypervisor.(*DiskContext).remove.func1(0xc423c4ac90, 0xc420bc0240) hyperd[26359]: /root/gopath/src/github.com/hyperhq/hyperd/vendor/github.com/hyperhq/runv/hypervisor/disk.go:127 +0xd0 hyperd[26359]: created by github.com/hyperhq/hyperd/vendor/github.com/hyperhq/runv/hypervisor.(*DiskContext).remove hyperd[26359]: /root/gopath/src/github.com/hyperhq/hyperd/vendor/github.com/hyperhq/runv/hypervisor/disk.go:125 +0x18e The reason is qc.qmp will be closed in QemuContext.Close when ctx closed. But high level code doesn't know that and keep send data to qc.qmp. Add this patch check the ctx status before send data to qc.qmp to handle the issue. Signed-off-by: Hui Zhu <teawater@hyper.sh>
1 parent acc162c commit c735bee

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)