Skip to content

Commit 307eab1

Browse files
authored
Merge pull request #374 from defang-io/lio-leaked-ctx
Cancel ctx (go warning)
2 parents cdd845c + 293f748 commit 307eab1

File tree

2 files changed

+45
-60
lines changed

2 files changed

+45
-60
lines changed

src/pkg/cli/client/byoc/aws/byoc.go

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (b *ByocAws) Deploy(ctx context.Context, req *defangv1.DeployRequest) (*def
236236
}, nil
237237
}
238238

239-
func (b ByocAws) findZone(ctx context.Context, domain, role string) (string, error) {
239+
func (b *ByocAws) findZone(ctx context.Context, domain, role string) (string, error) {
240240
cfg, err := b.driver.LoadConfig(ctx)
241241
if err != nil {
242242
return "", annotateAwsError(err)
@@ -267,7 +267,7 @@ func (b ByocAws) findZone(ctx context.Context, domain, role string) (string, err
267267
}
268268
}
269269

270-
func (b ByocAws) delegateSubdomain(ctx context.Context) (string, error) {
270+
func (b *ByocAws) delegateSubdomain(ctx context.Context) (string, error) {
271271
if b.customDomain == "" {
272272
return "", errors.New("custom domain not set")
273273
}
@@ -305,7 +305,7 @@ func (b ByocAws) delegateSubdomain(ctx context.Context) (string, error) {
305305
return resp.Zone, nil
306306
}
307307

308-
func (b ByocAws) WhoAmI(ctx context.Context) (*defangv1.WhoAmIResponse, error) {
308+
func (b *ByocAws) WhoAmI(ctx context.Context) (*defangv1.WhoAmIResponse, error) {
309309
if _, err := b.GrpcClient.WhoAmI(ctx); err != nil {
310310
return nil, err
311311
}
@@ -331,7 +331,7 @@ func (ByocAws) GetVersions(context.Context) (*defangv1.Version, error) {
331331
return &defangv1.Version{Fabric: cdVersion}, nil
332332
}
333333

334-
func (b ByocAws) Get(ctx context.Context, s *defangv1.ServiceID) (*defangv1.ServiceInfo, error) {
334+
func (b *ByocAws) Get(ctx context.Context, s *defangv1.ServiceID) (*defangv1.ServiceInfo, error) {
335335
all, err := b.GetServices(ctx)
336336
if err != nil {
337337
return nil, err
@@ -395,30 +395,15 @@ func (b *ByocAws) Delete(ctx context.Context, req *defangv1.DeleteRequest) (*def
395395
return &defangv1.DeleteResponse{Etag: etag}, nil
396396
}
397397

398-
// stack returns a stack-qualified name, like the Pulumi TS function `stack`
399-
func (b *ByocAws) stack(name string) string {
400-
if b.pulumiProject == "" {
401-
panic("pulumiProject not set")
402-
}
403-
return fmt.Sprintf("%s-%s-%s-%s", DefangPrefix, b.pulumiProject, b.pulumiStack, name) // same as shared/common.ts
404-
}
405-
398+
// stackDir returns a stack-qualified name, like the Pulumi TS function `stackDir`
406399
func (b *ByocAws) stackDir(name string) string {
407400
if b.pulumiProject == "" {
408401
panic("pulumiProject not set")
409402
}
410403
return fmt.Sprintf("/%s/%s/%s/%s", DefangPrefix, b.pulumiProject, b.pulumiStack, name) // same as shared/common.ts
411404
}
412405

413-
func (b *ByocAws) getClusterNames() []string {
414-
// This should match the naming in pulumi/ecs/common.ts
415-
return []string{
416-
b.stack("cluster"),
417-
b.stack("gpu-cluster"),
418-
}
419-
}
420-
421-
func (b ByocAws) GetServices(ctx context.Context) (*defangv1.ListServicesResponse, error) {
406+
func (b *ByocAws) GetServices(ctx context.Context) (*defangv1.ListServicesResponse, error) {
422407
bucketName := b.bucketName()
423408
if bucketName == "" {
424409
if err := b.driver.FillOutputs(ctx); err != nil {
@@ -456,14 +441,14 @@ func (b ByocAws) GetServices(ctx context.Context) (*defangv1.ListServicesRespons
456441
return &serviceInfos, nil
457442
}
458443

459-
func (b ByocAws) getSecretID(name string) string {
444+
func (b *ByocAws) getSecretID(name string) string {
460445
if b.pulumiProject == "" {
461446
panic("pulumiProject not set")
462447
}
463448
return fmt.Sprintf("/%s/%s/%s/%s", DefangPrefix, b.pulumiProject, b.pulumiStack, name) // same as defang_service.ts
464449
}
465450

466-
func (b ByocAws) PutConfig(ctx context.Context, secret *defangv1.SecretValue) error {
451+
func (b *ByocAws) PutConfig(ctx context.Context, secret *defangv1.SecretValue) error {
467452
if !pkg.IsValidSecretName(secret.Name) {
468453
return connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("invalid secret name; must be alphanumeric or _, cannot start with a number: %q", secret.Name))
469454
}
@@ -473,7 +458,7 @@ func (b ByocAws) PutConfig(ctx context.Context, secret *defangv1.SecretValue) er
473458
return annotateAwsError(err)
474459
}
475460

476-
func (b ByocAws) ListConfig(ctx context.Context) (*defangv1.Secrets, error) {
461+
func (b *ByocAws) ListConfig(ctx context.Context) (*defangv1.Secrets, error) {
477462
prefix := b.getSecretID("")
478463
term.Debug(" - Listing parameters with prefix", prefix)
479464
awsSecrets, err := b.driver.ListSecretsByPrefix(ctx, prefix)
@@ -554,7 +539,7 @@ func (b *ByocAws) Tail(ctx context.Context, req *defangv1.TailRequest) (client.S
554539
}
555540

556541
// This function was copied from Fabric controller and slightly modified to work with BYOC
557-
func (b ByocAws) update(ctx context.Context, service *defangv1.Service) (*defangv1.ServiceInfo, error) {
542+
func (b *ByocAws) update(ctx context.Context, service *defangv1.Service) (*defangv1.ServiceInfo, error) {
558543
if err := b.quota.Validate(service); err != nil {
559544
return nil, err
560545
}
@@ -625,7 +610,7 @@ func (b ByocAws) update(ctx context.Context, service *defangv1.Service) (*defang
625610
}
626611

627612
// This function was copied from Fabric controller and slightly modified to work with BYOC
628-
func (b ByocAws) checkForMissingSecrets(ctx context.Context, secrets []*defangv1.Secret) (*defangv1.Secret, error) {
613+
func (b *ByocAws) checkForMissingSecrets(ctx context.Context, secrets []*defangv1.Secret) (*defangv1.Secret, error) {
629614
if len(secrets) == 0 {
630615
return nil, nil // no secrets to check
631616
}
@@ -654,7 +639,7 @@ func searchSecret(sorted []qualifiedName, fqn qualifiedName) bool {
654639
type qualifiedName = string // legacy
655640

656641
// This function was copied from Fabric controller and slightly modified to work with BYOC
657-
func (b ByocAws) getEndpoint(fqn qualifiedName, port *defangv1.Port) string {
642+
func (b *ByocAws) getEndpoint(fqn qualifiedName, port *defangv1.Port) string {
658643
if port.Mode == defangv1.Mode_HOST {
659644
privateFqdn := b.getPrivateFqdn(fqn)
660645
return fmt.Sprintf("%s:%d", privateFqdn, port.Target)
@@ -668,7 +653,7 @@ func (b ByocAws) getEndpoint(fqn qualifiedName, port *defangv1.Port) string {
668653
}
669654

670655
// This function was copied from Fabric controller and slightly modified to work with BYOC
671-
func (b ByocAws) getPublicFqdn(fqn qualifiedName) string {
656+
func (b *ByocAws) getPublicFqdn(fqn qualifiedName) string {
672657
if b.customDomain == "" {
673658
return "" //b.fqdn
674659
}
@@ -677,12 +662,12 @@ func (b ByocAws) getPublicFqdn(fqn qualifiedName) string {
677662
}
678663

679664
// This function was copied from Fabric controller and slightly modified to work with BYOC
680-
func (b ByocAws) getPrivateFqdn(fqn qualifiedName) string {
665+
func (b *ByocAws) getPrivateFqdn(fqn qualifiedName) string {
681666
safeFqn := dnsSafeLabel(fqn)
682667
return fmt.Sprintf("%s.%s", safeFqn, b.privateDomain) // TODO: consider merging this with ServiceDNS
683668
}
684669

685-
func (b ByocAws) getProjectDomain(zone string) string {
670+
func (b *ByocAws) getProjectDomain(zone string) string {
686671
projectLabel := dnsSafeLabel(b.pulumiProject)
687672
if projectLabel == dnsSafeLabel(b.tenantID) {
688673
return dnsSafe(zone) // the zone will already have the tenant ID
@@ -799,7 +784,7 @@ func (b *ByocAws) LoadProjectName() (string, error) {
799784
}
800785
p, err := b.LoadProject()
801786
if err != nil {
802-
return "", err
787+
return b.tenantID, err
803788
}
804789
return p.Name, nil
805790
}

src/pkg/cli/tail.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func Tail(ctx context.Context, client client.Client, service, etag string, since
111111
}
112112

113113
ctx, cancel := context.WithCancel(ctx)
114+
defer cancel()
114115

115116
serverStream, err := client.Tail(ctx, &defangv1.TailRequest{Service: service, Etag: etag, Since: timestamppb.New(since)})
116117
if err != nil {
@@ -128,38 +129,37 @@ func Tail(ctx context.Context, client client.Client, service, etag string, since
128129
}
129130

130131
if !DoVerbose {
131-
term.Info(" * Press V to toggle verbose mode")
132-
oldState, err := term.MakeUnbuf(int(os.Stdin.Fd()))
133-
if err != nil {
134-
return err
135-
}
136-
defer term.Restore(int(os.Stdin.Fd()), oldState)
132+
// Allow the user to toggle verbose mode with the V key
133+
if oldState, err := term.MakeUnbuf(int(os.Stdin.Fd())); err == nil {
134+
defer term.Restore(int(os.Stdin.Fd()), oldState)
137135

138-
input := term.NewNonBlockingStdin()
139-
defer input.Close() // abort the read
140-
go func() {
141-
var b [1]byte
142-
for {
143-
if _, err := input.Read(b[:]); err != nil {
144-
return // exit goroutine
145-
}
146-
switch b[0] {
147-
case 3: // Ctrl-C
148-
cancel()
149-
case 10, 13: // Enter or Return
150-
fmt.Println(" ") // empty line, but overwrite the spinner
151-
case 'v', 'V':
152-
verbose := !DoVerbose
153-
DoVerbose = verbose
154-
modeStr := "OFF"
155-
if verbose {
156-
modeStr = "ON"
136+
term.Info(" * Press V to toggle verbose mode")
137+
input := term.NewNonBlockingStdin()
138+
defer input.Close() // abort the read loop
139+
go func() {
140+
var b [1]byte
141+
for {
142+
if _, err := input.Read(b[:]); err != nil {
143+
return // exit goroutine
144+
}
145+
switch b[0] {
146+
case 3: // Ctrl-C
147+
cancel() // cancel the tail context
148+
case 10, 13: // Enter or Return
149+
fmt.Println(" ") // empty line, but overwrite the spinner
150+
case 'v', 'V':
151+
verbose := !DoVerbose
152+
DoVerbose = verbose
153+
modeStr := "OFF"
154+
if verbose {
155+
modeStr = "ON"
156+
}
157+
term.Info(" * Verbose mode", modeStr)
158+
go client.Track("Verbose Toggled", P{"verbose", verbose})
157159
}
158-
term.Info(" * Verbose mode", modeStr)
159-
go client.Track("Verbose Toggled", P{"verbose", verbose})
160160
}
161-
}
162-
}()
161+
}()
162+
}
163163
}
164164
}
165165

0 commit comments

Comments
 (0)