Skip to content

Commit 798e6e1

Browse files
committed
Fix cancellation leak
1 parent bc065c2 commit 798e6e1

File tree

4 files changed

+11
-28
lines changed

4 files changed

+11
-28
lines changed

cli/device/create.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ import (
2121
"context"
2222
"fmt"
2323
"os"
24-
"os/signal"
25-
"syscall"
2624

2725
"github.com/arduino/arduino-cli/cli/errorcodes"
2826
"github.com/arduino/arduino-cli/cli/feedback"
2927
"github.com/arduino/arduino-cloud-cli/command/device"
3028
"github.com/arduino/arduino-cloud-cli/config"
3129
"github.com/sirupsen/logrus"
3230
"github.com/spf13/cobra"
31+
"go.bug.st/cleanup"
3332
)
3433

3534
type createFlags struct {
@@ -76,13 +75,8 @@ func runCreateCommand(flags *createFlags) error {
7675
params.FQBN = &flags.fqbn
7776
}
7877

79-
ctx, canc := context.WithCancel(context.Background())
80-
ch := make(chan os.Signal)
81-
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
82-
go func() {
83-
<-ch
84-
canc()
85-
}()
78+
ctx, cancel := cleanup.InterruptableContext(context.Background())
79+
defer cancel()
8680

8781
dev, err := device.Create(ctx, params, cred)
8882
if err != nil {

cli/device/creategeneric.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ import (
2121
"context"
2222
"fmt"
2323
"os"
24-
"os/signal"
25-
"syscall"
2624

2725
"github.com/arduino/arduino-cli/cli/errorcodes"
2826
"github.com/arduino/arduino-cli/cli/feedback"
2927
"github.com/arduino/arduino-cloud-cli/command/device"
3028
"github.com/arduino/arduino-cloud-cli/config"
3129
"github.com/sirupsen/logrus"
3230
"github.com/spf13/cobra"
31+
"go.bug.st/cleanup"
3332
)
3433

3534
type createGenericFlags struct {
@@ -69,13 +68,8 @@ func runCreateGenericCommand(flags *createGenericFlags) error {
6968
FQBN: flags.fqbn,
7069
}
7170

72-
ctx, canc := context.WithCancel(context.Background())
73-
ch := make(chan os.Signal)
74-
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
75-
go func() {
76-
<-ch
77-
canc()
78-
}()
71+
ctx, cancel := cleanup.InterruptableContext(context.Background())
72+
defer cancel()
7973

8074
dev, err := device.CreateGeneric(ctx, params, cred)
8175
if err != nil {

cli/device/createlora.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ import (
2121
"context"
2222
"fmt"
2323
"os"
24-
"os/signal"
25-
"syscall"
2624

2725
"github.com/arduino/arduino-cli/cli/errorcodes"
2826
"github.com/arduino/arduino-cli/cli/feedback"
2927
"github.com/arduino/arduino-cloud-cli/command/device"
3028
"github.com/arduino/arduino-cloud-cli/config"
3129
"github.com/sirupsen/logrus"
3230
"github.com/spf13/cobra"
31+
"go.bug.st/cleanup"
3332
)
3433

3534
type createLoraFlags struct {
@@ -83,13 +82,8 @@ func runCreateLoraCommand(flags *createLoraFlags) error {
8382
params.FQBN = &flags.fqbn
8483
}
8584

86-
ctx, canc := context.WithCancel(context.Background())
87-
ch := make(chan os.Signal)
88-
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
89-
go func() {
90-
<-ch
91-
canc()
92-
}()
85+
ctx, cancel := cleanup.InterruptableContext(context.Background())
86+
defer cancel()
9387

9488
dev, err := device.CreateLora(ctx, params, cred)
9589
if err != nil {

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/google/go-cmp v0.5.6
1616
github.com/h2non/filetype v1.1.3 // indirect
1717
github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6
18-
github.com/juju/errors v0.0.0-20210818161939-5560c4c073ff
18+
github.com/juju/errors v0.0.0-20210818161939-5560c4c073ff // indirect
1919
github.com/kevinburke/ssh_config v1.1.0 // indirect
2020
github.com/manifoldco/promptui v0.9.0
2121
github.com/mattn/go-runewidth v0.0.13 // indirect
@@ -27,6 +27,7 @@ require (
2727
github.com/spf13/viper v1.10.1
2828
github.com/stretchr/testify v1.7.0
2929
github.com/xanzy/ssh-agent v0.3.1 // indirect
30+
go.bug.st/cleanup v1.0.0
3031
go.bug.st/serial v1.3.3
3132
go.bug.st/serial.v1 v0.0.0-20191202182710-24a6610f0541 // indirect
3233
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3

0 commit comments

Comments
 (0)