@@ -4,13 +4,15 @@ package main
44
55import (
66 "context"
7- log "github.com/sirupsen/logrus"
8- "go.amzn.com/lambda/interop"
9- "go.amzn.com/lambda/rapidcore"
107 "os"
118 "runtime/debug"
129 "strconv"
1310 "strings"
11+ "time"
12+
13+ log "github.com/sirupsen/logrus"
14+ "go.amzn.com/lambda/interop"
15+ "go.amzn.com/lambda/rapidcore"
1416)
1517
1618type LsOpts struct {
@@ -188,6 +190,12 @@ func main() {
188190 SetLogsEgressAPI (localStackLogsEgressApi ).
189191 SetTracer (tracer )
190192
193+ // Corresponds to the 'AWS_LAMBDA_RUNTIME_API' environment variable.
194+ // We need to ensure the runtime server is up before the INIT phase,
195+ // but this envar is only set after the InitHandler is called.
196+ runtimeAPIAddress := "127.0.0.1:9001"
197+ sandbox .SetRuntimeAPIAddress (runtimeAPIAddress )
198+
191199 // xray daemon
192200 endpoint := "http://" + lsOpts .LocalstackIP + ":" + lsOpts .EdgePort
193201 xrayConfig := initConfig (endpoint , xRayLogLevel )
@@ -225,6 +233,14 @@ func main() {
225233 }
226234 go RunHotReloadingListener (interopServer , lsOpts .HotReloadingPaths , fileWatcherContext , lsOpts .FileWatcherStrategy )
227235
236+ log .Debugf ("Awaiting initialization of runtime api at %s." , runtimeAPIAddress )
237+ // Fixes https://github.com/localstack/localstack/issues/12680
238+ ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
239+ if err := waitForRuntimeAPI (ctx , runtimeAPIAddress ); err != nil {
240+ log .Fatalf ("Lambda Runtime API server at %s did not come up in 30s, with error %s" , runtimeAPIAddress , err .Error ())
241+ }
242+ cancel ()
243+
228244 // start runtime init. It is important to start `InitHandler` synchronously because we need to ensure the
229245 // notification channels and status fields are properly initialized before `AwaitInitialized`
230246 log .Debugln ("Starting runtime init." )
@@ -239,6 +255,7 @@ func main() {
239255 return
240256 }
241257
258+ // ping the runtime server until ready
242259 log .Debugln ("Completed initialization of runtime init. Sending status ready to LocalStack." )
243260 if err := interopServer .localStackAdapter .SendStatus (Ready , []byte {}); err != nil {
244261 log .Fatalln ("Failed to send status ready to LocalStack " + err .Error () + ". Exiting." )
0 commit comments