Skip to content

Commit 2023214

Browse files
committed
Fix already reserved panic on concurrent invokes
Closes aws#97.
1 parent ff909ae commit 2023214

File tree

1 file changed

+3
-47
lines changed

1 file changed

+3
-47
lines changed

lambda/rapidcore/server.go

+3-47
Original file line numberDiff line numberDiff line change
@@ -107,59 +107,35 @@ type Server struct {
107107
var _ interop.Server = (*Server)(nil)
108108

109109
func (s *Server) setRapidPhase(phase rapidPhase) {
110-
s.mutex.Lock()
111-
defer s.mutex.Unlock()
112-
113110
s.rapidPhase = phase
114111
}
115112

116113
func (s *Server) getRapidPhase() rapidPhase {
117-
s.mutex.Lock()
118-
defer s.mutex.Unlock()
119-
120114
return s.rapidPhase
121115
}
122116

123117
func (s *Server) setRuntimeState(state runtimeState) {
124-
s.mutex.Lock()
125-
defer s.mutex.Unlock()
126-
127118
s.runtimeState = state
128119
}
129120

130121
func (s *Server) getRuntimeState() runtimeState {
131-
s.mutex.Lock()
132-
defer s.mutex.Unlock()
133-
134122
return s.runtimeState
135123
}
136124

137125
func (s *Server) SetInvokeTimeout(timeout time.Duration) {
138-
s.mutex.Lock()
139-
defer s.mutex.Unlock()
140-
141126
s.invokeTimeout = timeout
142127
}
143128

144129
func (s *Server) GetInvokeTimeout() time.Duration {
145-
s.mutex.Lock()
146-
defer s.mutex.Unlock()
147-
148130
return s.invokeTimeout
149131
}
150132

151133
func (s *Server) GetInvokeContext() *InvokeContext {
152-
s.mutex.Lock()
153-
defer s.mutex.Unlock()
154-
155134
ctx := *s.invokeCtx
156135
return &ctx
157136
}
158137

159138
func (s *Server) setNewInvokeContext(invokeID string, traceID, lambdaSegmentID string) (*ReserveResponse, error) {
160-
s.mutex.Lock()
161-
defer s.mutex.Unlock()
162-
163139
if s.invokeCtx != nil {
164140
return nil, ErrAlreadyReserved
165141
}
@@ -226,9 +202,6 @@ func (s *Server) awaitInitCompletion() {
226202
}
227203

228204
func (s *Server) setReplyStream(w http.ResponseWriter, direct bool) (string, error) {
229-
s.mutex.Lock()
230-
defer s.mutex.Unlock()
231-
232205
if s.invokeCtx == nil {
233206
return "", ErrNotReserved
234207
}
@@ -248,9 +221,6 @@ func (s *Server) setReplyStream(w http.ResponseWriter, direct bool) (string, err
248221

249222
// Release closes the invocation, making server ready for reserve again
250223
func (s *Server) Release() error {
251-
s.mutex.Lock()
252-
defer s.mutex.Unlock()
253-
254224
if s.invokeCtx == nil {
255225
return ErrNotReserved
256226
}
@@ -267,9 +237,6 @@ func (s *Server) Release() error {
267237

268238
// GetCurrentInvokeID
269239
func (s *Server) GetCurrentInvokeID() string {
270-
s.mutex.Lock()
271-
defer s.mutex.Unlock()
272-
273240
if s.invokeCtx == nil {
274241
return ""
275242
}
@@ -350,8 +317,6 @@ func (s *Server) sendResponseUnsafe(invokeID string, additionalHeaders map[strin
350317

351318
func (s *Server) SendResponse(invokeID string, resp *interop.StreamableInvokeResponse) error {
352319
s.setRuntimeState(runtimeInvokeResponseSent)
353-
s.mutex.Lock()
354-
defer s.mutex.Unlock()
355320
runtimeCalledResponse := true
356321
return s.sendResponseUnsafe(invokeID, resp.Headers, resp.Payload, resp.Trailers, resp.Request, runtimeCalledResponse)
357322
}
@@ -372,8 +337,6 @@ func (s *Server) SendInitErrorResponse(resp *interop.ErrorInvokeResponse) error
372337
func (s *Server) SendErrorResponse(invokeID string, resp *interop.ErrorInvokeResponse) error {
373338
log.Debugf("Sending Error Response: %s", resp.FunctionError.Type)
374339
s.setRuntimeState(runtimeInvokeError)
375-
s.mutex.Lock()
376-
defer s.mutex.Unlock()
377340
additionalHeaders := map[string]string{
378341
directinvoke.ContentTypeHeader: resp.Headers.ContentType,
379342
directinvoke.ErrorTypeHeader: string(resp.FunctionError.Type),
@@ -501,14 +464,10 @@ func deadlineNsFromTimeoutMs(timeoutMs int64) int64 {
501464
}
502465

503466
func (s *Server) setInitFailuresChan() {
504-
s.mutex.Lock()
505-
defer s.mutex.Unlock()
506467
s.initFailures = make(chan interop.InitFailure)
507468
}
508469

509470
func (s *Server) getInitFailuresChan() chan interop.InitFailure {
510-
s.mutex.Lock()
511-
defer s.mutex.Unlock()
512471
return s.initFailures
513472
}
514473

@@ -593,14 +552,10 @@ func (s *Server) FastInvoke(w http.ResponseWriter, i *interop.Invoke, direct boo
593552
}
594553

595554
func (s *Server) setCachedInitErrorResponse(errResp *interop.ErrorInvokeResponse) {
596-
s.mutex.Lock()
597-
defer s.mutex.Unlock()
598555
s.cachedInitErrorResponse = errResp
599556
}
600557

601558
func (s *Server) getCachedInitErrorResponse() *interop.ErrorInvokeResponse {
602-
s.mutex.Lock()
603-
defer s.mutex.Unlock()
604559
return s.cachedInitErrorResponse
605560
}
606561

@@ -613,8 +568,6 @@ func (s *Server) trySendDefaultErrorResponse(resp *interop.ErrorInvokeResponse)
613568
}
614569

615570
func (s *Server) CurrentToken() *interop.Token {
616-
s.mutex.Lock()
617-
defer s.mutex.Unlock()
618571
if s.invokeCtx == nil {
619572
return nil
620573
}
@@ -625,6 +578,9 @@ func (s *Server) CurrentToken() *interop.Token {
625578
// Invoke is used by the Runtime Interface Emulator (Rapid Local)
626579
// https://github.com/aws/aws-lambda-runtime-interface-emulator
627580
func (s *Server) Invoke(responseWriter http.ResponseWriter, invoke *interop.Invoke) error {
581+
s.mutex.Lock()
582+
defer s.mutex.Unlock()
583+
628584
resetCtx, resetCancel := context.WithCancel(context.Background())
629585
defer resetCancel()
630586

0 commit comments

Comments
 (0)