Skip to content

Commit d7b9cb0

Browse files
committed
shim: move event context timeout to publsher
Before this change, if an event fails to send on the first attempt, subsequent attempts will fail with context.Cancelled because the the caller of publish passes a cancellable timeout, which the publisher uses to send the event. The publisher returns immediately if the send fails, but adds the event to an async queue to try again. Meanwhile the caller will return cancelling the context. Additionally, subsequent attempts may fail to send because the timeout was expected to be for a single request but the queue sleeps for `attempt*time.Second`. In the shim service, the timeout was set to 5s, which means the send will fail with context.DeadlineExceeded before it reaches `maxRequeue` (which is currently 5). This change moves the timeout to the publisher so each send attempt gets its own timeout. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
1 parent e818fe2 commit d7b9cb0

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

runtime/v2/runc/v2/service.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,9 +769,7 @@ func (s *service) forward(ctx context.Context, publisher shim.Publisher) {
769769
ns, _ := namespaces.Namespace(ctx)
770770
ctx = namespaces.WithNamespace(context.Background(), ns)
771771
for e := range s.events {
772-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
773772
err := publisher.Publish(ctx, runc.GetTopic(e), e)
774-
cancel()
775773
if err != nil {
776774
logrus.WithError(err).Error("post event")
777775
}

runtime/v2/shim/publisher.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ func (l *RemoteEventsPublisher) Publish(ctx context.Context, topic string, event
130130
func (l *RemoteEventsPublisher) forwardRequest(ctx context.Context, req *v1.ForwardRequest) error {
131131
service, err := l.client.EventsService()
132132
if err == nil {
133-
_, err = service.Forward(ctx, req)
133+
fCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
134+
_, err = service.Forward(fCtx, req)
135+
cancel()
134136
if err == nil {
135137
return nil
136138
}
@@ -149,7 +151,12 @@ func (l *RemoteEventsPublisher) forwardRequest(ctx context.Context, req *v1.Forw
149151
if err != nil {
150152
return err
151153
}
152-
if _, err = service.Forward(ctx, req); err != nil {
154+
155+
// try again with a fresh context, otherwise we may get a context timeout unexpectedly.
156+
fCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
157+
_, err = service.Forward(fCtx, req)
158+
cancel()
159+
if err != nil {
153160
return err
154161
}
155162

0 commit comments

Comments
 (0)