From b018858704197acf56d1cb37176331be2f230390 Mon Sep 17 00:00:00 2001 From: selladuraisomasundaram Date: Thu, 22 Oct 2026 22:23:40 +0530 Subject: [PATCH 1/2] fix(jobs): serialize concurrent job updates and archives with per-entity async locks --- core/container.py | 2 ++ core/dependencies.py | 2 ++ services/job_service.py | 59 ++++++++++++++++++------------- tests/test_job_concurrency.py | 65 +++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 23 deletions(-) create mode 100644 tests/test_job_concurrency.py diff --git a/core/container.py b/core/container.py index 260782f..ec1a654 100644 --- a/core/container.py +++ b/core/container.py @@ -132,6 +132,8 @@ class ServiceContainer: _fallback_registry: SessionRegistry = field(default_factory=SessionRegistry) live_interview_locks: dict[str, asyncio.Lock] = field(default_factory=dict) live_interview_locks_guard: asyncio.Lock = field(default_factory=asyncio.Lock) + job_locks: dict[str, asyncio.Lock] = field(default_factory = dict) + job_locks_guard: asyncio.Lock = field(default_factory = asyncio.Lock) def registry_for(self, app) -> SessionRegistry: """The session runtime registry this request should use. diff --git a/core/dependencies.py b/core/dependencies.py index 923e182..0c8b274 100644 --- a/core/dependencies.py +++ b/core/dependencies.py @@ -226,6 +226,8 @@ def get_job_service(request: Request) -> JobService: job_repository=container.job_repository, jd_analyzer_factory=container.jd_analyzer_factory_for(request.app), rubric_repository=container.rubric_repository, + locks = container.job_locks, + locks_guard = container.job_locks_guard, ) diff --git a/services/job_service.py b/services/job_service.py index 4e9a299..f1f2234 100644 --- a/services/job_service.py +++ b/services/job_service.py @@ -16,6 +16,7 @@ from __future__ import annotations import uuid +import asyncio from typing import Any, Dict, Optional from agents.jd_analyzer.agent import JDAnalyzerAgent @@ -42,6 +43,8 @@ def __init__( jd_analyzer_factory=None, rubric_repository: Optional[RubricRepository] = None, rubric_generator_factory=None, + locks: dict[str, asyncio.Lock] | None = None, + locks_guard: asyncio.Lock | None = None, ) -> None: self._jobs = job_repository # The same wiring-seam pattern as InterviewService's @@ -54,6 +57,12 @@ def __init__( # makes auto-rubric-on-create active in the actual app. self._rubrics = rubric_repository self._rubric_generator_factory = rubric_generator_factory + self._locks: dict[str, asyncio.Lock] = locks if locks is not None else {} + self._locks_guard = locks_guard if locks_guard is not None else asyncio.Lock() + + async def _lock_for(self, job_id: str) -> asyncio.Lock: + async with self._locks_guard: + return self._locks.setdefault(job_id, asyncio.Lock()) async def create_job( self, @@ -221,21 +230,23 @@ async def update_job(self, job_id: str, patch: Dict[str, Any]) -> JobRecord: 1.0), so an edit that breaks that invariant is rejected with the same 422 a bad `POST /jobs` payload would get, not silently stored. """ - record = await self.get_job(job_id) - if not record.is_active: - raise ConflictError( - "Cannot update an archived job", - internal_detail=f"job_id={job_id!r} is archived", - ) - - merged = record.job.model_dump() - merged.update(patch) - updated_job = JobDescription(**merged) - - updated_record = record.model_copy(update={"job": updated_job}) - stored = await self._jobs.save(updated_record) - logger.info("job updated", extra=log_context(event="job_updated", job_id=job_id)) - return stored + lock = await self._lock_for(job_id) + async with lock: + record = await self.get_job(job_id) + if not record.is_active: + raise ConflictError( + "Cannot update an archived job", + internal_detail=f"job_id={job_id!r} is archived", + ) + + merged = record.job.model_dump() + merged.update(patch) + updated_job = JobDescription(**merged) + + updated_record = record.model_copy(update={"job": updated_job}) + stored = await self._jobs.save(updated_record) + logger.info("job updated", extra=log_context(event="job_updated", job_id=job_id)) + return stored async def archive_job(self, job_id: str) -> JobRecord: """Soft-delete: mark a job as no longer accepting applications. @@ -245,14 +256,16 @@ async def archive_job(self, job_id: str) -> JobRecord: both. Idempotent: archiving an already-archived job just returns its current state rather than erroring. """ - record = await self._jobs.archive(job_id) - if record is None: - raise NotFoundError( - "No job found for the given job_id", - internal_detail=f"job record missing for job_id={job_id!r}", - ) - logger.info("job archived", extra=log_context(event="job_archived", job_id=job_id)) - return record + lock = await self._lock_for(job_id) + async with lock: + record = await self._jobs.archive(job_id) + if record is None: + raise NotFoundError( + "No job found for the given job_id", + internal_detail=f"job record missing for job_id={job_id!r}", + ) + logger.info("job archived", extra=log_context(event="job_archived", job_id=job_id)) + return record __all__ = ["JobService"] diff --git a/tests/test_job_concurrency.py b/tests/test_job_concurrency.py new file mode 100644 index 0000000..177059e --- /dev/null +++ b/tests/test_job_concurrency.py @@ -0,0 +1,65 @@ +import asyncio, pytest +from repositories.interfaces import JobRecord +from repositories.memory import InMemoryJobRepository +from schemas.job import JobDescription +from services.job_service import JobService + +class ConcurrentTestRepo(InMemoryJobRepository): + async def get(self, job_id): + res = await super().get(job_id) + await asyncio.sleep(0.01) + return res + + +@pytest.mark.asyncio +async def test_concurrent_job_updates_preserve_both_fields(): + repo = ConcurrentTestRepo() + record = JobRecord(job_id = "job_rate_test", + job = JobDescription( + job_id = "job_rare_test", + title = "Original", + description = "Sample role", + ), + ) + + await repo.save(record) + service = JobService(job_repository = repo) + + await asyncio.gather( + service.update_job(record.job_id, {"department": "Platform"}), + service.update_job(record.job_id, {"title": "Updated"}), + ) + + final = await repo.get(record.job_id) + + assert final.job.department == "Platform", "Department edit was lost!" + assert final.job.title == "Updated", "Title edit was lost!" + +@pytest.mark.asyncio +async def test_concurrent_updates_to_different_jobs_do_not_interfere(): + repo = ConcurrentTestRepo() + job_1 = JobRecord( + job_id = "job_1", + job = JobDescription(job_id = "job_1", title = "Role 1", description = "Desc 1"), + ) + job_2 = JobRecord( + job_id = "job_2", + job = JobDescription(job_id = "job_2", title = "Role 2", description = "Desc 2"), + ) + await repo.save(job_1) + await repo.save(job_2) + service = JobService(job_repository = repo) + + await asyncio.gather( + service.update_job("job_1", {"title": "Updated Role 1"}), + service.update_job("job_2", {"title": "Updated Role 2"}), + ) + + res_1 = await repo.get("job_1") + res_2 = await repo.get("job_2") + + assert res_1.job.title == "Updated Role 1" + assert res_2.job.title == "Updated Role 2" + + + From fc0c71a992de422de82a185135eeb3c88b097529 Mon Sep 17 00:00:00 2001 From: Siddhartha Kushwaha Date: Mon, 26 Oct 2026 17:34:07 +0530 Subject: [PATCH 2/2] fixed --- core/container.py | 4 +- core/dependencies.py | 4 +- services/job_service.py | 29 ++----- tests/test_job_concurrency.py | 152 ++++++++++++++++++++++++---------- 4 files changed, 119 insertions(+), 70 deletions(-) diff --git a/core/container.py b/core/container.py index e130ed7..0cfb4b6 100644 --- a/core/container.py +++ b/core/container.py @@ -148,8 +148,8 @@ class ServiceContainer: _fallback_registry: SessionRegistry = field(default_factory=SessionRegistry) live_interview_locks: dict[str, asyncio.Lock] = field(default_factory=dict) live_interview_locks_guard: asyncio.Lock = field(default_factory=asyncio.Lock) - job_locks: dict[str, asyncio.Lock] = field(default_factory = dict) - job_locks_guard: asyncio.Lock = field(default_factory = asyncio.Lock) + job_locks: dict[str, asyncio.Lock] = field(default_factory=dict) + job_locks_guard: asyncio.Lock = field(default_factory=asyncio.Lock) def registry_for(self, app) -> SessionRegistry: """The session runtime registry this request should use. diff --git a/core/dependencies.py b/core/dependencies.py index 27c207b..373371a 100644 --- a/core/dependencies.py +++ b/core/dependencies.py @@ -242,8 +242,8 @@ def get_job_service(request: Request) -> JobService: job_repository=container.job_repository, jd_analyzer_factory=container.jd_analyzer_factory_for(request.app), rubric_repository=container.rubric_repository, - locks = container.job_locks, - locks_guard = container.job_locks_guard, + locks=container.job_locks, + locks_guard=container.job_locks_guard, ) diff --git a/services/job_service.py b/services/job_service.py index 9a94dc0..0ffc2d7 100644 --- a/services/job_service.py +++ b/services/job_service.py @@ -15,8 +15,8 @@ """ from __future__ import annotations -import uuid import asyncio +import uuid from typing import Any, Dict, Optional from pydantic import ValidationError @@ -43,7 +43,7 @@ class JobService: """Use cases for job postings. Constructed per request, like `InterviewService` - it holds only - references (a repository, an optional agent factory), never state. + references to collaborators and the container's shared per-job locks. """ def __init__( @@ -69,7 +69,7 @@ def __init__( self._rubric_generator_factory = rubric_generator_factory self._locks: dict[str, asyncio.Lock] = locks if locks is not None else {} self._locks_guard = locks_guard if locks_guard is not None else asyncio.Lock() - + async def _lock_for(self, job_id: str) -> asyncio.Lock: async with self._locks_guard: return self._locks.setdefault(job_id, asyncio.Lock()) @@ -307,30 +307,15 @@ async def update_job(self, job_id: str, patch: Dict[str, Any]) -> JobRecord: merged = record.job.model_dump() merged.update(patch) - updated_job = JobDescription(**merged) + try: + updated_job = JobDescription(**merged) + except ValidationError as exc: + raise JobPatchValidationError(exc) from exc updated_record = record.model_copy(update={"job": updated_job}) stored = await self._jobs.save(updated_record) logger.info("job updated", extra=log_context(event="job_updated", job_id=job_id)) return stored - record = await self.get_job(job_id) - if not record.is_active: - raise ConflictError( - "Cannot update an archived job", - internal_detail=f"job_id={job_id!r} is archived", - ) - - merged = record.job.model_dump() - merged.update(patch) - try: - updated_job = JobDescription(**merged) - except ValidationError as exc: - raise JobPatchValidationError(exc) from exc - - updated_record = record.model_copy(update={"job": updated_job}) - stored = await self._jobs.save(updated_record) - logger.info("job updated", extra=log_context(event="job_updated", job_id=job_id)) - return stored async def archive_job(self, job_id: str) -> JobRecord: """Soft-delete: mark a job as no longer accepting applications. diff --git a/tests/test_job_concurrency.py b/tests/test_job_concurrency.py index 177059e..4248b9e 100644 --- a/tests/test_job_concurrency.py +++ b/tests/test_job_concurrency.py @@ -1,65 +1,129 @@ -import asyncio, pytest +"""Exercise shared request locks with controlled repository interleavings.""" +import asyncio +from types import SimpleNamespace + +import pytest +from fastapi import Request + +from core.dependencies import get_job_service +from core.errors import ConflictError from repositories.interfaces import JobRecord from repositories.memory import InMemoryJobRepository from schemas.job import JobDescription -from services.job_service import JobService +from services.job_service import JobPatchValidationError + + +class PausingJobRepository(InMemoryJobRepository): + def __init__(self): + super().__init__() + self.entered = asyncio.Queue() + self.release = asyncio.Event() -class ConcurrentTestRepo(InMemoryJobRepository): async def get(self, job_id): - res = await super().get(job_id) - await asyncio.sleep(0.01) - return res + record = await super().get(job_id) + await self.entered.put(("get", job_id)) + await self.release.wait() + return record + async def archive(self, job_id): + await self.entered.put(("archive", job_id)) + await self.release.wait() + return await super().archive(job_id) -@pytest.mark.asyncio -async def test_concurrent_job_updates_preserve_both_fields(): - repo = ConcurrentTestRepo() - record = JobRecord(job_id = "job_rate_test", - job = JobDescription( - job_id = "job_rare_test", - title = "Original", - description = "Sample role", - ), + +async def request_services(repo, *job_ids): + for job_id in job_ids: + await repo.save(JobRecord( + job_id=job_id, + job=JobDescription(job_id=job_id, title="Original", description="Sample role"), + )) + container = SimpleNamespace( + job_repository=repo, + rubric_repository=None, + jd_analyzer_factory_for=lambda app: None, + job_locks={}, + job_locks_guard=asyncio.Lock(), ) + app = SimpleNamespace(state=SimpleNamespace(container=container)) + # Resolve separately, just as two HTTP requests do. + return tuple(get_job_service(Request({"type": "http", "app": app})) for _ in range(2)) - await repo.save(record) - service = JobService(job_repository = repo) - await asyncio.gather( - service.update_job(record.job_id, {"department": "Platform"}), - service.update_job(record.job_id, {"title": "Updated"}), - ) +@pytest.mark.asyncio +async def test_concurrent_job_updates_preserve_both_fields(): + repo = PausingJobRepository() + first, second = await request_services(repo, "job_1") + async with asyncio.TaskGroup() as tasks: + tasks.create_task(first.update_job("job_1", {"department": "Platform"})) + assert await asyncio.wait_for(repo.entered.get(), 1) == ("get", "job_1") + tasks.create_task(second.update_job("job_1", {"title": "Updated"})) + try: + await asyncio.sleep(0) # Let the second request attempt its read. + assert repo.entered.empty(), "Second request read a stale snapshot" + finally: + repo.release.set() - final = await repo.get(record.job_id) + final = await repo.get("job_1") + assert final.job.department == "Platform" + assert final.job.title == "Updated" - assert final.job.department == "Platform", "Department edit was lost!" - assert final.job.title == "Updated", "Title edit was lost!" @pytest.mark.asyncio async def test_concurrent_updates_to_different_jobs_do_not_interfere(): - repo = ConcurrentTestRepo() - job_1 = JobRecord( - job_id = "job_1", - job = JobDescription(job_id = "job_1", title = "Role 1", description = "Desc 1"), - ) - job_2 = JobRecord( - job_id = "job_2", - job = JobDescription(job_id = "job_2", title = "Role 2", description = "Desc 2"), - ) - await repo.save(job_1) - await repo.save(job_2) - service = JobService(job_repository = repo) + repo = PausingJobRepository() + first, second = await request_services(repo, "job_1", "job_2") + async with asyncio.TaskGroup() as tasks: + tasks.create_task(first.update_job("job_1", {"title": "Updated Role 1"})) + tasks.create_task(second.update_job("job_2", {"title": "Updated Role 2"})) + try: + entered = [await asyncio.wait_for(repo.entered.get(), 1) for _ in range(2)] + assert set(entered) == {("get", "job_1"), ("get", "job_2")} + finally: + repo.release.set() - await asyncio.gather( - service.update_job("job_1", {"title": "Updated Role 1"}), - service.update_job("job_2", {"title": "Updated Role 2"}), - ) + assert (await repo.get("job_1")).job.title == "Updated Role 1" + assert (await repo.get("job_2")).job.title == "Updated Role 2" - res_1 = await repo.get("job_1") - res_2 = await repo.get("job_2") - assert res_1.job.title == "Updated Role 1" - assert res_2.job.title == "Updated Role 2" +@pytest.mark.asyncio +@pytest.mark.parametrize("archive_first", [False, True]) +async def test_update_and_archive_share_the_request_lock(archive_first): + repo = PausingJobRepository() + first, second = await request_services(repo, "job_1") + async with asyncio.TaskGroup() as tasks: + tasks.create_task( + first.archive_job("job_1") if archive_first + else first.update_job("job_1", {"title": "Updated"}) + ) + await asyncio.wait_for(repo.entered.get(), 1) + async def following_request(): + if archive_first: + with pytest.raises(ConflictError): + await second.update_job("job_1", {"title": "Updated"}) + else: + await second.archive_job("job_1") + tasks.create_task(following_request()) + try: + await asyncio.sleep(0) + assert repo.entered.empty(), "Update and archive did not serialize" + finally: + repo.release.set() + + final = await repo.get("job_1") + assert final.is_active is False + assert final.job.title == ("Original" if archive_first else "Updated") + + +@pytest.mark.asyncio +async def test_validation_failure_releases_lock_for_next_request(): + repo = PausingJobRepository() + first, second = await request_services(repo, "job_1") + repo.release.set() + with pytest.raises(JobPatchValidationError): + await first.update_job("job_1", {"openings": None}) + updated = await asyncio.wait_for(second.update_job("job_1", {"title": "Updated"}), 1) + assert updated.job.title == "Updated" + assert updated.job.openings == 1