mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-26 13:48:49 +08:00
Dji Tello Halt does not terminate all the related goroutines and may wait forever when it is called multiple times
Fix the issue.
This commit is contained in:
parent
3c8f48a8ac
commit
b07a272ca6
@ -10,6 +10,7 @@ import (
|
||||
"net"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"gobot.io/x/gobot"
|
||||
@ -192,6 +193,7 @@ type Driver struct {
|
||||
bouncing bool
|
||||
gobot.Eventer
|
||||
doneCh chan struct{}
|
||||
doneChReaderCount int32
|
||||
}
|
||||
|
||||
// NewDriver creates a driver for the Tello drone. Pass in the UDP port to use for the responses
|
||||
@ -280,7 +282,10 @@ func (d *Driver) Start() error {
|
||||
d.cmdConn = cmdConn
|
||||
|
||||
// handle responses
|
||||
d.addDoneChReaderCount(1)
|
||||
go func() {
|
||||
defer d.addDoneChReaderCount(-1)
|
||||
|
||||
d.On(d.Event(ConnectedEvent), func(interface{}) {
|
||||
d.SendDateTime()
|
||||
d.processVideo()
|
||||
@ -304,14 +309,23 @@ func (d *Driver) Start() error {
|
||||
d.SendCommand(d.connectionString())
|
||||
|
||||
// send stick commands
|
||||
d.addDoneChReaderCount(1)
|
||||
go func() {
|
||||
defer d.addDoneChReaderCount(-1)
|
||||
|
||||
stickCmdLoop:
|
||||
for {
|
||||
select {
|
||||
case <-d.doneCh:
|
||||
break stickCmdLoop
|
||||
default:
|
||||
err := d.SendStickCommand()
|
||||
if err != nil {
|
||||
fmt.Println("stick command error:", err)
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
@ -321,7 +335,11 @@ func (d *Driver) Start() error {
|
||||
func (d *Driver) Halt() (err error) {
|
||||
// send a landing command when we disconnect, and give it 500ms to be received before we shutdown
|
||||
d.Land()
|
||||
readerCount := atomic.LoadInt32(&d.doneChReaderCount)
|
||||
for i := 0; i < int(readerCount); i++ {
|
||||
d.doneCh <- struct{}{}
|
||||
}
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
d.cmdConn.Close()
|
||||
@ -946,7 +964,10 @@ func (d *Driver) processVideo() error {
|
||||
return err
|
||||
}
|
||||
|
||||
d.addDoneChReaderCount(1)
|
||||
go func() {
|
||||
defer d.addDoneChReaderCount(-1)
|
||||
|
||||
videoConnLoop:
|
||||
for {
|
||||
select {
|
||||
@ -989,6 +1010,10 @@ func (d *Driver) connectionString() string {
|
||||
return res
|
||||
}
|
||||
|
||||
func (d *Driver) addDoneChReaderCount(delta int32) {
|
||||
atomic.AddInt32(&d.doneChReaderCount, delta)
|
||||
}
|
||||
|
||||
func (f *FlightData) AirSpeed() float64 {
|
||||
return math.Sqrt(
|
||||
math.Pow(float64(f.NorthSpeed), 2) +
|
||||
|
@ -151,17 +151,29 @@ func TestHaltShouldTerminateAllTheRelatedGoroutines(t *testing.T) {
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(3)
|
||||
|
||||
d.addDoneChReaderCount(1)
|
||||
go func() {
|
||||
defer d.addDoneChReaderCount(-1)
|
||||
|
||||
<-d.doneCh
|
||||
wg.Done()
|
||||
fmt.Println("Done routine 1.")
|
||||
}()
|
||||
|
||||
d.addDoneChReaderCount(1)
|
||||
go func() {
|
||||
defer d.addDoneChReaderCount(-1)
|
||||
|
||||
<-d.doneCh
|
||||
wg.Done()
|
||||
fmt.Println("Done routine 2.")
|
||||
}()
|
||||
|
||||
d.addDoneChReaderCount(1)
|
||||
go func() {
|
||||
defer d.addDoneChReaderCount(-1)
|
||||
|
||||
<-d.doneCh
|
||||
wg.Done()
|
||||
fmt.Println("Done routine 3.")
|
||||
@ -169,6 +181,8 @@ func TestHaltShouldTerminateAllTheRelatedGoroutines(t *testing.T) {
|
||||
|
||||
d.Halt()
|
||||
wg.Wait()
|
||||
|
||||
gobottest.Assert(t, d.doneChReaderCount, int32(0))
|
||||
}
|
||||
|
||||
func TestHaltNotWaitForeverWhenCalledMultipleTimes(t *testing.T) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user