Skip to content

fix(events-processor): scope redis ctx per event - #785

Merged
vincent-pochet merged 1 commit into
mainfrom
fix/events-processor-shutdown-context
Aug 27, 2026
Merged

fix(events-processor): scope redis ctx per event#785
vincent-pochet merged 1 commit into
mainfrom
fix/events-processor-shutdown-context

Conversation

@vincent-pochet

@vincent-pochet vincent-pochet commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Context

FlagStore and CacheStore stored the context they were built with, and that context is the
process-wide one created in main and canceled on SIGTERM. Batch processing, on the other hand,
deliberately runs on a fresh context.Background() so a rolling restart lets in-flight records
finish instead of dropping them. The two never agreed: the batch kept going while every Redis
write inside it failed with context canceled.

Every restart therefore produced a burst of failures, one per record still being processed. The
events are safe, since the failure is retryable and the offset is not committed, but the failing
flag aborts the rest of processEvent, so charge usage cache keys are never expired for that
batch and stay warm until the next event on the same subscription.

Description

The stores no longer hold a context; Flagger.Flag and Cacher.ExpireKey take one, and the
processor passes the context of the record it is working on. Suppressing context.Canceled at
capture time was the cheaper option and was rejected: it hides the log line while still skipping
cache expiration and still bouncing the batch back through Kafka.

Connection setup still uses the process context on purpose — a startup ping should die with the
process. Existing store and service tests only gained a context argument; no expectation changed.

## Context

The flag and cache stores captured the process-wide cancelable context at
construction time. That context is canceled on SIGTERM, so every Redis write
still in flight during a rolling restart failed with `context canceled`, even
though batch processing itself deliberately runs on a fresh context so that
in-flight events survive shutdown.

## Description

Redis writes now receive the context of the record being processed instead of
reading one off the store, so a shutdown no longer cancels work that the
consumer is still allowed to finish. The context field is removed from both
stores and the `Flagger` and `Cacher` interfaces carry it as an argument.

Connection setup keeps using the process context: aborting a startup ping
along with the process is the correct behavior.
@vincent-pochet
vincent-pochet merged commit 02a4bc8 into main Aug 27, 2026
1 of 2 checks passed
@vincent-pochet
vincent-pochet deleted the fix/events-processor-shutdown-context branch August 27, 2026 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants