fix(api): reserve the app when starting an annotation reply job - #41203
Open
alanhuangyoo wants to merge 1 commit into
Open
fix(api): reserve the app when starting an annotation reply job#41203alanhuangyoo wants to merge 1 commit into
alanhuangyoo wants to merge 1 commit into
Conversation
enable_app_annotation and disable_app_annotation read the app-level key that is supposed to serialize these jobs, but never wrote it, so the guard could not fire: every request minted a new job id and enqueued another task that rebuilds or deletes the same annotation vector data. Reserve the app with SET NX EX before enqueueing and return the in-flight job id when the reservation is already held. The reservation is released if enqueueing fails, and the worker early-return paths now record a terminal status instead of leaving the caller polling "waiting" forever. Closes langgenius#40595
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #40595
Problem
AppAnnotationService.enable_app_annotationreads the app-level key that is supposed to serialize annotation reply jobs, but nothing ever writes it:The guard can therefore never fire. Every call mints a new job id and enqueues another task, and those tasks concurrently rebuild or delete the same annotation vector data and mutate the same
AppAnnotationSetting. The worker'sfinallyblock then deletes a key that was never acquired.disable_app_annotationhas the same shape.Two smaller defects come from the same code:
setnxand no expiry, so it survives forever whenever the task does not reach a terminal path.waitingindefinitely.Change
SET key job_id NX EXbefore enqueueing. A second request for the same action gets the in-flight job id back withprocessinginstead of starting a competing task..delay()raises, so a broker failure does not lock the app for the full TTL.errorstatus (plus the existing*_error_*key) on the worker early-return paths and drop the reservation there._running_job_id()decodes the reserved value because the client is configured withdecode_responses=False; the previous code would have returned raw bytes as the job id, which was masked by the guard never firing.Enable and disable still use separate reservations. The issue also proposes making them mutually exclusive with each other, which changes behaviour beyond the broken guard, so I left it out of this PR — happy to follow up if you want it.
Tests
TestAppAnnotationServiceEnableDisablepreviously mocked the whole redis client and asserted the exactsetnxcall, so it described the implementation rather than the behaviour and could not observe the missing reservation. It is rewritten against a small fake that implementsSET NX EXsemantics, and covers: the reservation is written with a TTL, a second request reuses the in-flight job without enqueueing, and a failed enqueue releases the reservation.Against the current
mainsources, all five fail:with, for the reuse cases:
With this change:
ruff checkandruff format --checkare clean on the four touched files.