|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "sync/atomic" |
| 8 | + |
| 9 | + "github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/fatalerror" |
| 10 | + "github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/interop" |
| 11 | + "github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/supervisor" |
| 12 | + "github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/supervisor/model" |
| 13 | + "github.com/google/uuid" |
| 14 | + log "github.com/sirupsen/logrus" |
| 15 | +) |
| 16 | + |
| 17 | +// LocalStackSupervisor wraps a ProcessSupervisor and intercepts runtime process termination events. |
| 18 | +// When a runtime process exits unexpectedly it sends a fault event via the EventsAPI so LocalStack |
| 19 | +// receives a proper error instead of timing out. |
| 20 | +type LocalStackSupervisor struct { |
| 21 | + model.ProcessSupervisor |
| 22 | + eventsChan chan model.Event |
| 23 | + eventsAPI interop.EventsAPI |
| 24 | + |
| 25 | + isShuttingDown *atomic.Bool |
| 26 | +} |
| 27 | + |
| 28 | +func NewLocalStackSupervisor(ctx context.Context, evs interop.EventsAPI) *LocalStackSupervisor { |
| 29 | + var isShuttingDown atomic.Bool |
| 30 | + ls := &LocalStackSupervisor{ |
| 31 | + ProcessSupervisor: supervisor.NewLocalSupervisor(), |
| 32 | + eventsAPI: evs, |
| 33 | + eventsChan: make(chan model.Event), |
| 34 | + isShuttingDown: &isShuttingDown, |
| 35 | + } |
| 36 | + |
| 37 | + go ls.loop(ctx) |
| 38 | + |
| 39 | + return ls |
| 40 | +} |
| 41 | + |
| 42 | +func (ls *LocalStackSupervisor) loop(ctx context.Context) { |
| 43 | + inCh, err := ls.ProcessSupervisor.Events(ctx, nil) |
| 44 | + if err != nil { |
| 45 | + panic(err) |
| 46 | + } |
| 47 | + defer close(ls.eventsChan) |
| 48 | + for { |
| 49 | + select { |
| 50 | + case event, ok := <-inCh: |
| 51 | + if !ok { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + select { |
| 56 | + case ls.eventsChan <- event: |
| 57 | + case <-ctx.Done(): |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + if ls.isShuttingDown.Load() { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + termination := event.Event.ProcessTerminated() |
| 66 | + if termination == nil { |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + if !strings.Contains(*termination.Name, "runtime-") { |
| 71 | + log.Debugf("Ignoring non-runtime process termination: %s", *termination.Name) |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + if termination.Signaled() != nil { |
| 76 | + log.Debugf("Runtime process signalled: %d", *termination.Signo) |
| 77 | + } |
| 78 | + |
| 79 | + faultData := interop.FaultData{ |
| 80 | + RequestID: interop.RequestID(uuid.NewString()), |
| 81 | + ErrorMessage: fmt.Errorf("Runtime exited without providing a reason"), |
| 82 | + ErrorType: fatalerror.RuntimeExit, |
| 83 | + } |
| 84 | + if !termination.Success() { |
| 85 | + faultData.ErrorMessage = fmt.Errorf("Runtime exited with error: %s", termination.String()) |
| 86 | + } |
| 87 | + |
| 88 | + if err := ls.eventsAPI.SendFault(faultData); err != nil { |
| 89 | + log.WithError(err).Error("Failed to send runtime fault event") |
| 90 | + } |
| 91 | + case <-ctx.Done(): |
| 92 | + return |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func (ls *LocalStackSupervisor) Exec(ctx context.Context, request *model.ExecRequest) error { |
| 98 | + if request.Domain == "runtime" { |
| 99 | + ls.isShuttingDown.Store(false) |
| 100 | + } |
| 101 | + return ls.ProcessSupervisor.Exec(ctx, request) |
| 102 | +} |
| 103 | + |
| 104 | +func (ls *LocalStackSupervisor) Terminate(ctx context.Context, request *model.TerminateRequest) error { |
| 105 | + defer func() { |
| 106 | + if request.Domain == "runtime" && strings.HasPrefix(request.Name, "runtime-") { |
| 107 | + ls.isShuttingDown.Store(true) |
| 108 | + } |
| 109 | + }() |
| 110 | + return ls.ProcessSupervisor.Terminate(ctx, request) |
| 111 | +} |
| 112 | + |
| 113 | +func (ls *LocalStackSupervisor) Kill(ctx context.Context, request *model.KillRequest) error { |
| 114 | + defer func() { |
| 115 | + if request.Domain == "runtime" && strings.HasPrefix(request.Name, "runtime-") { |
| 116 | + ls.isShuttingDown.Store(true) |
| 117 | + } |
| 118 | + }() |
| 119 | + return ls.ProcessSupervisor.Kill(ctx, request) |
| 120 | +} |
| 121 | + |
| 122 | +func (ls *LocalStackSupervisor) Events(ctx context.Context, _ *model.EventsRequest) (<-chan model.Event, error) { |
| 123 | + return ls.eventsChan, nil |
| 124 | +} |
0 commit comments