Skip to content

Commit 1b453d0

Browse files
Merge pull request digitalocean#163 from digitalocean/drain_channel_on_disconnect
Drain channel on disconnect
2 parents dd7bb9c + 0f00a50 commit 1b453d0

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ install:
4747
- rm qemu-${QEMU}.tar.${QEMU_EXT} libvirt-${LIBVIRT}.tar.${LIBVIRT_EXT}
4848

4949
before_install:
50-
- go get github.com/golang/lint/golint
50+
- go get golang.org/x/lint/golint
5151
- go get golang.org/x/tools/cmd/stringer
5252

5353
before_script:

qemu/domain.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ func (d *Domain) Events() (chan qmp.Event, chan struct{}, error) {
372372
// handle disconnection
373373
go func() {
374374
<-done
375+
// drain anything that gets sent on the channel
376+
// because the disconnect won't be processed if the
377+
// listenAndServe loop is waiting for the listener
378+
// to read from the unbuffered channel.
379+
go func() {
380+
for range stream {
381+
}
382+
}()
375383
d.disconnect <- stream
376384
close(stream)
377385
close(done)

qemu/domain_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,28 @@ func TestEvents(t *testing.T) {
577577
}
578578
}
579579

580+
// Test when a listener connects, but disconnects without
581+
// receiving from the events channel returned.
582+
func TestEventsDerelictListener(t *testing.T) {
583+
d, done := testDomain(t, nil)
584+
defer done()
585+
586+
_, stop, err := d.Events()
587+
if err != nil {
588+
t.Error(err)
589+
}
590+
591+
<-time.After(200 * time.Millisecond)
592+
593+
stop <- struct{}{}
594+
select {
595+
case <-stop:
596+
return
597+
case <-time.After(time.Millisecond * 20):
598+
t.Error("shutdown failed to complete")
599+
}
600+
}
601+
580602
func TestEventsUnsupported(t *testing.T) {
581603
d, done := testDomain(t, nil)
582604
defer done()
@@ -589,6 +611,8 @@ func TestEventsUnsupported(t *testing.T) {
589611
}
590612

591613
func testDomain(t *testing.T, fn func(qmp.Command) (interface{}, error)) (*Domain, func()) {
614+
t.Helper()
615+
592616
mon := &testMonitor{fn: fn}
593617
d, err := NewDomain(mon, "test")
594618
if err != nil {

qemu/string.gen.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)